Preface
This short series provides a one-to-one explanation of the test knowledge under. net, hoping to help beginners.
In the previous article, I introduced several test tools and recommended testdriven. net. At the end of this article, the official version testdriven. Net-2.14.2190 beta (direct download) and testdriven. Net-2.13.2184 official version (direct download) are provided ). This is the second article in this series. I will use this tool to introduce some important attributes supported by testdriven. net.
Attribute
Testdriven. net supports a variety of unit test frameworks, such as nunit, mbunit, MS team system, here I chose the most classic nunit unit test framework to introduce testdriven. net supports some important attributes. Testdriven. Net supports most nunit attributes, but some attributes are not supported yet.
Before using testdriven. Net for testing, the project must referenceProgramSet, that is, nunit. framework. DLL, and each source file containing the test must use the using statement to reference the Assembly, as shown in the following code: Using nunit. framework; In nunit, all attributes are included in nunit. framework namespace.
First, familiarize yourself with these attributes.
1. testfixtureattribute
This property is used to modify the test class, indicating that this class contains the test method. Note that there are some restrictions on using this attribute to modify the class: This class must be public and there must be a default constructor.
UsingSystem;UsingNunit. Framework;NamespaceTestdrivennet {[Testfixture]Public classYjingleefixture{//......}}
2. testattribute
This property marks a method of the class as a test method, which has been marked as a testfixture. The signature of a test method is defined as follows:
[Test]Public voidTestmethod (){}
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 attribute is used to modify the method. After modification, this method runs before each test method is called. We can use it to reset some variables and assign values before each method is run.
[Setup]Public voidInit (){}
4. teardownattribute
This attribute is used to modify the method. It indicates that this method runs after each test method is called. We can release some temporary variables.
[Teardown]Public voidDispose (){}
5. setupfixtureattribute
This attribute is used to modify the class. This class contains the setupattribute or teardownattribute attribute, which must be public and a default constructor. If this attribute is used, run the test in its namespace. First, run the setupattribute modifier method. After the test is completed, run the teardownattribute modifier method. Note that there is only one setupfixtureattribute in a namespace. If this attribute is defined in the entire assembly, it is valid in the entire assembly. We often use it to set global conditions.
[Setupfixture]Public classMysetupclass{[Setup]Public voidRunbeforeanytests (){}[Teardown]Public voidRunafteranytests (){}}
6. testfixturesetupattribute
This attribute is used to modify the method. After modification, this method runs before any test execution of fixture. We often use it to initialize some objects, such as constructors in the class.
[Testfixturesetup]Public voidFixtureinit (){}
7. testfixtureteardownattribute
This attribute is used to modify the method. After modification, this method runs after any fixture test execution. We often use this attribute to release some resources.
[Testfixtureteardown]Public voidFixturedispose (){}
8. expectedexceptionattribute
This attribute indicates that this method throws an expected exception. This method is used to indicate the exceptions thrown during the test execution. Type, which is the exact type of the expected exception. The second is a string of the expected full name of the exception. Either way, if a specified exception is thrown during the test, the test passes. If a different exception is thrown, the test fails. If an exception inherited from the expected exception is thrown, it is successful.
[Test] [Expectedexception(Typeof(Invalidoperationexception)]Public voidExpectanexceptionbytype (){}[Test] [Expectedexception("System. invalidoperationexception")]Public voidExpectanexceptionbyname (){}
9. platformattribute
Platform properties are used to specify a testing method or a platform for testing fixture. The Platform selection includes various operating systems and. NET Framework versions. Use a string without case sensitivity to specify the platform, or use the include or exclude attribute to include or exclude running platforms. You can also specify the platformattribute parameter. In either case, multiple commas can be used to separate strings.
Testfixture syntax
[Testfixture] [Platform(". Net-2.0")]Public classYjingleefixture{}
Test syntax
[Test] [Platform(Exclude ="WINXP")]Public voidSometest (){}
Platform value: Win series, UNIX, Linux, net, net-1.0, net-1.1, net-2.0, netcf, etc. They can be platform-specific 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 attribute can be used to specify certain test methods or test fixture as a specific category. When using a category, you can test only the selected category. A class test that is not selected does not run. For example, some tests take a long time and we certainly do not want to run them every time. You can categorize these tests and exclude them from the test scope in the nunit GUI. Note that this attribute is not supported in testdriven. net.
Testfixture syntax
[Testfixture] [Category("Longrunning")]Public classYjingleefixture{}
Test syntax
[Test] [Category("Verylong")]Public voidVerylongtest (){}
11. explicitattribute
This property ignores a test method or test fixture until it is explicitly selected to run. If you specify it (for example, place the mouse over this method and select runtest), the test method will run. We often use testing methods that are temporarily avoided.
Testfixture syntax
[Testfixture,Explicit]Public classYjingleefixture{}
Test syntax
[Test,Explicit]Public voidExplicittest (){}
12. suiteattribute
The suite attribute is used to define a set based on user preferences. This is not commonly used during testing because the framework provides a dynamic creation mechanism.
13. ignoreattribute
This property indicates that the test method or fixture will be ignored. This method or the test fixture will not be run for a period of time. We can mark the test method or fixture as the ignore attribute, and it will not be executed during the test. For example, we often use this attribute to mark the tests that need to be retained when testing is not currently running or refactoring the software, instead of using the annotation or rename method.CodeIt will be compiled with the Code marked with this mark, and the testing Code marked with this mark will not be run at runtime, so that you will not forget the tests in the past.
Testfixture syntax
[Testfixture] [Ignore("Ignore a fixture")]Public classYjingleefixture{}
Test syntax
[Test] [Ignore("Ignore a test")]Public voidIgnoredtest (){}
In testdriven. net, if this attribute is used, the test result is as follows:
Now, we have introduced so much about the attributes of the nunit testing framework. The testdriven. Net Testing Tool supports most of the attributes here, and we can use this tool to complete our testing. In the next article, I will continue to introduce you to the basic syntax of assertions. Next I will take an example to test the skills.
Preface
This short series provides a one-to-one explanation of the test knowledge under. net, hoping to help beginners.
In the previous article, I introduced several test tools and recommended testdriven. net. At the end of this article, the official version testdriven. Net-2.14.2190 beta (direct download) and testdriven. Net-2.13.2184 official version (direct download) are provided ). This is the second article in this series. I will use this tool to introduce some important attributes supported by testdriven. net.
Attribute
Testdriven. net supports a variety of unit test frameworks, such as nunit, mbunit, MS team system, here I chose the most classic nunit unit test framework to introduce testdriven. net supports some important attributes. Testdriven. Net supports most nunit attributes, but some attributes are not supported yet.
We use testdriven. net, the project must reference the Framework Assembly, that is, nunit. framework. DLL, and each source file containing the test must use the using statement to reference the Assembly, as shown in the following code: Using nunit. framework; In nunit, all attributes are included in nunit. framework namespace.
First, familiarize yourself with these attributes.
1. testfixtureattribute
This property is used to modify the test class, indicating that this class contains the test method. Note that there are some restrictions on using this attribute to modify the class: This class must be public and there must be a default constructor.
UsingSystem;UsingNunit. Framework;NamespaceTestdrivennet {[Testfixture]Public classYjingleefixture{//......}}
2. testattribute
This property marks a method of the class as a test method, which has been marked as a testfixture. The signature of a test method is defined as follows:
[Test]Public voidTestmethod (){}
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 attribute is used to modify the method. After modification, this method runs before each test method is called. We can use it to reset some variables and assign values before each method is run.
[Setup]Public voidInit (){}
4. teardownattribute
This attribute is used to modify the method. It indicates that this method runs after each test method is called. We can release some temporary variables.
[Teardown]Public voidDispose (){}
5. setupfixtureattribute
This attribute is used to modify the class. This class contains the setupattribute or teardownattribute attribute, which must be public and a default constructor. If this attribute is used, run the test in its namespace. First, run the setupattribute modifier method. After the test is completed, run the teardownattribute modifier method. Note that there is only one setupfixtureattribute in a namespace. If this attribute is defined in the entire assembly, it is valid in the entire assembly. We often use it to set global conditions.
[Setupfixture]Public classMysetupclass{[Setup]Public voidRunbeforeanytests (){}[Teardown]Public voidRunafteranytests (){}}
6. testfixturesetupattribute
This attribute is used to modify the method. After modification, this method runs before any test execution of fixture. We often use it to initialize some objects, such as constructors in the class.
[Testfixturesetup]Public voidFixtureinit (){}
7. testfixtureteardownattribute
This attribute is used to modify the method. After modification, this method runs after any fixture test execution. We often use this attribute to release some resources.
[Testfixtureteardown]Public voidFixturedispose (){}
8. expectedexceptionattribute
This attribute indicates that this method throws an expected exception. This method is used to indicate the exceptions thrown during the test execution. Type, which is the exact type of the expected exception. The second is a string of the expected full name of the exception. Either way, if a specified exception is thrown during the test, the test passes. If a different exception is thrown, the test fails. If an exception inherited from the expected exception is thrown, it is successful.
[Test] [Expectedexception(Typeof(Invalidoperationexception)]Public voidExpectanexceptionbytype (){}[Test] [Expectedexception("System. invalidoperationexception")]Public voidExpectanexceptionbyname (){}
9. platformattribute
Platform properties are used to specify a testing method or a platform for testing fixture. The Platform selection includes various operating systems and. NET Framework versions. Use a string without case sensitivity to specify the platform, or use the include or exclude attribute to include or exclude running platforms. You can also specify the platformattribute parameter. In either case, multiple commas can be used to separate strings.
Testfixture syntax
[Testfixture] [Platform(". Net-2.0")]Public classYjingleefixture{}
Test syntax
[Test] [Platform(Exclude ="WINXP")]Public voidSometest (){}
Platform value: Win series, UNIX, Linux, net, net-1.0, net-1.1, net-2.0, netcf, etc. They can be platform-specific 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 attribute can be used to specify certain test methods or test fixture as a specific category. When using a category, you can test only the selected category. A class test that is not selected does not run. For example, some tests take a long time and we certainly do not want to run them every time. You can categorize these tests and exclude them from the test scope in the nunit GUI. Note that this attribute is not supported in testdriven. net.
Testfixture syntax
[Testfixture] [Category("Longrunning")]Public classYjingleefixture{}
Test syntax
[Test] [Category("Verylong")]Public voidVerylongtest (){}
11. explicitattribute
This property ignores a test method or test fixture until it is explicitly selected to run. If you specify it (for example, place the mouse over this method and select runtest), the test method will run. We often use testing methods that are temporarily avoided.
Testfixture syntax
[Testfixture,Explicit]Public classYjingleefixture{}
Test syntax
[Test,Explicit]Public voidExplicittest (){}
12. suiteattribute
The suite attribute is used to define a set based on user preferences. This is not commonly used during testing because the framework provides a dynamic creation mechanism.
13. ignoreattribute
This property indicates that the test method or fixture will be ignored. This method or the test fixture will not be run for a period of time. We can mark the test method or fixture as the ignore attribute, and it will not be executed during the test. For example, we often use this attribute to mark the tests that need to be retained when we do not run the test temporarily or refactor the software, instead of using the annotation or rename method, the test code will be compiled with the labeled code, and the labeled test code will not be run at runtime, so as to ensure that the previous test will not be forgotten.
Testfixture syntax
[Testfixture] [Ignore("Ignore a fixture")]Public classYjingleefixture{}
Test syntax
[Test] [Ignore("Ignore a test")]Public voidIgnoredtest (){}
In testdriven. net, if this attribute is used, the test result is as follows:
Now, we have introduced so much about the attributes of the nunit testing framework. The testdriven. Net Testing Tool supports most of the attributes here, and we can use this tool to complete our testing. In the next article, I will continue to introduce you to the basic syntax of assertions. Next I will take an example to test the skills.