Unit Testing/xUnit.net/Create a Simple Test

From Wikiversity
Jump to navigation Jump to search

Create a C# Program[edit | edit source]

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[edit | edit source]

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[edit | edit source]

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[edit | edit source]

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.