IdentityMine

| Tags: Laurent Bugnion, Uncategorized

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.

01.[TestClass]
02.public class UnitTest1
03.{
04.    [TestMethod]      
05.    public void TestAlwaysPass()
06.    {
07.        const string expected = "Any text";
08.        var myClass = new ClassLibrary1.Class1(expected);
09.
10.        Assert.AreEqual(expected, myClass.Parameter);
11.    }
12.
13.    [TestMethod]
14.    public void TestAlwaysFail()
15.    {
16.        const string expected = "Any text";
17.        var myClass = new ClassLibrary1.Class1(expected);
18.
19.        const string notExpected = "Another text";
20.        Assert.AreEqual(notExpected, myClass.Parameter);
21.    }
22.}

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

Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on TumblrEmail this to someoneDigg thisFlattr the authorShare on StumbleUpon

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>