如何从 Visual Studio 2022 社区版中的单元测试获取代码覆盖率?

问题描述 投票:0回答:3

我已经下载了最新的 VS2022 v17.1 社区版,它没有内置代码覆盖率。我已经习惯了企业版,我所能找到的只是社区版的付费选项。

VS2022社区版可以免费进行代码覆盖吗?

enter image description here

visual-studio unit-testing nunit code-coverage xunit
3个回答
63
投票

您拥有与 VS 2022 一起使用的 Fine Code Coverage,您可以在此处访问它 https://github.com/FortuneN/FineCodeCoverage/releases 并单击 2022 文件。

之后,它只是您安装在计算机上的一个插件,并且可用于每个项目,而无需逐个项目添加它。

更新: 现在它可以直接从市场获得,因此您可以从扩展管理器安装它,也可以从市场 (https://marketplace.visualstudio.com/items?itemName=FortuneNgwenya.FineCodeCoverage2022) 下载它并在您的电脑。


35
投票

XUnit(和 NUNIT - 请参阅最后一段)测试项目附带 NuGet 插件Coverlet.Collector

enter image description here

这不需要安装在任何项目中,您所需要做的就是运行我在 Powershell 脚本中制作的这些步骤:

ExecCodeCoverage.ps1

# PURPOSE: Automates the running of Unit Tests and Code Coverage
# REF: https://stackoverflow.com/a/70321555/495455

# If running outside the test folder
#cd E:\Dev\XYZ\src\XYZTestProject

# This only needs to be installed once (globally), if installed it fails silently: 
dotnet tool install -g dotnet-reportgenerator-globaltool

# Save currect directory into a variable
$dir = pwd

# Delete previous test run results (there's a bunch of subfolders named with guids)
Remove-Item -Recurse -Force $dir/TestResults/

# Run the Coverlet.Collector - REPLACING YOUR SOLUTION NAME!!!
$output = [string] (& dotnet test ../YOURSOLUTIONNAME.sln --collect:"XPlat Code Coverage" 2>&1)
Write-Host "Last Exit Code: $lastexitcode"
Write-Host $output

# Delete previous test run reports - note if you're getting wrong results do a Solution Clean and Rebuild to remove stale DLLs in the bin folder
Remove-Item -Recurse -Force $dir/coveragereport/

# To keep a history of the Code Coverage we need to use the argument: -historydir:SOME_DIRECTORY 
if (!(Test-Path -path $dir/CoverageHistory)) {  
 New-Item -ItemType directory -Path $dir/CoverageHistory
}

# Generate the Code Coverage HTML Report
reportgenerator -reports:"$dir/**/coverage.cobertura.xml" -targetdir:"$dir/coveragereport" -reporttypes:Html -historydir:$dir/CoverageHistory 

# Open the Code Coverage HTML Report (if running on a WorkStation)
$osInfo = Get-CimInstance -ClassName Win32_OperatingSystem
if ($osInfo.ProductType -eq 1) {
(& "$dir/coveragereport/index.html")
}

将其放入测试项目中:

enter image description here

^ 右键单击“使用 Powershell 运行”

结果非常好(免费):

enter image description here

您可以深入查看突出显示的线路覆盖范围,它只是不如企业版强大或集成:

enter image description here

我也更新了脚本以支持历史记录:

enter image description here


'

NUnit 更新: 该脚本也适用于 NUNit,只需包含这些引用:

enter image description here

  <ItemGroup>
    <PackageReference Include="coverlet.collector" Version="3.1.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="3.1.2" />
    <PackageReference Include="GenFu" Version="1.6.0" />
    <PackageReference Include="Moq" Version="4.18.2" />
    <PackageReference Include="NUnit" Version="3.13.3" />
    <PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
    <PackageReference Include="NunitXml.TestLogger" Version="3.0.127" />
    <PackageReference Include="ReportGenerator" Version="5.1.10" />
  </ItemGroup>

19
投票

我在使用 Visual Studio 扩展时遇到了一些问题,所以我最终使用了我最好的朋友,命令行。

您可以使用 Microsoft 的

dotnet-coverage
danielpalme
dotnet-reportgenerator-globaltool

从命令行进行操作

我相信这应该适用于任何 .Net core 运行时和 VS 版本,也适用于 CI 服务器(我测试过 .Net 5)

  • 安装(以管理员身份运行)
dotnet tool install -g dotnet-coverage
dotnet tool install -g dotnet-reportgenerator-globaltool
  • 使用 XML 输出格式运行测试:
dotnet-coverage collect -f xml -o coverage.xml dotnet test <solution/project>
  • 生成 html 报告
reportgenerator -reports:coverage.xml -targetdir:.\report -assemblyfilters:+MyTestedAssembly.dll
  • 打开
    report\index.html
© www.soinside.com 2019 - 2024. All rights reserved.