UWP and Unit Tests

UWP and Unit Tests


Short introduction

Creating mobile application which looks great and have outstanding user experience is very important. Each functionality should be well developed and tested – that is why you should write tests for your UWP applications. This will help you check that each functionality is working fine as expected. In this article I would like to show you how to create Unit Tests for Universal Windows 10 Application. To test UWP apps we will use xUnit.net Framework about which you can read more here.

Example for this article is available on my GitHub

What do I need to start?

1) Visual Studio 2015 Community (for free or choose higher version)


Let’s start

1) Open Visual Studio and select new project type: “Unit Test App (Universal Windows):


Of course you have to choose Platform version (leave default selection):


2) Now open “project.json” file and add below line in “dependencies” section:

"dependencies": {
 "Microsoft.NETCore.UniversalWindowsPlatform": "5.1.0",
 "xunit.runner.visualstudio": "2.1.0",
 "xunit": "2.1.0",
 "xunit.runner.devices": "2.1.0"


3) Remove reference called “MSTestFramework.Universal”:


4) Now open “UnitTest.cs” file. Modify it to look like below:

using System;
using Xunit;

namespace UnitTestsForUwp
   public class UnitTest1
       public void TestMethod1()
         Assert.Equal(4, 4);

As you can see there is no reference to standard MSTest Framework – we are using xUnit Framework here.

It is good to stop here for a moment to talk a little bit about xUnit attributes:

a. Fact- tests which are always true. They test invariant conditions.

b. Theory – tests which are only true for a particular set of data.


Open Test Explorer and run tests:


As you can see during tests application in launched:


Now let’s add some Theory:

 public void TestMethod2(int value)

  bool IsOdd(int value)
    return value % 2 == 1;

As you can see “InlineData” here is a parameter for test method. In this case we will test if remainder is equal 1.

Click “Run all” in Test Explorer and see test result.


Prepare test application to display tests results

Now we need to make some changes to adjust our test app to display tests results.

1) Open xaml file with name of your project – in my case this is “UnitTestApp.xaml”. Make changes like below. You need to add “ui” namespace and change “Application” tag to “RunnerApplication”:

  xmlns:ui = "using:Xunit.Runners.UI"

Now open “UnitTestApp.xaml.cs” file and replace content with below code:

 sealed partial class App : RunnerApplication
    protected override void OnInitializeRunner()
    partial void InitializeRunner();

Rebuild app and launch it


Click “Run Everything” button. You should see result:


As you can see you have clear information which test did not passed and where Assert failed:



Write more advanced tests connected with your existing UWP application

One our test application is configured we can add some app with functionality to test. Right click on the solution and select “Add new project” then “Blank App (Universal Windows) – in my case it is “SampleTestApp”:


Here you can use application that I created earlier for test purpose. It is available on my GitHub.

Once you add project to the solution you can reference it in the test project:


Now you can access whole code available in your UWP app project. That is why you should use MVVM pattern. Now for instance we can test functionality available in “MainViewModel” or we are able to mock “ICognitiveClient”:


Sum up

Great user experience is not enough. You should always write tests for your applications. Remember also that good practice is to apply MVVM pattern so you are able separate code.