Nunit Test Tool
What is Nunit?
Nunit is a TDD tool, which is the same as Junit!
How to Get Nunit
Open the Visual Studio tool, search for "nunit" in the tool> Extension Manager Online Library in the menu bar (you must be able to connect to the Internet), and click Install, it is more convenient.
Nunit User Experience
Create a class library PlugInUnitTest, reference nunit. framework. dll, modify Class1.cs to TestFactory. cs, add the feature [TestFixture] to the class, and add the feature [Test] to the class.
Compile the class library.
If Visual NUnit is not displayed, use Ctl + F7 to open the interface. After selecting the instance, you can click the Run button for execution.
Nunit Basics
[TestFixture] indicates that the class contains the test code (this feature can be inherited ). This class must be public and have a default constructor.
[Test] indicates that it is a Test method. The return value of the test method must be void and cannot contain parameters.
[SetUp] attribute: used to identify a method and run it before all tests are started. It is used to initialize some resources, such as initialization classes, before a test.
[TearDown] attribute: used to identify a method and run it after all tests are completed to release some resources.
[Ignore] attribute: used to identify a method, indicating that this method does not need to be tested for some reason (for example, related code is not completed)
Nunit common classes and Methods
Assert (Assert ):
If the assertion fails, no method is returned and an error is reported.
If a method contains multiple assertions, all assertions after the asserted failure will not be executed. For this reason, it is best to use a try statement for each test asserted.
1. Test whether two parameters are equal
Assert.AreEqual( int expected, int actual );
Assert.AreEqual( decimal expected, decimal actual );
。。。。
2. Test whether two parameters reference the same object.
Assert.AreSame( object expected, object actual );
Assert.AreNotSame( object expected, object actual );
3. Test whether an object is contained by an array or list.
Assert.Contains( object anObject, IList collection );
Comparison assertions:
4. Test whether an object is larger than another object.
Assert.Greater( int arg1, int arg2 );
5. Test whether an object is smaller than another object.
Assert.Less( int arg1, int arg2 );
Type assertions:
Assert.IsInstanceOfType( Type expected, object actual );
Conditional test:
Assert.IsTrue( bool condition );
Assert.IsFalse( bool condition);
Assert.IsNull( object anObject );
Assert.IsNotNull( object anObject );
Assert.IsNaN( double aDouble );
Assert.IsEmpty( string aString );
Assert.IsNotEmpty( string aString );
Assert.IsEmpty( ICollection collection );
Assert.IsNotEmpty( ICollection collection );
String assert: provides many useful methods for verifying string values.
StringAssert.Contains( string expected, string actual );
StringAssert.StartsWith( string expected, string actual );
StringAssert.EndsWith( string expected, string actual );
StringAssert.AreEqualIgnoringCase( string expected, string actual );