Did you learn to test it? (2): Introduction to the properties of test syntax

Source: Internet
Author: User

Objective

This short series of one by one explains. NET to test the relevant knowledge, hope to help beginners.

In the previous article I introduced a few test tools, we recommend the use of testdriven.net. To the completion of this document the official testdriven.net-2.14.2190 beta version (direct download) and the testdriven.net-2.13.2184 official version (direct download) are available. This article is the second in this series, and I will use this tool to introduce some of the important attributes supported by TestDriven.NET.

Property

TestDriven.NET supports a variety of unit testing frameworks, like the Nunit,mbunit,ms Team System, where I chose the most classic NUnit unit testing framework to introduce some of the important attributes that TestDriven.NET supports. TestDriven.NET has actually supported most nunit properties, but some properties are not yet supported.

Before we use the TestDriven.NET test, the project must refer to the framework's assembly, which is nunit.framework.dll, and must use a using statement to reference the assembly in each source file that contains the test, like this: using Nunit.framework; In NUnit, all attributes are included in the Nunit.framework namespace.

First we are familiar with these attributes in turn.

1.TestFixtureAttribute

This property is used to modify the test class to indicate that the class contains a test method. Note that there are some restrictions on using this property to decorate a class: This class must be public and must have a default constructor.

System;  nunit.framework;  testdrivennet{    [testfixture]    yjingleefixture    {        // ......    }}
2.TestAttribute

This property marks a method of a class as a test method that has been marked as a testfixture. The signature of a test method is defined as follows:

[Test]  TestMethod () {}

Note that this method must have no parameters. If the programmer marks the test method as an incorrect signature, it will not run.

3.SetUpAttribute

This property is used to modify the method, and after this method is run before each test method is called, we can use it to reset some variables and assign values before each method runs.

[SetUp]  Init () {}
4.TearDownAttribute

This property is used to modify the method, which means that the method is run after each test method is called, and we can use it to release some of the staged variables.

[TearDown]  Dispose () {}
5.SetUpFixtureAttribute

This property is used to modify the class, which contains the Setupattribute or Teardownattribute attribute, which must be public and a default constructor. As long as this property is used, under its namespace, running the test first runs the method in which the Setupattribute adornment is run, and the method in which the Teardownattribute adornment is run at the end of the run test. Note that there is only one setupfixtureattribute under a namespace, and if this property is defined under the entire assembly, it is valid under the entire assembly. We often use it to set global conditions.

[setupfixture]  mysetupclass{    [SetUp]    runbeforeanytests ()    {    }    [  TearDown]    runafteranytests ()    {    }}
6.TestFixtureSetUpAttribute

This property is used to modify the method, which is used to run before any test execution fixture, and we often use it to initialize some objects, like constructors in a class.

[Testfixturesetup]  Fixtureinit () {}
7.TestFixtureTearDownAttribute

This property is used to modify the method, after which the method runs after fixture any test execution, and we often use it to release some resources.

[Testfixtureteardown]  Fixturedispose () {}
8.ExpectedExceptionAttribute

Using this property indicates that the method throws an expected exception. This method is used to indicate the exception that will be thrown when this test is executed. Type, which is the exact type of the expected exception. The second one is a string of expected exception names. Either way, the test passes when the test is executed, if it throws the specified exception. If a different exception is thrown, the test fails. This is also successful if you throw an exception that is inherited from the expected exception.

[Test] [expectedexception(typeof(InvalidOperationException))]  Expectanexceptionbytype () {}[Test] [expectedexception(" System.InvalidOperationException ")]expectanexceptionbyname () {}
9.PlatformAttribute

Platform properties are used to specify test method test methods or platforms that test fixture run. The platform selection includes various operating systems and the. NET Framework versions. Use a string that is not case-insensitive to specify the platform, or to include or exclude a running platform by using the include or exclude property. You can also specify the Platformattribute parameter. In either case, you can separate strings with multiple commas.

Testfixture syntax

[testfixture] [Platform("NET-2.0")]  yjingleefixture{}

Test syntax

[Test] [Platform"WinXP")]  sometest () {}

Platform-specific values: Win series, Unix, Linux, Net, Net-1.0, Net-1.1, Net-2.0, NETCF, etc. They can be platform-specified values: Win series, Unix, Linux, Net, Net-1.0, Net-1.1, Net-2.0, NETCF, etc. They can be uppercase, lowercase, or mixed.

10.CategoryAttribute

This property can designate certain test methods or test fixture as belonging to a particular classification. When using classifications, only the selected categories can be tested. A category test that is not selected will not run. For example we have some tests that need to run for a long time and certainly don't want to run it every time. You can put these tests into a category and then exclude them from the test in the NUnit GUI. Note that this property is not supported in TestDriven.NET.

Testfixture syntax

[testfixture] [Category("longrunning")]  yjingleefixture{}

Test syntax

[Test] [Category("Verylong")]  verylongtest () {}
11.ExplicitAttribute

This property ignores a test method or test fixture until it is explicitly selected to run. If you specify it (for example, if you put your mouse on this method and then select Runtest) The test method will run. We are often used to test methods that are temporarily avoided.

Testfixture syntax

[testfixture,Explicit]  yjingleefixture{}

Test syntax

[TestExplicit]  explicittest () {}
12.SuiteAttribute

Suite properties are used to define collections based on user preferences. It is not commonly used in testing because the framework provides a dynamic creation mechanism.

13.IgnoreAttribute

This property indicates that this test method or test fixture will be ignored. This method or test fixture will not run for a period of time. We can mark the test method or fixture as the Ignore property and will not execute when the test is run. For example, we often use this attribute to mark tests that are temporarily not running or to refactor the software, instead of using annotations or renaming methods, so that the test code compiles with the code with this tag and does not run the tagged test code at run time, ensuring that the past tests are not forgotten.

Testfixture syntax

[testfixture] [Ignore("Ignore a Fixture")]  yjingleefixture{}

Test syntax

[Test] [Ignore("Ignore a Test")]  ignoredtest () {}

In TestDriven.NET, if you use this property, the test displays the following results:

OK, so much for the properties of the NUnit Unit test framework, the TestDriven.NET test tool supports most of the properties here, and we can use this tool to complete our testing work. Next I continue to introduce you to the basic syntax of assertions, and then take a practical example of the test techniques.

Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.

Did you learn to test it? (2): Introduction to the properties of test syntax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.