C# 6 and .NET Core 1.0:Modern Cross:Platform Development
上QQ阅读APP看书,第一时间看更新

Unit testing an application

Microsoft has a proprietary unit testing framework known as MS Test, which is closely integrated with Visual Studio. However, to use a unit testing framework that is compatible with .NET Core, we will use the third-party framework xUnit.net.

Creating a unit of code that needs testing

Add a new Class Library project named Ch05_Calculator. In the Solution Explorer window, right-click on the Class1.cs file and choose Rename. Change its name to Calculator.

Modify the code to look like this:

namespace Ch05_Calculator
{
    public class Calculator
    {
        public double Add(double a, double b)
        {
            return a * b;
        }
    }
}

Creating a unit test project

Add a new Class Library project named Ch05_CalculatorUnitTests. In the Solution Explorer, right-click on References and choose Manage NuGet Packages.

In the NuGet Package Manager window, click on the Browse tab, and then search for xunit. Click on Install for the latest stable version:

In the Solution Explorer, right-click on References and choose Add Reference…. In the Reference Manager window, select the checkbox for Ch05_Calculator and then click on OK. In the Solution Explorer window, right-click on the Class1.cs file and choose Rename. Change its name to CalculatorUnitTests.

Modify the code to look like this:

using Ch05_Calculator;
using Xunit;

namespace Ch05_CalculatorUnitTests
{
    public class CalculatorUnitTests
    {
        [Fact]
        public void TestAdding2And2()
        {
            // arrange
            double a = 2;
            double b = 2;
            double expected = 4;
            var calc = new Calculator();
            // act
            double actual = calc.Add(a, b);
            // assert
            Assert.Equal(expected, actual);
        }
        [Fact]
        public void TestAdding2And3()
        {
            // arrange
            double a = 2;
            double b = 3;
            double expected = 5;
            var calc = new Calculator();
            // act
            double actual = calc.Add(a, b);
            // assert
            Assert.Equal(expected, actual);
        }
    }
}

A well-written unit test will have three parts:

  • Arrange: This part will declare and instantiate variables for input and output
  • Act: This part will execute the unit that you are testing
  • Assert: This part will make one or more assertions about the output

Running unit tests

You must install a runner to execute your tests. There is a runner for Visual Studio, but we will use the one that executes in a console application because it is cross-platform.

In the Solution Explorer window, right-click on References and choose Manage NuGet Packages. In the NuGet Package Manager, click on the Browse tab, and then search for xunit.runner.console. Click on Install for the latest stable version.

Open a Command Prompt and navigate to C:\Code\Chapter05\. Enter the following command at the prompt to run your tests:

packages\xunit.runner.console.2.1.0\tools\xunit.console Ch05_CalculatorUnitTests\bin\Debug\Ch05_CalculatorUnitTests.dll

You should see the following results:

Fix the bug in the Add method, rebuild the unit test project, and then rerun the unit tests at the Command Prompt. You should see the following results: