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

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:

主站蜘蛛池模板: 达拉特旗| 都安| 仪陇县| 武冈市| 泰和县| 昆山市| 白沙| 崇州市| 郴州市| 乡宁县| 宝坻区| 兖州市| 农安县| 汽车| 西昌市| 娄烦县| 崇州市| 康平县| 祁阳县| 闵行区| 锦屏县| 顺平县| 栖霞市| 故城县| 台山市| 定结县| 乌苏市| 襄汾县| 浙江省| 长寿区| 清水县| 密云县| 东城区| 扶余县| 安吉县| 辰溪县| 河南省| 年辖:市辖区| 朔州市| 繁昌县| 济阳县|