The usual unit test frameworks are named after the first letter of the language they support, and they are collectively referred to as the Xunit framework. C + +, called Cppunit,java, is called JUnit. NET is called NUnit. Of course not all of them are so named, but mostly so. Below I mainly explain some basic operation of Nuni.
1:nunit two important attributes
1.1:[testfixture]
This is the identification of classes that contain automated tests, which can be changed to testclass more understandable. However, the code cannot be compiled in the past.
1.2:[test]
This is to identify the method that requires automated testing. Remember to add this attribute to the method you want to test.
2: Writing the first unit Test 2.1: A unit test typically consists of three behaviors:
2.1.1: Prepare the object (which is the right thing to do)
2.1.2: Manipulating objects (some logical handling of objects)
2.1.3: Assertion (Assert) vs. pre-judgment results
2.2: Write a method to judge the suffix name
Public BOOLIsvalidextensions (stringfileName) { if(string. Isnullorwhitespace (FileName))Throw NewArgumentNullException ("FileName"); varExtension =path.getextension (fileName); if(extension. Contains ("PDF")) { return true; } return false; }
2.3: Writing test methods
2.3.1:assert This class
Assert.isfalse (bool condition, string message)
Parameter 1: returned result parameter 2: Show failed information
assert.areequal (int expected, int actual, string message)
Parameter 1: Expected result parameter 2: actual result parameter 3: Show failed information
Of course, there are many ways to learn the Assert.
[Test] publicvoid isvalidextensions_badextension_returnsfalse () { var arithmetic=New arithmetic (); bool restlt = arithmetic. Isvalidextensions ("log.pdf"); Assert.isfalse (RESTLT," not returning the correct value "); }
2.3.2: Compile the project and then open NUnit, click NUnit file to open the compiled DLL and then
2.3.3: We want to modify the unit test method
Right click on the Arithmetictest select Loadfixture will re-import this arithmetictest. Clearfixture Clean this arithmetictest.
2.4: Refactoring Test with parameters
Analogy above our parameters called Log.pdf but now also test log.txt how to do, if say 2 can write, 10 or even 100 do not worry about NUnit give us two features
[TestCase]
OK, let's change the unit test code above.
[TestCase ("log.pdf")] [TestCase ("Log.txt")] Public voidIsvalidextensions_badextension_returnsfalse (stringfileName) { varArithmetic=Newarithmetic (); BOOLRESTLT =arithmetic. Isvalidextensions (FileName); Assert.isfalse (RESTLT,"no correct value returned"); }
We are running NUnit to see the effect:
See the effect, we have two methods. This is not very simple.
But some say I can specify the effect I expected, it is obvious that we are here to modify the test method
[TestCase ("log.pdf",true)] [TestCase ("Log.txt",false)] Public voidIsvalidextensions_badextension_returnsfalse (stringfileName) { varArithmetic=Newarithmetic (); BOOLRESTLT =arithmetic. Isvalidextensions (FileName); Assert.istrue (RESTLT,"The expected value is not returned"); }
We run to find that all 2 of these can be compiled here without mapping.
2.5:setup and Teardown
The setup feature indicates that each run of the test class will first enter this method equivalent to what we often call constructors
Teardown means that the method is run at the end of the run and we often say destructors .
Such as
[Setup] A ()
[Test] B ()
[Teardown] C ()
The order of operation is a to B to C
2.6: Anomaly Detection
2.6.1:expectedexception identifying this identity is a very common way to test for exceptions.
Public BOOLIsvalidextensions (stringfileName) { if(string. Isnullorwhitespace (FileName))Throw NewArgumentNullException (); varExtension =path.getextension (fileName); if(extension. Contains ("PDF")) { return true; } return false; }[test] [ExpectedException (typeof(ArgumentNullException), expectedmessage ="The value cannot be null. ")] Public voidisvaildfilename_emptyfilename_throwexception () {varArithmetic =Newarithmetic (); Arithmetic. Isvalidextensions (""); }
First parameter: class that represents the exception being displayed
Second parameter: Represents the expected exception
Note: This exception does not call assert because ExpectedException itself is judged.
Let's look at the test results:
The exception is the same as we expected, but there is a problem if our constructor throws an exception test will also pass, so it will lead to the test is not true. (I tested the same test pass when the constructor throws an empty exception)
Then we introduce another abnormal test mode lambda expression to transform
[Test] publicvoid isvaildfilename_emptyfilename_throwexception () { varnew arithmetic (); var ex = assert.catch<exception> (() = arithmetic. Isvalidextensions (string. Empty)); Stringassert.contains (" value cannot be null.") ", ex. Message);//Can not write value cannot be null also can pass test }
This avoids the above situation, as long as the lambda expression throws an exception to pass the test, or anywhere throws an exception can not pass the test.
2.7: Ignore Test
Sometimes some code doesn't need to be tested, but you move it into the main project then you can consider ignoring it (of course, this is a rare case).
[Test ] [Ignore (" This method is not currently tested ")] public void Isvaildfilename_badextension_returnfalse () { varnew Arithmetic (); Assert.istrue (arithmetic. Isvalidextensions ("one")); }
Test results:
The yellow color means that the test is not considered for the time being.
2.8: Set Test category
In the actual development may be many of our tests, in order to separate testing we can set the category, but also in the test to select some classification test below we see how to achieve
[Test] [Category ("Background Unit Test")] Public voidIsvaildfilename_badextension_returnfalse () {varArithmetic =Newarithmetic (); Assert.istrue (arithmetic. Isvalidextensions (" One")); } [Test] [Category ("Front end Unit testing")] Public voidisvaildfilename_emptyfilename_throwexception () {varArithmetic =Newarithmetic (); varex = Assert.catch<exception> (() = = arithmetic. Isvalidextensions (string. Empty)); Stringassert.contains ("The value cannot be null. ", ex. Message); }
Let's look at the effect:
You can do your own testing, the actual function is much more powerful than this.
2.9: What to do if a property in the test class has no return value
This is relatively simple I put the code directly.
Public BOOLIsOnline {Get;Set; } Public voidIsvalidextensions (stringfileName) { if(string. Isnullorwhitespace (FileName))Throw NewArgumentNullException (); varExtension =path.getextension (fileName); if(!extension. Contains ("PDF"))return; IsOnline=true; }
[Test] Public void Isvaildfilename_badextension_returnfalse () { varnew arithmetic (); Arithmetic. Isvalidextensions ("log.pdf"); Assert.istrue (arithmetic. IsOnline); }
The effect was clearly passed. This validates a property of the class (but has a dependency)
The Art of reference unit testing above
Basic usage of NUnit