单元测试方法属性(Unit Test Method Attribute)

Additional test attributes(可以在测试方法上使用的属性)
As you have seen, the unit-testing subsystem within Visual Studio uses attributes to identify test cases.
A number of additional properties can be set to provide further information about a test case. This
information is then accessible either via the Properties window associated with a test case or within the
other test windows. This section goes through the descriptive attributes that can be applied to a test method.


Description(描述测试用例的额外信息)
Because test cases are listed by the test method name, a number of tests may have similar names, or names
that are not descriptive enough to indicate what functionality they test. The Description attribute, which
takes a String as its sole argument, can be applied to a test method to provide additional information about
a test case.


Owner(描述测试用例的作者或所属测试用例)
The Owner attribute, which also takes a String argument, is useful for indicating who owns, wrote, or is
currently working on a particular test case.


Priority(描述测试用例的优先级,该属性不会显示在测试框架中)
The Priority attribute, which takes an Integer argument, can be applied to a test case to indicate the
relative importance of a test case. Though the testing framework does not use this attribute, it is useful for
prioritizing test cases when you are determining the order in which failing, or incomplete, test cases are
resolved.


TestCategories(测试分类:可将多个测试用例归为一类,或则将一个测试用例归到多类中,测试框架可以选择按类别显示)
The TestCategory attribute accepts a single String identifying one user-defied category for the test. Like
the Priority attribute, the TestCategory attribute is essentially ignored by Visual Studio but is useful
for sorting and grouping related items together. A test case may belong to many categories but must have a
separate attribute for each one.


WorkItems
The WorkItem attribute can be used to link a test case to one or more work items in a work-item tracking
system such as Team Foundation Server. If you apply one or more WorkItem attributes to a test case, you
can review the test case when making changes to existing functionality. You can read more about Team
Foundation Server in Chapter 57, “Team Foundation Server.”
Ignore
You can temporarily prevent a test method from running by applying the Ignore attribute to it. Test
methods with the Ignore attribute will not be run and will not show up in the results list of a test run.


Timeout(超时时间,毫秒为单位,但测试用例运行时间超过指定时间后认为测试失败)
A test case can fail for any number of reasons. A performance test, for example, might require a particular
functionality to complete within a specifid time frame. Instead of the tester writing complex multithreading
tests that stop the test case when a particular timeout has been reached, you can apply the Timeout attribute
to a test case with a timeout value in milliseconds, as shown in the following code. This ensures that the test
case fails if that timeout is reached.

 

[TestMethod()]
[Owner("Mike Minutillo")]
[Description("Tests the functionality of the Current Status Method")]
[Priority(3)]
[Timeout(10000)]
[TestCategory("Financial")]
public void CurrentStatusTest()
{
Subscription target = new Subscription();
Subscription.Status actual;
actual = target.CurrentStatus;
Assert.AreEqual(Subscription.Status.Temporary, actual,
"Subscription.CurrentStatus was not set correctly.");
}

Code Illustrate
    This snippet augments the original CurrentStatusTest method with some of these attributes to illustrate
their usage. In addition to providing additional information about what the test case does and who wrote it,
this code assigns the test case a priority of 3 and a category of "Financial" . Lastly, the code indicates that
this test case should fail if it takes more than 10 seconds (10,000 milliseconds) to execute.


posted @ 2015-02-14 12:39    阅读(825)  评论(6编辑  收藏  举报