Dotnet Core 简明教程

.NET Core - Testing Library

在本章中,我们将测试 StringLibrary,为此,我们需要重新排列我们的项目,以便按照默认惯例进行。

In this chapter, we will test our StringLibrary and to do so, we need to rearrange our projects so that we can follow the default convention.

打开 global.json 文件。

Let us open the global.json file.

{
   "projects": [ "src", "test" ],
   "sdk": {
      "version": "1.0.0-preview2-003131"
   }
}

你将在该文件的顶部看到项目设置,它默认设置了一些文件夹,例如 srctest

At the top of this file you will see the project settings and it sets up some folder such as src and test by default.

按照惯例,我们必须在这些文件夹中保存项目,这是新惯例,将用作 .NET Core 的一部分。

As by convention we must have projects in these folders, this is the new convention and that is going to be used as part of .NET Core.

在解决方案资源管理器中,你可以看到控制台项目和库项目都在 src 文件夹中,而测试项目在 test 文件夹中。

In the Solution Explorer, you can see that both the console project and the library project are inside the src folder while the Testing project is inside test folder.

src folder

解决方案资源管理器中的项目结构不代表项目在磁盘上的实际位置。打开解决方案文件夹,你会看到 StringLibrary 项目不在 src 文件夹中。

And the projects structure in Solution Explorer doesn’t represent where the projects physically exist on the disk. Let us now open the Solution folder and you will see that StringLibrary project is not inside the src folder.

stringlibrary project

你可以看到 srctest 文件夹都映射到 global.json 文件中指定的惯例。但是,我们有一个 StringLibrary 项目不符合惯例。现在将 StringLibrary 项目添加到 src 文件夹中。

You can see that both src and test folders map to the convention specified in the global.json file. However, we have one project StringLibrary which is out of convention. Let us now add the StringLibrary project inside the src folder.

在 src 文件夹中,我们有两个项目,我们需要修复问题,以便我们可以正确使用所有项目。回到 Visual Studio,右键单击 StringLibrary 项目,并选择“删除”选项。它不会删除它,只会删除项目。

In the src folder, we have two projects and we need to fix the problem so that we can use all the projects properly. Let us go back to the Visual Studio and right-click on the StringLibrary project and select the Remove option. It won’t delete it, but it will only remove the project.

remove project

现在,右键单击 src 文件夹,并选择 Add → Existing Project…

Now right-click on the src folder and select Add → Existing Project…

src

浏览到 StringLibrary 项目,该项目现位于 src 文件夹内,选择 StringLibrary.csproj 文件并单击 Open

Browse to the StringLibrary project which is now inside the src folder, select the StringLibrary.csproj file and click Open.

stringlibrary csproj

现在我们必须移除 StringLibrary 的引用,该引用来自控制台应用程序的 project.json 文件。

We now have to remove the reference of StringLibrary from the project.json file of the console app.

{
   "version": "1.0.0-*",
   "buildOptions": {
      "emitEntryPoint": true
   },
   "dependencies": {
      "Microsoft.NETCore.App": {
         "type": "platform",
         "version": "1.0.1"
      },
      "NuGet.CommandLine": "3.5.0",
      "System.Runtime.Serialization.Json": "4.0.3"
   },
   "frameworks": {
      "netcoreapp1.0": {
         "dependencies": { },
         "imports": "dnxcore50"
      }
   }
}

保存更改,然后再次在控制台项目中添加 StringLibrary 的引用。

Save the changes and then add a reference of StringLibrary again in your console project.

{
   "version": "1.0.0-*",
   "buildOptions": {
      "emitEntryPoint": true
   },
   "dependencies": {
      "Microsoft.NETCore.App": {
         "type": "platform",
         "version": "1.0.1"
      },
   "NuGet.CommandLine": "3.5.0",
      "System.Runtime.Serialization.Json": "4.0.3"
   },
   "frameworks": {
      "netcoreapp1.0": {
         "dependencies": {
            "StringLibrary": {
               "target": "project"
            }
         },
         "imports": "dnxcore50"
      }
   }
}

现在一切应该恢复正常,你可以构建 StringLibrary ,然后构建 FirstApp (控制台项目),而不会出现任何错误。现在让我们使用 xunit 来测试 StringLibrary 功能。我们需要将 StringLibrary 引用添加到我们的测试项目中。右键单击 StringLibraryTests 项目的引用,然后选择添加引用…

Now everything should be working again and you can build StringLibrary and then FirstApp (console project) without any error. Let us now test the StringLibrary functionality using xunit. We need to add reference of StringLibrary into our testing project. Right-click on the References of StringLibraryTests project and select Add Reference…

add

单击 OK ,这会向我们的测试项目添加 StringLibrary 的引用。现在让我们替换 Tests.cs 文件中的以下代码。

Click OK which will add a reference of StringLibrary to our testing project. Let us now replace the following code in the Tests.cs file.

using System;
using Xunit;
using StringLibrary;

namespace Tests {
   public class Tests {
      [Fact]
      public void StartsWithUpperCaseTest() {
         string input = "Mark";
         Assert.True(input.StartsWithUpper());
      }
      [Fact]
      public void StartsWithLowerCaseTest() {
         string input = "mark";
         Assert.True(input.StartsWithLower());
      }
      [Fact]
      public void StartsWithNumberCaseTest() {
         string input = "123";
         Assert.True(input.StartsWithNumber());
      }
   }
}

你可以看到我们有三个测试方法,它们会测试 StringLibrary 的功能。让我们单击 Run All 链接,你将在测试资源管理器中看到以下输出。

You can see that we have three test methods which will test the functionality of StringLibrary. Let us click the Run All link and you will see the following output in Test Explorer.

run all link

你还可以从命令行运行测试。让我们打开命令提示符并执行 dotnet test 命令。

You can also run the tests from the command line. Let us open the command prompt and execute the dotnet test command.

dotnet test