Improvements in Windows Store Unit Tests: Test Exceptions in Async Lambda Expressions, Run Tests on the UI Thread

Charles Sterling

Microsoft Visual Studio 2012 includes support for unit testing managed Windows Store apps and includes unit test library templates for Visual C#, Visual Basic and Visual C++. For more information about these, see Creating and Running Unit Tests for Windows Store Apps. The namespace Microsoft.VisualStudio.TestPlatform.UnitTestFramework contains a rich set of asserts that can be used inside the TestMethod.

The CTP2 release of VS Update2 adds two new features in the Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AppContainer namespace to make unit testing Window Store apps even easier:

  • The Assert.ThrowsException<T> method enables you to test whether an exception is thrown in an async lamba expression.
  • The UITestMethodAttribute attribute [UITestMethod] enables you to run unit tests on the main UI thread.

Download the CTP2 release of Visual Studio Update2

Test Exceptions in Async Lambda Expressions:

One of the supported asserts in Microsoft.VisualStudio.TestPlatform.UnitTestFramework is Assert.ThrowsException. Usage: Assert.ThrowsException<T>(Action) or Assert.ThrowsException<T>(Func<object>).

Most Windows Store apps have a lot of async calls. It is possible to have asynchronous calls in Windows Store Unit Tests using async Task TestMethod. With the CTP2 release of VS Update2, the Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AppContainer.Assert.ThrowsException can be used to test async lambda expressions. For doing await within the context of Assert.ThrowsException() in an async method, we need to use the Assert class in the new namespace Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AppContainer.

Here’s how you can use the new ThrowsException method:

using AsyncAssert = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AppContainer.Assert;

 

[TestMethod]

public async Task TestMethod1()

{

    await AsyncAssert.ThrowsException<Exception>(

        async () =>

        {

            await Task.Run(() => { throw new Exception(“Exception message”); });

        }

    );

}

Run Tests on the UIThread in Windows Store Test Projects:

When you create a unit test project for a Windows store apps using the Unit Test Library template in Windows Store section of Create New Project dialogue, tests marked with the TestMethod attribute don’t run on the UI thread. Previously, in situations when you wanted to perform actions on the UI thread in your tests, such as creating a UI element, you had to use a somewhat complicated approach like the one described here.

With the CTP2 release of VS Update2, you can now use the UITestMethod attribute to run unit tests for Windows Store Apps on the main UI thread. The UITestMethod is in the Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AppContainer namespace and you should have the attribute UITestMethod for your tests instead of the attribute TestMethod.

Here’s a simple example of using UITestMethod:


using Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AppContainer

 

 

[UITestMethod]

public void TestMethod1()

{

    BitmapImage bm = new BitmapImage();   //Some code to execute on UI Thread

}

0 comments

Discussion is closed.

Feedback usabilla icon