指定到特定模块以使用 xunit 进行测试

Specify to a specific module to test using xunit

我目前正在使用 xunit 对我的项目进行单元测试。但是,当我激活收集覆盖选项时,它显示正在测试其他模块。

实际上,我正在运行执行此命令。

dotnet test mymodule.Tests /p:CollectCoverage=true /p:CoverletOutputFormat=opencover

项目文件是这样的。

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="coverlet.msbuild" Version="2.5.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
    <PackageReference Include="xunit" Version="2.3.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\mymodule\mymodule.csproj" />
  </ItemGroup>

</Project>

这是输出。

Build started, please wait...
Build completed.

Test run for C:\workspace\mymodule\mymodule.Tests\bin\Debug\netcoreapp2.1\mymodule.Tests.dll(.NETCoreApp,Version=v2.1)
Microsoft (R) Test Execution Command Line Tool Version 15.9.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...

Total tests: 90. Passed: 90. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 1.5794 Seconds

Calculating coverage result...
  Generating report 'C:\workspace\mymodule\mymodule.Tests\coverage.opencover.xml'

+--------------------------------------------------+--------+--------+--------+
| Module                                           | Line   | Branch | Method |
+--------------------------------------------------+--------+--------+--------+
| mymodule                                         | 66.2%  | 75%    | 63.1%  |
+--------------------------------------------------+--------+--------+--------+
| xunit.runner.reporters.netcoreapp10              | 2.1%   | 0.8%   | 8.9%   |
+--------------------------------------------------+--------+--------+--------+
| xunit.runner.utility.netcoreapp10                | 11.9%  | 6.6%   | 16.8%  |
+--------------------------------------------------+--------+--------+--------+
| xunit.runner.visualstudio.dotnetcore.testadapter | 49.3%  | 41.8%  | 49.4%  |
+--------------------------------------------------+--------+--------+--------+

+---------+--------+--------+--------+
|         | Line   | Branch | Method |
+---------+--------+--------+--------+
| Total   | 24.3%  | 17.9%  | 25.6%  |
+---------+--------+--------+--------+
| Average | 6.075% | 4.475% | 6.4%   |
+---------+--------+--------+--------+

我的问题是如何 运行 只测试我的模块?

提前致谢。

嗯,我认为问题出在 coverlet.msbuild 我尝试将版本降级到 1.0.0,但一切正常。

Build started, please wait...
Build completed.

Test run for C:\workspace\mymodule\mymodule.Tests\bin\Debug\netcoreapp2.1\mymodule.Tests.dll(.NETCoreApp,Version=v2.1)
Microsoft (R) Test Execution Command Line Tool Version 15.9.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...

Total tests: 90. Passed: 90. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 1.6376 Seconds

Calculating coverage result...
  Generating report 'C:\workspace\mymodule\mymodule.Tests\coverage.xml'

+------------+----------+
| Module     | Coverage |
+------------+----------+
| mymodule   | 53%      |
+------------+----------+

您实际上可以使用标志来指定程序集

-p:Include="[mymodule]*"

此标志指定您只想覆盖指定的程序集。因此,您的最终 cmd 将显示为:

dotnet test mymodule.Tests /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Include="[mymodule]*"

https://github.com/tonerdo/coverlet/blob/master/Documentation/MSBuildIntegration.md