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

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

Rate This
  • Comments 2

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

}

Leave a Comment
  • Please add 2 and 2 and type the answer here:
  • Post
  • It looks like methods marked with [TestInitialize] still run on a on-UI thread even when they are being invoked on behalf of a test annotated with [UITestMethod]. This means a) your initialization runs on a different thread from your test and b) there's apparently no way to get [TestInitialize] code to run on the UI thread.

    That's rather frustrating, as in most of the cases where I want my test method to run on the UI thread, I want everything about that test (setup, teardown, the test itself) to run on the UI thread too.

  • I got the exception when I change TestMethod to UITestMethod.

    Result Message: Exception thrown while executing test. If using extension of TestMethodAttribute then please contact vendor. Error message: async TestMethod with UITestMethodAttribute are not supported. Either remove async or use TestMethodAttribute.

Page 1 of 1 (2 items)