Three unit test NUnit

NUnit article describes three points, beginners can read the article, there is a direct reference to the foundation's official documents . First blog, hope everyone gets.

Navigation:
unit test a NUnit
unit test NUnit two
NUnit unit test ter

Assert assertion addition, the NUnit also provides additional methods direct assertions, such as string, directory, file and the like. Further, if needed, self expansion.

String Assert

To achieve a string type of assertion, it includes the following methods:

 public class StringAssertTest
    {
        [Test]
        public void Contains_Test()
        {
            string expected = "a";//期望值。被包含
            string actual = "acb";//真是值。包含
            StringAssert.Contains(expected, actual);
            expected = "d";
            StringAssert.DoesNotContain(expected, actual);
        }

        [Test]
        public void StartsWith_Test()
        {
            string actual = "dcc";
            string expected = "d";
            StringAssert.StartsWith(expected, actual);
            expected = "c";
            StringAssert.DoesNotStartWith(expected, actual);
        }

        [Test]
        public void EndsWith_Test()
        {
            string actual = "dcc";
            string expected = "c";
            StringAssert.EndsWith(expected, actual);
            expected = "d";
            StringAssert.DoesNotEndWith(expected, actual);
        }

        [Test]
        public void EqualIgnoringCase_Test()
        {
            string actual = "adc";
            string expected = "adc";
            StringAssert.AreEqualIgnoringCase(expected, actual);
            expected = "a";
            StringAssert.AreNotEqualIgnoringCase(expected, actual);
        }

        [Test]
        public void Match_Test()
        {
            string actual = "adc";
            string expected = "adc";
            StringAssert.IsMatch(expected, actual);
            expected = "g";
            StringAssert.DoesNotMatch(expected, actual);
        }
    }
    

Collection Assert

CollectionAssert provides for the realization of a collection of IEnumerable comparison provides a series of methods:

[Test]
        public void Equal_Test()
        {
            List<int> expected = new List<int> { 1, 2, 3 };
            List<int> actual = new List<int> { 2, 3, 1 };
            CollectionAssert.AreNotEqual(expected, actual);//集合元素位置不同,不相等
            actual = new List<int> { 1, 2, 3 };
            CollectionAssert.AreEqual(expected, actual);
        }

        [Test]
        public void ItemsOfType_Test()
        {
            List<object> cols = new List<object> { "1", "2", "b" };
            CollectionAssert.AllItemsAreInstancesOfType(cols, typeof(string));
        }

More follows:

CollectionAssert.AllItemsAreInstancesOfType(IEnumerable collection, Type expectedType);
CollectionAssert.AllItemsAreInstancesOfType(
    IEnumerable collection, Type expectedType, string message, params object[] args);

CollectionAssert.AllItemsAreNotNull(IEnumerable collection);
CollectionAssert.AllItemsAreNotNull(
    IEnumerable collection, string message, params object[] args);

CollectionAssert.AllItemsAreUnique(IEnumerable collection);
CollectionAssert.AllItemsAreUnique(
    IEnumerable collection, string message, params object[] args);

CollectionAssert.AreEqual(IEnumerable expected, IEnumerable actual);
CollectionAssert.AreEqual(
    IEnumerable expected, IEnumerable actual, string message, params object[] args);

CollectionAssert.AreEquivalent(IEnumerable expected, IEnumerable actual);
CollectionAssert.AreEquivalent(
    IEnumerable expected, IEnumerable actual, string message, params object[] args);

CollectionAssert.AreNotEqual(IEnumerable expected, IEnumerable actual);
CollectionAssert.AreNotEqual(
    IEnumerable expected, IEnumerable actual, string message, params object[] args);

CollectionAssert.AreNotEquivalent(IEnumerable expected, IEnumerable actual);
CollectionAssert.AreNotEquivalent(
    IEnumerable expected, IEnumerable actual, string message, params object[] args);

CollectionAssert.Contains(IEnumerable expected, object actual);
CollectionAssert.Contains(
    IEnumerable expected, object actual, string message, params object[] args);

CollectionAssert.DoesNotContain(IEnumerable expected, object actual);
CollectionAssert.DoesNotContain(
    IEnumerable expected, object actual, string message, params object[] args);

CollectionAssert.IsSubsetOf(IEnumerable subset, IEnumerable superset);
CollectionAssert.IsSubsetOf(
    IEnumerable subset, IEnumerable superset, string message, params object[] args);

CollectionAssert.IsNotSubsetOf(IEnumerable subset, IEnumerable superset);
CollectionAssert.IsNotSubsetOf(
    IEnumerable subset, IEnumerable superset, string message, params object[] args);

CollectionAssert.IsEmpty(IEnumerable collection);
CollectionAssert.IsEmpty(
    IEnumerable collection, string message, params object[] args);

CollectionAssert.IsNotEmpty(IEnumerable collection);
CollectionAssert.IsNotEmpty(
    IEnumerable collection, string message, params object[] args);

CollectionAssert.IsOrdered(IEnumerable collection);
CollectionAssert.IsOrdered(
    IEnumerable collection, string message, params object[] args);

CollectionAssert.IsOrdered(IEnumerable collection, IComparer comparer);
CollectionAssert.IsOrdered(IEnumerable collection,
    IComparer comparer, string message, params object[] args);

Directory Assert

Folder assertion:

        [Test]
        public void Directory_Test()
        {
            string basePath = AppDomain.CurrentDomain.BaseDirectory;
            DirectoryAssert.Exists(basePath + "/Directory"); // 判断文件夹是否存在
        }
 DirectoryAssert.AreEqual(DirectoryInfo expected, DirectoryInfo actual);
DirectoryAssert.AreEqual(DirectoryInfo expected, DirectoryInfo actual, 
    string message, params object[] args);

DirectoryAssert.AreNotEqual(DirectoryInfo expected, DirectoryInfo actual);
DirectoryAssert.AreNotEqual(DirectoryInfo expected, DirectoryInfo actual, 
    string message, params object[] args);

DirectoryAssert.Exists(DirectoryInfo actual);
DirectoryAssert.Exists(DirectoryInfo actual, 
    string message, params object[] args);

DirectoryAssert.Exists(string actual);
DirectoryAssert.Exists(string actual, 
    string message, params object[] args);

DirectoryAssert.DoesNotExist(DirectoryInfo actual);
DirectoryAssert.DoesNotExist(DirectoryInfo actual, 
    string message, params object[] args);

DirectoryAssert.DoesNotExist(string actual);
DirectoryAssert.DoesNotExist(string actual, 
    string message, params object[] args);

File Assert

Document asserted:

        [Test]
        public void File_Test()
        {
            // 判断文件是否存在
            FileAssert.Exists(AppDomain.CurrentDomain.BaseDirectory + "/Directory/file.txt");
        }
FileAssert.AreEqual(Stream expected, Stream actual); //判断两个文件流是否相同
FileAssert.AreEqual(
    Stream expected, Stream actual, string message, params object[] args);

FileAssert.AreEqual(FileInfo expected, FileInfo actual); // 判断两个文件是否同一路径的同一文件
FileAssert.AreEqual(
    FileInfo expected, FileInfo actual, string message, params object[] args);

FileAssert.AreEqual(string expected, string actual);
FileAssert.AreEqual(
    string expected, string actual, string message, params object[] args);

FileAssert.AreNotEqual(Stream expected, Stream actual);
FileAssert.AreNotEqual(
    Stream expected, Stream actual, string message, params object[] args);

FileAssert.AreNotEqual(FileInfo expected, FileInfo actual);
FileAssert.AreNotEqual(
    FileInfo expected, FileInfo actual, string message, params object[] args);

FileAssert.AreNotEqual(string expected, string actual);
FileAssert.AreNotEqual(
    string expected, string actual, string message, params object[] args);

FileAssert.Exists(FileInfo actual); //判断文件是否存在
FileAssert.Exists(
    FileInfo actual, string message, params object[] args);

FileAssert.Exists(string actual);
FileAssert.Exists(
    string actual, string message, params object[] args);

FileAssert.DoesNotExist(FileInfo actual);
FileAssert.DoesNotExist(
    FileInfo actual, string message, params object[] args);

FileAssert.DoesNotExist(string actual);
FileAssert.DoesNotExist(
    string actual, string message, params object[] args);

Spread

NUnit itself provides very comprehensive functions, in general, to fully meet our project requirements. Even if can not meet the demand, NUnit also extensible, including: property, constraints, assertions expansion. Document navigation

Guess you like

Origin www.cnblogs.com/jimizhou/p/11412307.html