官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 乌拉特后旗| 杭锦后旗| 金坛市| 怀化市| 水富县| 丘北县| 金平| 普兰店市| 苍梧县| 平谷区| 子长县| 台前县| 石河子市| 通州市| 巴青县| 弥勒县| 富源县| 班玛县| 清河县| 扎赉特旗| 思茅市| 沁阳市| 迁西县| 昂仁县| 定安县| 绥阳县| 秭归县| 道真| 明溪县| 靖边县| 新余市| 大洼县| 嘉鱼县| 顺义区| 教育| 海门市| 罗甸县| 剑河县| 洛阳市| 将乐县| 康马县|