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

时间:2019-01-01 20:39:02

标签: .net-core xunit.net

我目前正在使用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%   |
+---------+--------+--------+--------+

我的问题是如何仅对mymodule进行测试?

谢谢。

2 个答案:

答案 0 :(得分:0)

好吧,我认为问题出在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%      |
+------------+----------+

答案 1 :(得分:0)

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

-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