如何通过cmd 运行多个specflow项目?

How to run multiple specflow projects through cmd?

我有许多 .Net Framework class 库项目,所有项目都安装了 specflow 和 specrun.specflow nuget 包。

我能够 运行 在 Visual Studio 2019 年的测试资源管理器中 运行 所有这些项目,但我想知道这是否可以 运行 使用 cmd 提示符。

我计划通过 cmd 创建一个批处理文件 运行 所有项目来实现自动化,而无需在 VS 2019 中去测试资源管理器并手动 运行 它们

有人知道这是否可以实现吗?如果可能,您能否分享 运行 它们所需的命令?

编辑 1:

根据 Greg Burghardt 的建议,我做了以下操作

  1. 我去了 vstest.console.exe 所在的路径(C:\Program Files (x86)\Microsoft Visual Studio19\Professional\Common7\IDE\Extensions\TestPlatform)
  2. 从该路径打开 cmd 并且 运行 cmd vstest.console.exe mytests.dll 起初我得到一个错误说上面的 dll 没有找到,所以我将 dll 粘贴到相同的位置并再次执行相同的命令,我得到以下消息

C:\Program Files (x86)\Microsoft Visual Studio19\Professional\Common7\IDE\Extensions\TestPlatform\mytests.dll 中没有可用的测试。请确保测试发现者和执行者已注册并且平台和框架版本设置合适,然后重试。

此外,可以使用 /TestAdapterPath 命令指定测试适配器的路径。示例 /TestAdapterPath:.

编辑 2:

我没有复制 dll 并将其粘贴到 vstest.console.exe 路径位置,而是直接提供了 dll 所在的路径以及 运行 dll 中存在的所有测试 所以cmd看起来像

vstest.console.exe D:\Specflow\Dummy\bin\Debug\mytests.dll

使用 Visual Studio 附带的 vstest.console.exe 命令行实用程序。该可执行文件深埋在 Visual Studio 安装目录中。您可以通过在 Visual Studio.

的安装文件夹中搜索 Windows 文件资源管理器中的“vstest.console.exe”来找到计算机上的确切路径

基本的命令行参数是:

path\to\vstest.console.exe path\to\tests.dll

即运行构建测试项目生成的DLL文件中的所有测试。有无数的过滤选项。

运行 SpecFlow 标签测试

运行通过 SpecFlow 标记从命令行进行测试很容易。每个标签都变成一个 [TestCategory] 属性,所以只需使用 TestCategory 过滤器:

path\to\vstest.console.exe path\to\tests.dll /TestCaseFilter:"TestCategory=SpecFlowTagName"

举个例子,假设您遇到这种情况:

Feature: Application Security
    In order to ...
    As a ...
    I want to ...

@SmokeTest
Scenario: Logging in
    Given "tester" is a registered user
    When the user logs in as "tester"
    Then the user should see their dashboard

上面的场景有一个与之关联的标签:SmokeTest。您可以 运行 这个场景,以及使用此命令标记为“SmokeTest”的任何其他场景:

path\to\vstest.console.exe path\to\tests.dll /TestCaseFilter:"TestCategory=SmokeTest"

运行 SpecFlow 按功能测试

每个功能都变成了测试 class。功能标题(不是文件名。功能文件“功能:...”之后的文本。)被转换为 C# class 名称。非字母数字字符将转换为“_”字符。然后在上面附加了“Feature”这个词。

使用此示例功能:

Feature: Application Security
    In order to ...
    As a ...
    I want to ...

功能标题是 Application Security,因此测试 class 被命名为 ApplicationSecurityFeature。现在您可以 运行 整个功能文件的全名:

vstest.console.exe tests.dll /TestCaseFilter:"FullyQualifiedName~ApplicationSecurityFeature"

运行 SpecFlow 测试场景

这只是 运行按功能排列它们的变体。特性文件中的每个场景都成为一个测试方法。场景的标题转换为 C# class 名称,将所有非字母数字字符替换为“_”。

鉴于此功能和场景:

Feature: Application Security
    In order to ...
    As a ...
    I want to ...

Scenario: Logging in
    ...

class 名称为“ApplicationSecurityFeature”,测试方法名称为LoggingIn。同样,运行 通过完全限定名称:

vstest.console.exe tests.dll /TestCaseFilter:"FullyQualifiedName~ApplicationSecurityFeature.LoggingIn"

有关 vstest 命令行选项的更多信息,请参见 Microsoft.com:VSTest.Console.exe command-line options