Bogdan Bujdea

Windows Platform Developer

Testing Windows 10 apps…in 2017

It’s been a while since I wrote my last article regarding testing Windows 10 apps, and some of you asked me privately or in comments to do a follow up article.

In this article I’m going to talk about unit tests, mocks, and UI tests. Let’s take a look on each one of them and see how they compare, but first let’s check the setup.

We have a UWP app for the latest Windows 10 version, anniversary edition, which will have a Calculator class and a ViewModel class that inherits from Screen(Caliburn.Micro). You can find the code for the projects on my github account: https://github.com/thewindev/uwp-testing-samples.

Unit tests

First I tried the template Unit Test App project for Universal apps. I made two classes called MSTests and NUnitTests, each of them containing these two basic unit tests:

 

using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;

namespace Calculator.UnitTestsApp
{
  [TestClass]
  public class MSTests
  {
    [TestMethod]
    public void SimpleClassTest()
    {
      var result = new UWPCalculator.Calculator().AddNumbers(2, 3);
      Assert.AreEqual(5, result);
    }

    [TestMethod]
    public void CaliburnViewModelTest()
    {
      var result = new UWPCalculator.ViewModel().MultiplyNumbers(2, 3);
      Assert.AreEqual(6, result);
    }
  }
}

As you can see from the image below, only MSTest works for this type of project, and you can run them with Resharper Test Runner and of course with Test Explorer. But when I tried to run NUnit tests with Resharper I had a TargetInvocationException so their result is set to inconclusive.

I also tried to use a class library with the same unit tests but I wasn’t able to run them, even if I used Resharper or Test Explorer. So the only way of doing unit tests is to use a Unit Test app with MSTest. I’m ok with that, now let’s talk about mocking.

Mocks

You can’t have unit tests without mocks, so what alternatives do we have?

I only tried HyperMock so far and I think it works really well. Below you have a small example where I’m inserting a mock of the ICalculator interface inside the ViewModel constructor, and then I setup AddNumbers so that it would return 4 when you add any integers.

Here’s a small example:

 

[TestClass]
    public class MockTests
    {
        private Mock _mockService;

        [TestMethod]
        public void mock_Test()
        {
            _mockService = Mock.Create();
            _mockService.Setup(c => c.AddNumbers(Param.IsAny(), Param.IsAny())).Returns(4);
            var viewModel = new ViewModel(_mockService.Object);
            var result = viewModel.AddNumbers(2, 13);
            Assert.AreEqual(4, result);
        }
    }

Coded UI Tests

It’s a little bit harder than testing classic desktop apps, but you can use the Coded UI tests template for Universal apps. I’m saying it’s a bit harder because you can’t record steps, but you can write code so it’s still something. Here’s an example where I have two text box controls for numbers, a button for calculating the result and another text box for the result.

 

            XamlWindow myapp = XamlWindow.Launch("09e8d05c-4c18-47d3-9a45-4398fdb2ca4d_gr83fyt2yaqj0!App");
            await Task.Delay(500);
            UIMap.UIUWPCalculatorWindow.UIFirstEdit.Text = "2";
            await Task.Delay(500);
            UIMap.UIUWPCalculatorWindow.UISecondEdit.Text = "3";
            await Task.Delay(500);
            Mouse.Click(UIMap.UIUWPCalculatorWindow.UICalculateButton, MouseButtons.Left);
            Assert.AreEqual("5", UIMap.UIUWPCalculatorWindow.UIResultEdit.Text);

I used the Coded UI Test Builder to get a reference to the controls, I changed the text value, then I invoked a click event on the button and verified that the result text box has the correct output. I will do another article on Coded UI Tests, but for this article it’s enough to say that they are a good alternative for UI testing.

Conclusion

So it’s been one and a half years since Windows 10 was released, and it’s clear that they made some progress regarding testing UWP apps and now you have at least some options if you want to write tests. I’m still not satisfied because I think that testing is an important part of programming, and the fact that there are still some limitations it’s unacceptable, but hey, it’s only my opinion!

Tools that I used:

JetBrains ReSharper Ultimate 2016.2.2  Build 106.0.20160913.91321

Visual Studio 2015 Enterprise & Visual Studio 2017 RC Enterprise

, , , , ,