单元测试入门Get started with unit testing

使用 Visual Studio 定义和运行单元测试,使代码保持正常运行、确保代码覆盖率并在客户之前找到错误和缺陷。Use Visual Studio to define and run unit tests to maintain code health, ensure code coverage, and find errors and faults before your customers do. 经常运行单元测试,确保代码正常运行。Run your unit tests frequently to make sure your code is working properly.

创建单元测试Create unit tests

本节从较高层面介绍了如何创建单元测试项目。This section describes at a high level how to create a unit test project.

  1. 在 Visual Studio 中,打开要测试的项目。Open the project that you want to test in Visual Studio.

    为了演示示例单元测试,本文测试了简单的“Hello World”项目。For the purposes of demonstrating an example unit test, this article tests a simple "Hello World" project. 此类项目的示例代码如下所示:The sample code for such a project is as follows:

    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("Hello World!");
        }
    }
    
  2. 在“解决方案资源管理器”中,选择解决方案节点 。In Solution Explorer, select the solution node. 然后,在顶部菜单栏中,选择“文件” > “添加” > “新项目” 。Then, from the top menu bar, select File > Add > New Project.

  3. 在新项目对话框中,找到并选择要使用的测试框架的单元测试项目模板。In the new project dialog box, find a unit test project template for the test framework you want to use and select it.

    Visual Studio 2019 中的单元测试项目模板

    单击“下一步”,选择测试项目的名称,然后单击“创建” 。Click Next, choose a name for the test project, and then click Create.

    Visual Studio 2019 中的单元测试项目模板

    选择测试项目的名称,然后单击“确定” 。Choose a name for the test project, and then click OK.

    项目将添加到解决方案中。The project is added to your solution.

    解决方案资源管理器中的单元测试项目

  4. 在单元测试项目中,右键单击“引用”或“依赖项”,然后选择“添加引用”,添加对要测试的项目的引用 。In the unit test project, add a reference to the project you want to test by right-clicking on References or Dependencies and then choosing Add Reference.

  5. 选择包含待测试代码的项目,单击“确定” 。Select the project that contains the code you'll test and click OK.

    在 Visual Studio 中添加项目引用

  6. 向单元测试方法添加代码。Add code to the unit test method.

    在 Visual Studio 中向单元测试方法添加代码

Tip

有关创建单元测试的更详细演练,请参阅创建并运行托管代码的单元测试For a more detailed walkthrough of creating unit tests, see Create and run unit tests for managed code.

运行单元测试Run unit tests

  1. 在顶部菜单栏中选择“测试” > “Windows” > “测试资源管理器”,打开测试资源管理器Open Test Explorer by choosing Test > Windows > Test Explorer from the top menu bar.

  2. 单击“全部运行”,运行单元测试 。Run your unit tests by clicking Run All.

    在测试资源管理器中运行单元测试

    测试完成后,绿色复选标记表示测试通过。After the tests have completed, a green check mark indicates that a test passed. 红色“x”图标表示测试失败。A red "x" icon indicates that a test failed.

    在测试资源管理器中查看单元测试结果

Tip

可以使用测试资源管理器从内置测试框架 (MSTest) 或第三方测试框架运行单元测试。You can use Test Explorer to run unit tests from the built-in test framework (MSTest) or from third-party test frameworks. 可以将测试分组为不同类别、筛选测试列表,以及创建、保存和运行测试播放列表。You can group tests into categories, filter the test list, and create, save, and run playlists of tests. 你还可以调试测试并分析测试性能和代码覆盖率。You can also debug tests and analyze test performance and code coverage.

查看实时单元测试结果View live unit test results

如果在 Visual Studio 2017 或更高版本中使用 MSTest、xUnit 或 NUnit 测试框架,可查看单元测试的实时结果。If you are using the MSTest, xUnit, or NUnit testing framework in Visual Studio 2017 or later, you can see live results of your unit tests.

Note

只有企业版中提供 Live Unit Testing 功能。Live unit testing is available in Enterprise edition only.

  1. 选择“测试” > “Live Unit Testing” > “启动”,从“测试”菜单启用 Live Unit Testing 。Turn live unit testing from the Test menu by choosing Test > Live Unit Testing > Start.

    启用 Live Unit Testing

    在 Visual Studio 2019 中启用 Live Unit Testing

  2. 编写和编辑代码时,请在代码编辑器窗口中查看测试的结果。View the results of the tests within the code editor window as you write and edit code.

    查看测试的结果

  3. 单击测试结果指示器查看详细信息,例如涵盖该方法的测试的名称。Click a test result indicator to see more information, such as the names of the tests that cover that method.

    选择测试结果指示符

有关 Live Unit Testing 的详细信息,请参阅 Live Unit TestingFor more information about live unit testing, see Live unit testing.

使用 IntelliTest 生成单元测试Generate unit tests with IntelliTest

运行 IntelliTest 时,可以看到哪些测试会失败,并可添加任何必要的代码来修复它们。When you run IntelliTest, you can see which tests are failing and add any necessary code to fix them. 你可选择要保存到测试项目中的已生成测试,以提供回归套件。You can select which of the generated tests to save into a test project to provide a regression suite. 当你更改代码时,重新运行 IntelliTest,以使生成的测试与你的代码更改同步。As you change your code, rerun IntelliTest to keep the generated tests in sync with your code changes. 若要了解如何操作,请参阅使用 IntelliTest 为你的代码生成单元测试To learn how, see Generate unit tests for your code with IntelliTest.

Tip

IntelliTest 仅适用于面向 .NET Framework 的托管代码。IntelliTest is only available for managed code that targets the .NET Framework.

使用 IntelliTest 生成单元测试

分析代码覆盖率Analyze code coverage

若要确定正在由编码的测试(例如单元测试)实际进行测试的项目代码的比例,则可以使用 Visual Studio 的代码覆盖率功能。To determine what proportion of your project's code is actually being tested by coded tests such as unit tests, you can use the code coverage feature of Visual Studio. 若要有效防止 Bug,测试应作用于你的大部分代码。To guard effectively against bugs, your tests should exercise a large proportion of your code. 若要了解如何操作,请参阅使用代码覆盖率确定所测试的代码量To learn how, see Use code coverage to determine how much code is being tested.

使用第三方测试框架Use a third-party test framework

通过使用第三方测试框架(如 Boost、Google、和 NUnit),可以在 Visual Studio 中运行单元测试。You can run unit tests in Visual Studio by using third-party test frameworks such as Boost, Google, and NUnit. 使用 NuGet 包管理器为所选框架安装 NuGet 包 。Use the NuGet Package Manager to install the NuGet package for the framework of your choice. 或者,对于 NUnit 和 xUnit 测试框架,Visual Studio 包含预配置的测试项目模板,其中包含必要的 NuGet 包。Or, for the NUnit and xUnit test frameworks, Visual Studio includes preconfigured test project templates that include the necessary NuGet packages.

创建使用 NUnit 的单元测试:To create unit tests that use NUnit:

  1. 打开包含待测试代码的解决方案。Open the solution that contains the code you want to test.

  2. 右键单击“解决方案资源管理器”中的解决方案,然后选择“添加” > “新建项目” 。Right-click on the solution in Solution Explorer and choose Add > New Project.

  3. 选择“NUnit 测试项目”项目模板 。Select the NUnit Test Project project template.

    Visual Studio 2019 中的 NUnit 测试项目模板

    单击“下一步”,为项目命名,然后单击“创建” 。Click Next, name the project, and then click Create.

    为项目命名,然后单击“确定”进行创建 。Name the project, and then click OK to create it.

    项目模板包括对 NUnit 和 NUnit3TestAdapter 的 NuGet 引用。The project template includes NuGet references to NUnit and NUnit3TestAdapter.

    解决方案资源管理器中的 NUnit NuGet 依赖项

  4. 将测试项目中的引用添加到包含待测试代码的项目中。Add a reference from the test project to the project that contains the code you want to test.

    右键单击“解决方案资源管理器”中的项目,然后选择“添加” > “引用” 。Right-click on the project in Solution Explorer, and then select Add > Reference. (还可以从“引用” 或“依赖项” 节点右键单击菜单来添加一个引用。)(You can also add a reference from the right-click menu of the References or Dependencies node.)

  5. 将代码添加到测试方法。Add code to your test method.

    将代码添加到单元测试代码文件

  6. 从测试资源管理器运行测试,或右键单击测试代码并选择“运行测试” 。Run the test from Test Explorer or by right-clicking on the test code and choosing Run Test(s).

请参阅See also