UWP and Unit Tests

UWP and Unit Tests

uwptests1

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):

uwptests2_2_2

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

uwptests3

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"
 }

uwptests4_4_4_4

3) Remove reference called “MSTestFramework.Universal”:

uwptests5

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

using System;
using Xunit;

namespace UnitTestsForUwp
{
   public class UnitTest1
    {
      [Fact]
       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:

uwptests6

As you can see during tests application in launched:

uwptests7

Now let’s add some Theory:

 [Theory]
 [InlineData(6)]
 public void TestMethod2(int value)
  {
    Assert.True(IsOdd(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”:

<ui:RunnerApplication
  x:Class="UnitTestsForUwp.App"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="using:UnitTestsForUwp"
  xmlns:ui = "using:Xunit.Runners.UI"
  RequestedTheme="Light">
</ui:RunnerApplication>

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

 sealed partial class App : RunnerApplication
  {
    protected override void OnInitializeRunner()
     {
        AddTestAssembly(GetType().GetTypeInfo().Assembly);
        InitializeRunner();
     }
    partial void InitializeRunner();
 }

Rebuild app and launch it

uwptests8

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

uwptests9

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

uwptests10

 

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”:

uwptests11

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:

uwptests12

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”:

uwptests13

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.

Advertisements