WindowsDevicePortalWrapper  0.9.0.0
A client library that wraps the Windows Device Portal REST APIs.
Running Tests

/// Summary description for DemoTest ///

There are two types of tests for excercising the wrapper library.

First, unit tests built against mock responses should be run before every Pull Request. Use Run All via the Test Explorer in Visual Studio prior to submitting any Pull Requests. You can also configure Visual Studio to automatically run the tests after each build of the solution via Test Settings in the Test menu.

runalltests

Second, manual or semi-automated tests can also be run on a per-device basis via the device specific apps. See the following for information on running these apps:

For Xbox One: https://github.com/Microsoft/WindowsDevicePortalWrapper/blob/master/XboxWDPDriver.md "Using the XboxWdpDriver to test wrappers against a real Xbox One"

Writing Tests

Windows Device Portal Wrapper (WDPW) tests are run against mock data so that a windows device is not required to run the tests.

  1. Create a Unit Test class in the Unit Test Project
    addunittest
  2. Have the test class inherit from the BaseTests class which will automatically establish your mock connection (the constructor and TestContext can be removed)
1 {c#}
2  ///
3  [TestClass]
4  public class DemoTest : BaseTests
5  {
6  }

a. If you are writing a device specific version test then override the PlatformType, the FriendlyOperatingSystemVersion used to specify where mock files are stored/named, and the OperatingSystemVersion to be used when validating the OS’s actual version number.

1 {c#}
2  /// <summary>
3 /// Gets the Platform type these tests are targeting.
4 /// </summary>
5 protected override DevicePortalPlatforms PlatformType
6 {
7  get { return DevicePortalPlatforms.XboxOne;}
8 }
9 
10 /// <summary>
11 /// Gets the friendly OS Version these tests are targeting.
12 /// </summary>
13 protected override string FriendlyOperatingSystemVersion
14 {
15  get { return "rs1_xbox_rel_1608";}
16 }
17 
18 /// <summary>
19 /// Gets the OS Version these tests are targeting.
20 /// </summary>
21 protected override string OperatingSystemVersion
22 {
23  get { return "14385_1002_amd64fre_rs1_xbox_rel_1608_160709_1700";}
24 }
  1. Define void methods with no parameters tagged with “testMethod” for each test case you want to run
1 {c#}
2  [TestMethod]
3  public void TestMethod1()
4  {
5  //
6  // TODO: Add test logic here
7  //
8  }
  1. Each test should start with a call to TestHelpers.MockHttpResponder.AddMockResponse to prepare a mock response for a specified endpoint in one of three ways:

    a. Default mock

    Use mock data from the MockData\ <endpoint>_Default.dat file.

    1 {c#}
    2  TestHelpers.MockHttpResponder.AddMockResponse(DevicePortal.MachineNameApi, HttpMethods.Get);

    b. Version specific mock

    Use mock data from the MockData\ <platform>\ <friendly OS version>\ <endpoint>_<platform>_<friendly OS version>.dat file

    1 {c#}
    2  TestHelpers.MockHttpResponder.AddMockResponse(DevicePortal.KnownFoldersApi, this.PlatformType, this.FriendlyOperatingSystemVersion, HttpMethods.Get);

    c. Provided response

    Use a response object as a mock.

    1 {c#}
    2  TestHelpers.MockHttpResponder.AddMockResponse(DevicePortal.GetFilesApi, response, HttpMethods.Get);
  2. Have the test methods use TestHelpers.Portal to call DevicePortal methods and then Assert methods to validate results
1 {c#}
2  Task<string> getNameTask = TestHelpers.Portal.GetDeviceName();
3  getNameTask.Wait();
4  Assert.IsNotNull(TestHelpers.Portal.OperatingSystemVersion);
  1. After building the test right click on it in the test explorer and select “Debug Selected Tests” to be able to hit selected break points as it runs.

    debugselectedtests

Collecting Data for Tests

Using MockDataGenerator.exe to Generate Mock Data

MockDataGenerator.exe, from the MockDataGenerator project, is used to target a WDP instance to create mock data.

MockDataGenerator Parameters

Parameter Purpose
/Address URL for device (eg. https://10.0.0.1:11443)
/User WDP username
/Pwd WDP password
/Endpoint API to call (default is all endoints in program.cs)
/Method HTTP method to use (default is GET)
/Directory Directory to save mock data file(s) (default is .)
/requestBody File to use for the request body. Only applicable when /Endpoint is present.
/requestBodyMultiPartFile Normally the requestBody file is considered JSON and provided as is. Specify this parameter to include it as a multipart file instead.

MockDataGenerator File Name Format

All mock data is saved as files in the <endpoint>_<platform>_<friendly OS version>.dat format, such as api_os_devicefamily_XboxOne_rs1_xbox_rel_1608.dat.

All file names are pre-pended with the HTTP method except for GET (as it is the default). Hence the file name for calling the System Performance API with GET is api_resourcemanager_systemperf_XboxOne_rs1_xbox_rel_1608.dat but calling the WebSocket is WebSocket_api_resourcemanager_systemperf_XboxOne_rs1_xbox_rel_1608.dat.

MockDataGenerator Examples

All examples connect to 10.0.0.1:11443 with username TestUser and password SuperSecret.

1 MockDataGenerator.exe /ip: 10.0.0.1:11443 /user:TestUser /pwd:SuperSecret
1 MockDataGenerator.exe /ip: 10.0.0.1:11443 /user:TestUser /pwd:SuperSecret /endpoint:api/os/devicefamily

or

1 MockDataGenerator.exe /ip: 10.0.0.1:11443 /user:TestUser /pwd:SuperSecret /endpoint:api/os/devicefamily /method:Get
1 MockDataGenerator.exe /ip: 10.0.0.1:11443/user:TestUser /pwd:SuperSecret /endpoint:api/resourcemanager/systemperf /method:WebSocket

Adding Mock Data to the Solution

  1. Mock data should be added to the UnitTestProject in the MockData directory.
    • Default mock data should be added to the MockData directory.

      defaultmocks
* Device-version mock data should be added to the MockData<Device\>\ \<Friendly OS Version\> directory with the Friendly OS Version parsed from the mock data’s file name.

  ![platformspecificmocks](https://cloud.githubusercontent.com/assets/1520739/17312269/5248edf4-5806-11e6-833e-cb2445ffc0f1.png)
  1. In the properties view the mock data files should have their “Copy to Output Directory” property marked as “Copy if newer.” If this is not done then the tests will be unable to find the files.

    copytooutputdirectory