Miscellaneous Metadata

,

The following attributes allow you to control various other aspects of test execution.

TestProperty Attribute

The TestProperty attribute allows arbitrary metadata to be associated with a test method. For example, you could use it to store the name of a test pass that this test covers by decorating the test method with [TestProperty("TestPass", "Accessibility")]. Unlike the Silverlight UTF for the browser and the Visual Studio desktop CLR unit testing tools, the Windows Phone UTF does not display TestProperty information within the test harness.

Ignore Attribute

The Ignore attribute can be used to temporarily exclude a specific test from execution. This can be useful for excluding a test that is blocking other tests from running. It allows you to retain compilation of the test, rather than merely commenting out the code.


Note

The number of tests listed in the unit test harness is unaffected by the Ignore attribute.


Description Attribute

The Description attribute is used on test methods and allows you to provide a string describing the purpose and/or behavior of the test method. The description is then presented beneath the title of the test result details screen on the phone. The following example demonstrates the use of the Description attribute:

[TestMethod]
[Description("An example test demonstrating the Description attribute.")]
public void ShouldAlwaysPass()
{
    Assert.IsTrue(true);
}

Timeout Attribute

The Timeout attribute allows you to specify an amount in milliseconds in which a test method must complete or the test method fails.

The following is an example of using the Timeout attribute to prevent an asynchronous test method from taking longer than 100 milliseconds to execute:

[TestMethod]
[Asynchronous]
[Timeout(100)]
public void ShouldFailDueToAsyncTestTimeout()
{
    EnqueueDelay(1000);
    EnqueueTestComplete();
}

This test method fails because the call to EnqueueDelay delays the completion of the test by 1000ms (1 second), and the Timeout attribute specifies that the test should take no longer than 100ms. The EnqueueDelay attribute and the other asynchronous related attributes are discussed later in this chapter.

Owner Attribute

The Owner attribute is used to specify the person responsible for maintaining, running, and/or debugging the test. This attribute accepts a single string parameter, indicating the name of the owner, as shown in the following example:

[TestMethod]
[Owner("Daniel Vaughan")]
public void ShouldAlwaysPass()
{
    Assert.IsTrue(true);
}

The value of the attribute is not displayed within the test harness of the current Windows Phone UTF.

ExpectedException Attribute

Verifying that your code produces a correct response to a known set of values is one thing; verifying that it responds appropriately when given bad input is another. This is called negative testing, and it allows you to verify that code behaves correctly even when exceptional conditions arise.

Ordinarily, if a test method raises an exception, the exception causes that test to fail. In a negative test, however, raising an exception may be the expected behavior. In that case the ExpectedException attribute can be used to indicate that if an exception of a particular type, with a particular message, is not thrown, the test should fail. The following example demonstrates the use of the ExpectedException attribute:

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void ShouldFailDueToArgumentException()
{
    throw new ArgumentException("Invalid argument supplied.");
}


Note

The TagAttribute, AsynchronousAttribute, and BugAttribute are not available in the Microsoft desktop CLR test framework. Using any of them, therefore, means that your unit tests will not be compatible with the desktop CLR.


Asynchronous Attribute

The Asynchronous attribute informs the UTF that a test method should be considered to be in a running state until the WorkItemTest.EnqueueTestComplete method is called.


Tip

Use the Asynchronous attribute in combination with the Timeout attribute to prevent the test method from taking forever if it fails to call EnqueueTestComplete.


For more information on the Asynchronous attribute, see the section “Asynchronous Testing.”

Bug Attribute

The Bug attribute allows you to associate a known bug with a unit test. Its key characteristic is that it reverses the result of a test, so you can first use it to demonstrate the existence of a bug, and after the bug is resolved, the Fixed property indicates that the test should pass. For example, if a method is able to reproduce a bug in the software, it may first look like this:

[Bug("TFS 123")]
[TestMethod]
public void ExerciseBuggyCode()
{
    Assert.IsTrue(false); /* Simulates some broken code. */
}

Consider the preceding test method. It passes because the Bug attribute’s Fixed property is false by default.

When the issue is resolved, you can add the Fixed property to the Bug attribute, which removes the inversion behavior of the Bug attribute.

[Bug("TFS 123", Fixed = true)]
[TestMethod]
public void ExerciseBuggyCode()
{
    Assert.IsTrue(true); /* Simulates issue resolved. */
}

Priority Attribute

Contrary to first assumptions, the Priority attribute is not used by the test system. Its purpose can be defined by you. It is, however, added to the set of implicit expression tags. The attribute requires an integer constructor argument, which specifies the priority value. For example:

[TestMethod]
[Priority(1)]
public void AlwaysPass()
{
    Assert.IsTrue(true, "Test method intended to always pass.");
}

We can then use the tag expression Priority1 to execute this test method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.162.105