NUnit basic usage, NUnit usage
Usually, the Unit test framework is named with the start letter and Unit of the language they support. They are collectively referred to as the xUnit framework. C ++ is called CppUnit, Java is called JUnit, And. Net is called NUnit. Of course not all of them are named like this, but most of them are. The following describes some basic NUni operations.
1: two important attributes of Nunit
1.1: [TestFixture]
This is a class that identifies automated testing. It may be changed to TestClass for better understanding. However, the Code cannot be compiled.
1.2: [Test]
This indicates that this method requires automated testing. Remember to add this attribute to the method to be tested.
2: write the first unit test 2.1: a unit test usually contains three actions:
2.1.1: Prepare an object (that is, operate on the object)
2.1.2: Operation object (logical processing of objects)
2.1.3: Comparison of Assert and pre-prediction results
2.2: compile a method to determine the suffix
public bool IsValidExtensions(string fileName) { if (string.IsNullOrWhiteSpace(fileName)) throw new ArgumentNullException("fileName"); var extension = Path.GetExtension(fileName); if (extension.Contains("pdf")) { return true; } return false; }
2.3: Compile the test method
2.3.1: Assert class
Assert. IsFalse (bool condition, string message)
Parameter 1: returned result parameter 2: displays the error message.
Assert. AreEqual (int expected, int actual, string message)
Parameter 1: Expected result parameter 2: actual result parameter 3: display failed Information
Of course, many methods of Assert can be learned by yourself.
[Test] public void IsValidExtensions_BadExtension_ReturnsFalse () {var arithmetic = new Arithmetic (); bool restlt = arithmetic. isValidExtensions ("logstores"); Assert. isFalse (restlt, "no correct value returned ");}
2.3.2: compile the project and open Nunit. Click the NUnit File to open the compiled dll File, as shown in figure
2.3.3: We want to modify the unit test method.
Right-click ArithmeticTest and select LoadFixture to re-import this ArithmeticTest. ClearFixTure clear this ArithmeticTest
2.4: Use parameter refactoring Test
What should I do if I want to test log.txt while calling log.htm in our workshop? If I can write two or ten or even 100, I don't have to worry that NUnit provides us with two features.
[TestCase]
OK. Let's modify the unit test code above.
[TestCase ("logstores")] [TestCase ("log.txt")] public void encode (string fileName) {var arithmetic = new Arithmetic (); bool restlt = arithmetic. isValidExtensions (fileName); Assert. isFalse (restlt, "no correct value returned ");}
We are running NUnit to see the effect:
When we see the results, we have two methods. Is this simple.
But some also say that I can specify the expected results. It is obvious that we can modify the test method here.
[TestCase ("logstores", true)] [TestCase ("log.txt", false)] public void IsValidExtensions_BadExtension_ReturnsFalse (string fileName) {var arithmetic = new Arithmetic (); bool restlt = arithmetic. isValidExtensions (fileName); Assert. isTrue (restlt, "no expected value returned ");}
We run the command and we will find that both of them can be compiled without being mapped here.
2.5: setup and teardown
The setup feature indicates that each time this test class is run, the method is equivalentConstructor
Teardown indicates that the method will be run again after it is finished,Destructor.
For example
[Setup] ()
[Test] B ()
[Teardown] C ()
The running sequence is from A to B to C.
2.6: exception detection
2.6.1: ExpectedException: This identifier is a common test exception. Let's take a look at its usage.
Public bool IsValidExtensions (string fileName) {if (string. isNullOrWhiteSpace (fileName) throw new ArgumentNullException (); var extension = Path. getExtension (fileName); if (extension. contains ("pdf") {return true;} return false;} [Test] [ExpectedException (typeof (ArgumentNullException), The ExpectedMessage = "value cannot be null. ")] Public void IsVaildFileName_EmptyFileName_ThrowException () {var arithmetic = new Arithmetic (); arithmetic. IsValidExtensions ("");}
The first parameter indicates the display of abnormal classes.
Second parameter: indicates the expected exception
Note: This exception does not call Assert because ExpectedException has its own judgment.
Let's take a look at the test results:
This indicates that the exception is the same as we expected. However, if the constructor throws an exception and the test passes, the test is not authentic. (I have tested whether an empty exception is thrown in the constructor)
Then we introduced another abnormal test method Lambda expression to rebuild it.
[Test] public void IsVaildFileName_EmptyFileName_ThrowException () {var arithmetic = new Arithmetic (); var ex = Assert. catch <Exception> () => arithmetic. isValidExtensions (string. empty); StringAssert. contains ("value cannot be null. ", Ex. Message); // You can pass the test even if the value cannot be Null}
In this way, the above situation is avoided, as long as the lambda expression throws an exception and passes the test. Otherwise, the exception thrown anywhere cannot pass the test.
2.7: Ignore Test
Sometimes some code does not need to be tested, but you need to migrate it to the main project, so you can ignore it (of course, this situation is rare)
[Test] [Ignore ("this method does not need to be tested currently")] public void IsVaildFileName_BadExtension_ReturnFalse () {var arithmetic = new Arithmetic (); Assert. isTrue (arithmetic. isValidExtensions ("11 "));}
Test results:
If it is yellow, the test method is not considered for the moment.
2.8: Set the test category
In actual development, we may perform many tests. to separate the test, we can set the category, or select some classification tests in the test. Let's see how to implement the test.
[Test] [Category ("background unit Test")] public void IsVaildFileName_BadExtension_ReturnFalse () {var arithmetic = new Arithmetic (); Assert. isTrue (arithmetic. isValidExtensions ("11");} [Test] [Category ("frontend unit Test")] public void IsVaildFileName_EmptyFileName_ThrowException () {var arithmetic = new Arithmetic (); var ex = Assert. catch <Exception> () => arithmetic. isValidExtensions (string. empty); StringAssert. contain S ("value cannot be null. ", Ex. Message );}
Let's look at the effect:
You can test it by yourself. The actual function is much more powerful than this one.
2.9: What if a property in the test class has no return value?
This is relatively simple and I directly paste the code.
public bool IsOnline { get; set; } public void IsValidExtensions(string fileName) { if (string.IsNullOrWhiteSpace(fileName)) throw new ArgumentNullException(); var extension = Path.GetExtension(fileName); if (!extension.Contains("pdf")) return; IsOnline = true; }
[Test] public void IsVaildFileName_BadExtension_ReturnFalse() { var arithmetic = new Arithmetic(); arithmetic.IsValidExtensions("log.pdf"); Assert.IsTrue(arithmetic.IsOnline); }
This effect is obvious. In this way, an attribute of the class is verified (but there is a dependency)
The art of the above reference unit test