Detailed explanation of Assert in unit test - xUnit

Posted by sysop on Fri, 25 Feb 2022 13:18:44 +0100

Previous: Detailed unit test - xUnit

brief introduction

Assert evaluates the test results based on the return value of the code, the final state of the object, whether the event occurs, etc. The result of assert may be Pass or Fail. If all assets Pass, the whole test passes. If any assets fails, the result fails.

How many assets should there be in a Test? A simple way is that there is only one assert in each Test method. Another suggestion is that there can be multiple assets in each Test, as long as these assets are aimed at the same behavior.

type

Assert has improved many API s, which can operate on different types: bool, string, int, object, collection, etc

Demonstration example: build one first net core or standard class library project, and then establish an xunit test project to create a Programmer class

Bool

Assert.True,Assert.False is used to judge whether the expected result is true or false. First, add an attribute to the programmer, whether it is PM at the same time: public bool IsPm {get; set;}, Then create the unit test of IsPm

    public class ProgrammerTests
    {
        [Fact]
        public void IsPm()
        {
            // Arrange
            var programmer = new Programmer();

            // Act
            var result = programmer.IsPM;

            // Assert
            Assert.True(result);
        }
    }

Assert.True code passes the unit test when the result is true. Conversely, assert False is also true. Then run the unit test, you can see that it failed, and prompt us that the expected result is true and the time result is false
Then, in the unit test, assign true to the IsPM attribute of Programmer, and then run the unit test. You can see that it is passed

character string

string type provides relatively many APIs, such as assert Equal - equal to, assert Startswith - started, assert Endswith - end with, assert Contains - contains, assert Notequal - unequal, assert Matches - regular matches, etc

[Fact]
public void FullName()
{
    var programmer = new Programmer();
    programmer.FirstName = "Nick";
    programmer.LastName = "Carter";
    var fullName = programmer.FullName;
    Assert.Equal("Nick Carter", fullName); //equal
    Assert.StartsWith("Nick1", fullName);//Start with
    Assert.EndsWith("Carter", fullName);//End with
    Assert.Contains("Carter", fullName);//contain
    Assert.Contains("Car", fullName);
    Assert.NotEqual("CAR", fullName);//Unequal
    Assert.Matches(@"^[A-Z][a-z]*\s[A-Z][a-z]*", fullName);//regular expression 
}

number

Numbers include int, double and other types, and assert is provided Equal - equal, assert Inrange - range API

[Fact]
public void Age()
{
    var programmer = new Programmer();
    programmer.Age = 25;
    var age = programmer.Age;
    Assert.Equal(25, age); //Judge whether they are equal
    Assert.InRange(age, 24, 35);//Determine whether it is within a certain range
}

Null

Null can judge the value type and reference type, and provide assert Notnull - expecting non null, assert Null - null expected

[Fact]
public void Null()
{
    var programmer = new Programmer();
    programmer.FirstName = "Nick";
    Assert.NotNull(programmer); // Expectation is not empty
    Assert.Null(programmer.FirstName); // Expectation is empty
}

aggregate

Assert also supports collections. Contains expects the collection to contain an element, and DoesNotContain expects the collection to contain no element, as shown below

[Fact]
public void Languages()
{
    //Arrange
    var programmer = new Programmer();

    //Act
    programmer.Languages = new List<string>()
    {
        "GO",
        "JAVA",
        "C#"
    };

    Assert.Contains("JAVA", programmer.Languages); // Expect Java
    Assert.DoesNotContain("C#", programmer.Languages); // Expect no C#
}

object

For complex objects, Assert also supports whether the IsNotType instance is of a certain type, whether the IsAssignableFrom instance inherits a certain type, and whether the two NotSame instances are the same instance. as follows

[Fact]
public void Programmer()
{
    var p = new Programmer();
    var p2 = new Programmer();

    Assert.IsNotType<Person>(p); //Is the expected Person type
    Assert.IsType<Programmer>(p); //Expected Programmer type
    Assert.IsAssignableFrom<Person>(p); //The object p is expected to inherit from the Person type
    Assert.NotSame(p, p2); //Expect not the same instance
    Assert.Same(p, p2); //The expectation is the same instance
}

Topics: C# .NET unit testing