< Unit Testing < xUnit.net

Create a C# Program

Create the following C# program. Save it as Multiply.cs

public class Multiply
{
    public double MultiplyValues(double x, double y)
    {
        return x * y;
    }        
}

Create a Test Program

Create the following C# test program. Save it as MultiplyTest.cs

using Xunit;

public class UnitTest
{
    [Fact]
    public void Multiply2x2Test()
    {
        var multiply = new Multiply();
        var result = multiply.MultiplyValues(2, 2);
        Assert.Equal(4, result);
    }
}

Test Success

Test the program by running the following command in a terminal or command prompt in the same folder as the project above and observe the results.

dotnet test

Test Failure

Change the multiply source code somehow, such as multiplying by 0 rather than multiplying by y. Test the program by running the test again and observe the results.

This article is issued from Wikiversity. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.