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

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:

主站蜘蛛池模板: 台北市| 扎兰屯市| 青海省| 凤城市| 永寿县| 仪征市| 博白县| 库车县| 于田县| 罗山县| 孝感市| 曲松县| 太保市| 法库县| 南汇区| 广宁县| 巫山县| 宁南县| 鄂温| 九江市| 江孜县| 永宁县| 周宁县| 建瓯市| 木兰县| 蕲春县| 建宁县| 关岭| 合阳县| 石台县| 二连浩特市| 东明县| 固镇县| 响水县| 子长县| 大丰市| 潼南县| 包头市| 吉林省| 水富县| 土默特左旗|