Test framework nunit's assertion assertions use detail

  • 2020-06-07 05:12:39
  • OfStack

Any xUnit tool USES assertions for conditional judgment, and NUnit is no exception. Compared with other xUnit (such as Junit, phpUnit, pythonUnit), NUnit provides more aspects and flexible testing methods due to its extensive use of language features such as Generic and Attribute. The following assertion is introduced first.

Nunit1 a total of four assertions, Assert, StringAssert FileAssert and DirectoryAssert. They are all in NUnit Framework namespace, including Assert is commonly used, is also our most familiar, while the other three assertion class, as the name implies, that correspond to the string of assertion, file and directory of assertion, in theory, only Assert class can complete all condition judgment, however, if the three assertions, after the reasonable use will make the code more concise, beautiful, more easy to understand and maintain.

1: Assert class

For our usual unit test code, the static method in the Assert class is done. The most familiar method is the Assert.AreEqual () method.

1: Equal/unequal judgment

AreEqual: Determine if two values are equal

AreNotEqual: Determine if two values are not equal

Example:


Int a=1
Assert.AreEqual(a,1);// By judging 
Assert.AreEqual(a,2);// Can't pass judgment 

Note that in addition to AreEqual/ AreNotEqual, almost all of the following methods support polymorphism (multiple data types), as well as multiple parameters, and in addition to the frequently used ES47en.AreEqual (int a,int b), there is a typical parameter form:

Assert.AreEqual(int a,int b,string message);
Or Assert.XXX(int a,int b,string message);
The third parameter is the output information of the condition, so as to facilitate the wrong location

For types double and decimal, the two methods also support floating point errors when comparing

For example, the following code:


double a = 1.1;
double b = 1.11;
double c = 0.1;
Assert.AreEqual(a, b, c);// By judging 
c=0.01;
Assert.AreEqual(a, b, c);// Can't pass judgment 

2: Class judgment

AreSame: Determine that two objects are equal

AreNotSame: Determine if two objects are not equal

Contains: Determine whether or not an object

Example:


List<string> list = new List<string>();
list.Add("a");
list.Add("b");
Assert.Contains("a", list); // By judging 
Assert.Contains("aa", list); // Can't pass judgment 


3: Conditional judgment

IsTrue: Determine if the condition is true

True: same as above

IsFalse: Determine if the condition is false

False: same as above

IsNull: Determine if it is empty

Null: same as above

IsNotNull: Judgment is not space

NotNull: same as above

IsNaN: The value is Nan

IsEmpty/IsNotEmpty: Determines whether the string is null/not

IsEmpty/IsNotEmpty: Determines whether the set is empty/not

Example:


int a=100;
Assert. IsTrue(a==100);// By judging 
Assert. True(a==100);// By judging 
 

4: Comparative judgment

Greater: greater than

GreaterOrEqual: Greater than or equal

Less: less than

LessOrEqual: Less than or greater than

Example:


int a = 100;
Assert.Greater(a, 99);
Assert.GreaterOrEqual(a, 100);

5: Type judgment

IsInstanceOfType/ IsNotInstanceOfType: Is/is not an instance of a class (generics are supported from 2.5 onwards)

Such as:


public class Person
{
    public string name {set;get;}
}
Person p = new Person();
p.name = "a";
Assert.IsInstanceOf(typeof(Person), p);// By judging 
Assert.IsInstanceOf(typeof(System.String), p);// Can't pass judgment 

6: Abnormal judgment

Throws: Some type of exception should be thrown

DoesNotThrow: A certain type of exception should not be thrown

7: Other common methods

Pass: Force the test through

Fail: Force the test to fail

Ignore: Ignore this test method

Inconclusive: This test was not validated

2: StringAssert class

StringAssert is used for assertion judgments of type String:

StringAssert.Contains: Whether contains substrings or not

StringAssert.StartsWith: Whether to start with a substring

StringAssert.EndsWith: Whether it ends with a substring

StringAssert. AreEqualIgnoringCase: Whether two strings are equal when case insensitive

StringAssert. IsMatch: Match or not, (using regular expressions for string comparisons)

Example:


string s1 = "abc";
StringAssert.Contains("b", s1);
StringAssert.StartsWith("a", s1);
StringAssert.EndsWith("c", s1);
string s2 = "aBc";
StringAssert.AreEqualIgnoringCase(s1, s2);
StringAssert.IsMatch("[a|book]", "123");


3: CollectionAssert class

AllItemsAreInstancesOfType: Whether an item in a collection is an instance of type x

AllItemsAreNotNull: The items in the set are not empty

AllItemsAreUnique: each item in the collection is only 1

AreEqual: Two sets are equal

AreEquivalent: The two sets are equivalent

AreNotEqual: Two sets are not equal

AreNotEquivalent: The two sets are not equivalent

DoesNotContain: The collection contains no objects

IsSubsetOf: One set is a subset of the other set

IsNotSubsetOf:1 set is not a subset of another set

IsEmpty: Set is empty

IsNotEmpty: The set is not empty

IsOrdered: The items of the collection have been sorted

Example:


List<int> a = new List<int>();
List<int> b = new List<int>();
CollectionAssert.IsEmpty(a);
for (int i = 1; i <= 10; i++)
{
    a.Add(i);
    b.Add(i);
}
CollectionAssert.AreEqual(a, b);
CollectionAssert.IsOrdered(a);
b.Remove(1);
CollectionAssert.IsSubsetOf(b, a);
CollectionAssert.AreEqual(a, b);


Related articles: