• 《Pragmatic Unit Testing in C#》Part1


    1. Classic Asserts

    Asserts are the fundamental building block for unit tests; the NUnit library provides a number of different forms of assert as static methods in the Assert class.

    AreEqual

    Assert.AreEqual(expected, actual [, string message])

    Assert.AreEqual(expected, actual, tolerance [, string message])

    Less / Greater

    Assert.Less(x, y)

    Assert.Greater(x,y)

    GreaterOrEqual / LessOrEqual

    Assert.GreaterOrEqual(x, y)

    Assert.LessOrEqual(x,y) 

    IsNull / IsNotNull

    Assert.IsNull(object [, string message])

    Assert.IsNotNull(object [, string message]) 

    AreSame

    Assert.AreSame(expected, actual [, string message])

    IsTrue

    Assert.IsTrue(bool condition [, string message])

    Assert.IsFalse(bool condition [, string message])

    Fail

    Assert.Fail([string message])

     

    2. Constraint-based Asserts

    Is.EqualTo

    Assert.That(actual, Is.EqualTo(expected))

    Assert.That(actual, new EqualConstraint(expected)) 

    Here is one called Within() that is equivalant to our same example that used the classic-style in the previous section.

    Assert.That(10.0/3.0, Is.EqualTo(3.33).Within(0.01f));

    Is.Not.EqualTo

    Assert.That(actual, Is.Not.EqualTo(expected)) 

    Assert.That(actual, new NotConstraint(new EqualConstraint(expected))); 

    Is.AtMost 

    Assert.That(actual, Is.AtMost(expected)) 

    Is.Null

    Assert.That(expected, Is.Null);

    Assert.That(expected, Is.Not.Null);

    Assert.That(expected, !Is.Null);

    Is.Empty

    Assert.That(expected, Is.Empty);

    Is.AtLeast

    Assert.That(actual, Is.AtLeast(expected));

    Is.InstanceOfType

    Assert.That(actual, Is.InstanceOfType(expected));

    Has.Length

    Assert.That(actual, Has.Length(expected));

     3.  More NUnit Asserts 

    List.Contains

    Assert.That(actualCollection, List.Contains(expectedValue))

    Assert.That({5, 3, 2}, List.Contains(2))

    Is.SubsetOf

    Assert.That(actualCollection, Is.SubsetOf(expectedCollection))

    Assert.That(new byte[] {5, 3, 2}, Is.SubsetOf(new byte[] {1, 2, 3, 4, 5}))

    Text.StartsWith

    Assert.That(actual, Text.StartsWith(expected))

    Assert.That("header:data.", Text.StartsWith("header:"))

    Assert.That("header:data.", Text.StartsWith("HeadeR").IgnoreCase)

    Text.Matches

    Assert.That(actual, Text.Matches(expected))

    Assert.That("header:data.", Text.Matches("$header^\."))

    FileAssert.AreEqual / AreNotEqual

    FileAssert.AreEqual(FileInfo expected, FileInfo actual)

    FileAssert.AreEqual(String pathToExpected, String pathToActual)

    Test whether two files are the same, byte for byte. Note that if we do the work of opening a Stream (file-based, or not), we can use the EqualsConstraint instead, like so:

    Stream expectedStream = File.OpenRead("expected.bin");

    Stream actualStream = File.OpenRead("actual.bin");

    Assert.That(actualStream, Is.EqualTo(expectedStream)); 

  • 相关阅读:
    个人作业——软件评测
    软件工程实践2019第五次作业
    18年今日头条笔试第一题题解:球迷(fans)
    游戏2.1版本
    游戏2.0版本 代码
    游戏2.0版本
    改进版游戏代码
    改进版游戏
    2017.1.13之审判日
    找朋友 的内存超限代码
  • 原文地址:https://www.cnblogs.com/samcn/p/1590624.html
Copyright © 2020-2023  润新知