Demo – WPF TDD with xUNIT

This demo is about using WPF Test driven development with xUnit framework. xUnit.net is a free, open source, community-focused unit testing tool for the .NET Framework.

xUnit.net has two different types of unit tests: facts and theories.

  • Facts are tests which are always true. They test invariant conditions.
  • Theories are tests which are only true for a particular set of data.

In the below demo, We write few failing tests, make them pass, then repeat the process. I have installed and configured below tools and packages.

  • Visual Studio 2017 Community,
  • WPF
  • TDD with xUNIT Framework
  • Autofac for dependency injection

Our solution would look like this, 4 layers


  • WPFTDDxUNIT.Model — This contains the domain entity which is being used in the project. It is a good practice to keep domain changes to a separate project so that this is not changed accidentally by other team members.
  • WPFTDDxUNIT.DataAccess — In this demo project, Data access is carrying an in-memory collection of records. All the friend’s list is loaded from this project. You can use any relational database like SQL Server or MySQL or any other to save this data.
  • WPFTDDxUNIT.UI.WPF — WPF User Interface is carrying our front end. We want to focus on separation of concerns, so we have not crammed data access and domain into this project.
  • WPFTDDxUNIT.UITests — This is what this blog post is about. We will use this project in the solution to unit tests our view models and other classes to ensure our actual code is backed by units tests. We will use xUnit TDD framework for this task.

The weird interface would look like this, :p

Before we proceed for units test, need to confirm if the below package is installed from nuget:-


Install-Package xunit.runner.visualstudio -Version 2.4.1

If you have Visual Studio Community (or Pro or Enterprise), you can run your xUnit.net tests within Visual Studio’s built-in test runner (i.e Test Explorer).

If not installed the nuget package, please Right click on the project in Solution Explorer and choose Manage NuGet Packages. Search for (and install) a package named xunit.runner.visualstudio:

Now We go to our Unit tests projects, and they can be seen at Test Explorer. Luckily all the unit tests are green.

In the below unit test, we will violate single responsibility principle and cram multiple things to be checked. Here, we use the [Fact] attribute to denote a parameterless unit test.

We can also do the assert check for .net property change events. In the below code, we see the fact is simply going to check if the RaisePropertyChangedEvent is triggered for the friend.

In order to run single test, we can use Ctrl+R, T or Click the Run All link in the Test Explorer window, to see the results update in the Test Explorer window as the tests are run:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.