.net continuous integration testing chapter of Nunit file assertion, and a collection of string assertion assertion

Series catalog

Recall that the use of the method is basically able to complete most of the tasks in the job, but some functions to achieve it is quite troublesome, for example, 字符串相等性比较不区分大小写,字符串是否匹配某一正则规则,集合中的每一个(某一个)元素是否符合特定规则and so on, Nunit provides some specific methods used to implement some of the more difficult to achieve with ordinary assertion of extension types and methods.

Here are some StringAssert type the following method

StringAssert.AreEqualIgnoringCase(string expected,string actual)

This method is used if the two strings are equal to assert case-insensitive case, it is necessary to provide two parameters, the first one is the expected result, the second is the actual results. This method is relatively simple, sample no longer available.

Note that there are many ways Nunit need to provide two parameters, under normal circumstances are the first results are expected, and the second is the actual result

StringAssert.Contains

For asserting whether a string contains another character string, wherein the first parameter is a string is included, the second is the actual string, the semantics is not particularly clear, note

        [Test]
        public void DemoTest()
        {
            StringAssert.Contains("hello", "hello,world");
        }

This code fragment returns success

StringAssert.StartsWith

Whether asserted string starts with a (several) character

       [Test]
        public void DemoTest()
        {
            StringAssert.StartsWith("h", "hello,world");
        }

StringAssert.EndsWith

And StringAssert.StartsWith similar, string class has this method, I believe we are more familiar

StringAssert.IsMatch

Whether the assertion of a specified string matches the regular rules

[warning] This method is not sensitive switch

Here are two of 文件(目录)the assertion class FileAssert and DirectoryAssert related to two classes following assertion method is basically the same, but the assertion is a file, a directory is asserted. Below an example FileAssert

FileAssert.AreEqual

This method is used to assert whether two files are the same file, accepts a string argument

It should be noted, string path must point to a file, not a directory or file does not exist.

FileAssert.Exists

Whether a file exists for the assertion, acceptance 字符串参数or FileInfoas a parameter

Similarly DirectoryAssert method, and these methods are of opposite directions corresponding to assertion, the method comprising Not name, be readily appreciated

Unit testing process, we often have to deal with the collection, Nunit in providing a CollectionAssert classes for working with the assertion of collection types

Here's what the class the following assertions

CollectionAssert.AllItemsAreInstancesOfType

A set of assertions whether there are elements of a particular type.

        [Test]
        public void DemoTest()
        {
            ArrayList ar = new ArrayList {"a", "b", "c"};
            CollectionAssert.AllItemsAreInstancesOfType(ar, typeof(string));
        }

Above assertion will return success, because we added to the ArrayList was full of elements of type string

It should be noted that many assertions are looking forward to the first argument type, the second is the actual type, and this method is the opposite, the first is the actual collection, the second is expected type

This type of support parent class, for example, if typeof(object)it can, in terms of semantics, the element is a string type, the object type is, put through the

CollectionAssert.AllItemsAreNotNull

For asserting the collection of all the elements is not Null, namely collection does not contain null elements, this method has only one parameter, we have to judge the incoming collection to

CollectionAssert.AllItemsAreUnique

For asserting a collection of elements inside we are all unique, that there are no duplicate set of elements.

[warning] It should be noted that this method does not provide support for a heavy-duty custom comparator, it can only be used to assert the simple types, such as string, int, datetime etc if only, if you need to override a class type equalsand gethashcodebut we do not usually do this, but to provide a comparator outside the class, if only to determine whether the complex type, we introduce later

Here is an example to illustrate this problem

      [Test]
        public void DemoTest()
        {
            Person[] psn = new Person[] {
                new Person { Name="sto",Age=32,BirthDay=DateTime.Today.AddYears(-10)},
                new Person { Name="sto",Age=32,BirthDay=DateTime.Today.AddYears(-10)}};
            CollectionAssert.AllItemsAreUnique(psn);
        }

The above code will pass the test, because the two new out when Persn not the same object, but the actual business, the properties of two objects exactly equal we think they are equal, more than we expected result is not by

CollectionAssert.AreEqual

For asserting two sets are equal, if two sets, the elements are simple objects, if the object is complicated by this method is not very convenient (although this method provides IComparer provided as an argument, but the collection is not very convenient IComparer )

[warning] two set here equal to a first number of the sets of elements must be equal (needless to say), a second set of elements顺序必须一致的

Another point to be noted that there is to be noted, as stated above, a consistent set of two order elements, and is consistent with runtime type will be considered equal, the two sets of 类型不必一样, for example, is a Array, one is the list,泛型参数也不必须一样

As long as the run-time type is identical to

Consider the following piece of code

        [Test]
        public void DemoTest()
        {
            int[] a = {3, 4, 5};
            List<object> b =new List<object> {3, 4, 5};
            CollectionAssert.AreEqual(a, b);
        }

a and b are two sets of different collection types, generic types are not the same, but the running time is the same, so the above code execution returns success status

CollectionAssert.AreEquivalent

This method is used to determine whether two sets of equivalent elements, if the same type of two sets of elements, the number of same is deemed equivalent, compared to the above method, it is not关心顺序

        [Test]
        public void DemoTest()
        {
            int[] a = {3, 4, 5};
            List<object> b =new List<object> {4,3, 5};
            CollectionAssert.AreEquivalent(a, b);
        }

Through the above code tests

CollectionAssert.Contains

For asserting whether the collection contains an element

Incidentally To determined whether the collection contains an element, have the same particular type as long as the specified element and element set running, and the same value, i.e., that set of elements comprising the

Consider the following code snippet


        [Test]
        public void DemoTest()
        {
            int[] a = {3, 4, 5};
            object element = 4;
            List<object> b =new List<object> {element,3, 5};
            CollectionAssert.Contains(b, 4);
        }

B set in the above code segment which does not contain elements of type int value of 4, only the element 4 is of type object, but it is true runtime type int, so the above tests are passed.

CollectionAssert.IsEmpty

For asserting a set, an empty set, i.e. the number of elements is 0

CollectionAssert.IsOrdered

For asserting element is set by the positive sequence arrangement, a so-called positive sequence, it refers to a table by Arabic numerals or characters arranged in order of normal order.

CollectionAssert.IsSubsetOf

For determining whether a set of the subset in another set, is the same as above, these two sets need not be the same type of set (may be a Array, is a List), a long element completely contained in another set of collection, i.e., that it is a subset of another set

As long as a collection element completely contained in the set to another, not necessarily the same order

CollectionAssert.IsSupersetOf

For asserting a set is a superset of whether another set of the subset as determined usage.

Most of the above methods have a method of determining the contrary, the name of a multi-Not, not listed here

Guess you like

Origin www.cnblogs.com/tylerzhou/p/11300896.html