Running unit tests in Visual Studio 2011 and Windows 8 (WinRT)

This is a quick tip, because it confused me at first. But thanks to the always excellent Tim Heuer and Peter Provost (from the Visual Studio team), here is the answer:

Usual disclaimer: This is for XAML/C#. I am not sure how this works for the other programming stacks.

Creating unit tests for your WinRT application/library

  • Start Visual Studio 2011.
  • Create a new project.
  • From the Add New Project dialog, select Unit Test Library (In the Visual C#/Windows Metro Style category). Give a name to the project and press OK.
  • Either open the unit test class that was created, or create a new class. No need to select a fancy template, just create a new empty class.
  • Decorate the class with a [TestClass] attribute.
  • Create a public method with no parameters, and decorate it with a [TestMethod] attribute.
  • Right click on the unit test project and select Add Reference from the context menu.
  • In the Reference Manager, select Solution and then the project you want to write tests for. Then press Add and then Close.

You can now write your tests, using the usual Assert syntax. Here is a simple example.

[TestClass]
public class UnitTest1
{
    [TestMethod]        
    public void TestAlwaysPass()
    {
        const string expected = "Any text";
        var myClass = new ClassLibrary1.Class1(expected);

        Assert.AreEqual(expected, myClass.Parameter);
    }

    [TestMethod]
    public void TestAlwaysFail()
    {
        const string expected = "Any text";
        var myClass = new ClassLibrary1.Class1(expected);

        const string notExpected = "Another text";
        Assert.AreEqual(notExpected, myClass.Parameter);
    }
}

Running the unit tests

To run the unit tests you just wrote, follow the steps:

  • Select the menu View / Other Windows / Unit Test Explorer.

  • Build your application. You should now see the unit tests you wrote in the explorer window.

 

  • Press Run All to run all the unit tests.

Hopefully this quick tip will be helpful!

Cheers

Laurent

 

Print | posted on Saturday, September 24, 2011 12:50 AM

Feedback

# re: Running unit tests in Visual Studio 2011 and Windows 8 (WinRT)

left by rob at 1/17/2012 5:37 PM Gravatar
Thanks Laurent, any idea how to configure where the test output goes?
I ran a test created in vs 2010 and no output was generated (as far as I can tell)

# re: Running unit tests in Visual Studio 2011 and Windows 8 (WinRT)

left by Paul at 5/10/2012 12:23 AM Gravatar
Having a reference to the Microsoft.Media.SmoothStreaming.dll in a Metro Style App project, then attempting to run tests from a separate Unit Test project does not work in Visual Studio 11, even with the processor types both set to either x86 or x64 in the Debug compilation configuration.

The platform target of the Metro Style App must be set to either x86 or x64 (Any CPU won't work with the SmoothStreaming SDK assembly). The Unit Test project target must match the setting of the Metro Style App when it contains a reference to the Metro Style App assembly, otherwise you get the following error:

"Error 1 There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "%APPROOT%\bin\x86\Debug\UVerseApp.exe", "x86". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project"

When both are set to x64, the solution compiles and runs fine but the Unit Test Explorer will not discover any of the tests from the unit test project, so you can't run them or even see them.

When both are set to x86, the solution builds fine and the Unit Test Explorer properly displays all tests. However, if you attempt to run any of the tests, you encounter an application error in vstest.executionengine.appcontainer.x86.exe every time. The Metro app will still run fine - this only happens when you attempt to run or debug tests methods using Unit Test Explorer.

Is this a known issue and/or are there any possible workarounds?

# re: Running unit tests in Visual Studio 2011 and Windows 8 (WinRT)

left by Vikram Agrawal [MSFT] at 5/10/2012 6:31 AM Gravatar
Hi Paul,

By default execution and discovery happens in 32 bits mode irrespective of project flavor. To force discovery and execution in 64 bits steps are as follows:
From VS=> Unit Test -> TestSettings -> Default Processor Architecture -> x64
From commandline=> /platform:x64

This should change discovery and execution to 64 bits and tests should be discovered when you change projects to 64 bits.

You can enable logs of executor and check if any error logged in trace log files.
http://blogs.msdn.com/b/aseemb/archive/2012/03/02/how-to-enable-ute-logs.aspx#10298395

Just to double check, you took reference of SDK in test project also and enabled all capabilities in test project those are enabled in app.

If this does not solve the issue, please share a repro project, if repro project is not possible then logs to help us analyze the issue further.

# re: Running unit tests in Visual Studio 2011 and Windows 8 (WinRT)

left by Paul at 5/11/2012 9:01 PM Gravatar
Vikram,

Thanks for your excellent information. I missed the extra step you outlined to set the Default Processor Architecture to match the project configuration's target compilation architecture. Once I did that, my test methods did appear properly in the Unit Test Explorer for both x86 and x64 processor architecture settings.

However, any time I attempt to run any of the tests, regardles of processor architecture, I encounter the vstest.executionengine.appcontainer(x86).exe crash message I mentioned in my previous post.

I implemented tracing using the instructions in aseemb's blog you referenced (http://blogs.msdn.com/b/aseemb/archive/2012/03/02/how-to-enable-ute-logs.aspx#10298395), but the only indicative message I see in the log each time is:
"Error: An internal error occurred while launching test executor for Windows Metro style app." Discovery seems to work fine, and that is confirmed in the log. However, test executions fail every time.

# re: Running unit tests in Visual Studio 2011 and Windows 8 (WinRT)

left by Paul at 5/11/2012 9:37 PM Gravatar
Vikram,

I also attempted to run the tests from Command Line as detailed by your blog post at http://blogs.msdn.com/b/vikramagrawal/archive/2012/05/06/running-unit-tests-for-windows-metro-style-apps-from-command-line.aspx and received the same error message as indicated in my previous post:

"Error: An internal error occurred while launching test executor for Windows Metro style app."

# re: Running unit tests in Visual Studio 2011 and Windows 8 (WinRT)

left by Vikram Agrawal [MSFT] at 5/14/2012 3:45 AM Gravatar
Paul,

Will it be possible for you to share a small project(Remove all your product code and with just sufficient code that's encountering the issue.) with which we can reproduce the issue locally. This will enable us for investigation of issue and help you better. If you can share repro project please send it to me at agrawal_vikram[at]hotmail[dot]com

Thanks.

-Vikram

# re: Running unit tests in Visual Studio 2011 and Windows 8 (WinRT)

left by Paul at 5/14/2012 11:08 PM Gravatar
Vikram,

Thanks again for your response. We were able to get it to work by referencing the compiled exe of our source project rather than the assembly. When referencing the assembly DLL, it still fails. Is that a requirement of the Metro Style App project type?
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: