Getting started with Devices: Universal Windows Platform

In this article, we will demonstrate getting started with xUnit.net for UWP, showing you how to write and run your first set of unit tests.

  1. File -> New Project and create a Blank UWP project:

     
  2. Add the xunit.runner.devices package. If you want unit tests in this project, also add xunit:

     
  3. Replace App.xaml and App.xaml.cs with the following (using your namespace in x:Class):
    • App.xaml
       
      <ui:RunnerApplication
          x:Class="UwpTestRunner.App"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:ui="using:Xunit.Runners.UI"
          RequestedTheme="Light">
      </ui:RunnerApplication>


       
    • App.xaml.cs
       
      using System.Reflection;
      using Xunit.Runners.UI;
      namespace UwpTestRunner
      {
          sealed partial class App : RunnerApplication
          {
              protected override void OnInitializeRunner()
              {
                  // tests can be inside the main assembly
                  AddTestAssembly(GetType().GetTypeInfo().Assembly);
                  // otherwise you need to ensure that the test assemblies will 
                  // become part of the app bundle
                  // AddTestAssembly(typeof(PortableTests).GetTypeInfo().Assembly);
              }
          }
      }


       
  4. Delete MainPage.xaml and MainPage.xaml.cs:

     
  5. Add your unit tests:

     
  6. (optional) If your tests are in other assemblies, add a project reference and modify the App.xaml.cs to include the assembly containing your tests
  7. (optional) Add a xunit.runner.json file as Content to specify runner configuration
Copyright © 2017 .NET Foundation. Contributions welcomed at https://github.com/xunit/xunit.github.io.