Dotnet Core 简明教程

.NET Core - Create a Testing Project

在本章中,我们将讨论如何使用 .NET Core 创建测试项目。单元测试是一个软件开发过程,其中包括应用程序中最小的可测试部分,即单元。它们会被单独独立地进行检查,以了解操作是否正确。单元测试可以自动化,也可以手动进行。

In this chapter, we will discuss how to create a Testing project using .NET Core. Unit testing is a development process for the software that has the smallest testable parts of an application, which are called units. They are individually and independently scrutinized for any proper operation. Unit testing is can either be automated or done manually as well.

现在打开“新建项目”对话框并选择 Visual C# → .NET Core 模板。

Let us now open the New Project dialog box and select Visual C# → .NET Core template.

visual csharp

在此对话框中,您可以看到没有用于单元测试的项目模板。为了创建单元测试项目,我们应该使用命令行实用工具。我们进入刚才创建的解决方案文件夹;创建一个测试文件夹并在测试文件夹内创建一个新文件夹并将其命名为 StringLibraryTests

On this dialog box, you can see that there is no project template for unit testing. To create a unit test project, we should use the command line utility. Let us go to the Solution folder that we created; create a test folder and inside the test folder create another folder and call it StringLibraryTests.

stringlibrarytests

现在,让我们使用 dotnet 命令行实用工具执行以下命令来新建一个测试项目:

Let us now use the dotnet commandline utility to create a new test project by executing the following command −

dotnet new -t xunittest

您现在可以看见创建了一个新的 C# 项目;让我们执行 v 命令查看此文件夹,您将看到 project.jsonTests.cs 文件,如下图所示。

You can now see that a new C# project is created; let us look into the folder by executing the v command and you will see project.json and Tests.cs files as shown below.

dir command

以下是 project.json 文件中的代码。

Here is the code in project.json file.

{
   "version": "1.0.0-*",
   "buildOptions": {
      "debugType": "portable"
   },
   "dependencies": {
      "System.Runtime.Serialization.Primitives": "4.1.1",
      "xunit": "2.1.0",
      "dotnet-test-xunit": "1.0.0-rc2-192208-24"
   },
   "testRunner": "xunit",
   "frameworks": {
      "netcoreapp1.0": {
         "dependencies": {
            "Microsoft.NETCore.App": {
               "type": "platform",
               "version": "1.0.1"
            }
         },
         "imports": [
            "dotnet5.4",
            "portable-net451+win8"
         ]
      }
   }
}

以下是 Test.cs 文件中的代码。

Following is the code in the Test.cs file.

using System;
using Xunit;
namespace Tests {
   public class Tests {
      [Fact]
      public void Test1() {
         Assert.True(true);
      }
   }
}

要从 NuGet 获取必需的依赖项,让我们执行以下命令:

To fetch the necessary dependencies from NuGet, let us execute the following command −

dotnet restore

当还原必需的依赖项后,我们就可以运行该测试。

We can run the test when the necessary dependencies are restored.

restored

您可看到编译成功;向下看可以看到有关执行的测试的一些信息。

You can see that the compilation succeeded; as you go down you can see some information about the test executed.

test executed

当前,我们执行了 1 个测试,0 个错误,0 个失败,0 个跳过,执行过程耗费的时间也列为信息。

Currently we have 1 test executed, 0 error, 0 failed, 0 skipped and the time taken by the execution process also mentioned as information.