我下载了最新的VS2022 v17.1社区版,它没有内置代码覆盖。我已经习惯了企业版,我所能找到的就是社区版的付费选项。
可以在免费的VS2022社区版中进行代码覆盖吗?
发布于 2021-12-16 08:57:15
您有使用VS 2022的精细代码覆盖率,您可以在这里访问它,https://github.com/FortuneN/FineCodeCoverage/releases并单击2022文件。
在那之后,它只是一个插件,你安装在你的电脑上,它可以用于每一个项目,而不需要添加一个项目的项目。
更新:现在可以直接从市场上获得,所以您可以从Extensions安装它,也可以从市场(https://marketplace.visualstudio.com/items?itemName=FortuneNgwenya.FineCodeCoverage2022)下载它并在您的计算机上执行它。
发布于 2021-12-12 06:53:29
XUnit (和NUNIT -见最后一段)测试项目附带NuGet插件Coverlet.Collector
这不需要安装在任何项目中,您只需运行我在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")
}
将其放入TestProject中:
结果相当好(免费):
您可以向下查看突出显示的行覆盖率,但它不像企业版那样强大或集成:
我还更新了脚本以支持History:
‘
NUnit UPDATE:此脚本也适用于NUNit,只需包含以下引用:
<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>
发布于 2022-09-08 13:39:41
我在Visual扩展方面遇到了一些问题,所以我最终使用了我最好的朋友--命令行。
您可以从命令行使用dotnet-coverage
和dotnet-reportgenerator-globaltool
进行操作。
我认为这应该适用于任何.Net核心运行时和VS版本,以及CI服务器(我已经测试过.Net 5)。
dotnet tool install -g dotnet-coverage
dotnet tool install -g dotnet-reportgenerator-globaltool
dotnet-coverage collect -f xml -o coverage.xml dotnet test <solution/project>
reportgenerator -reports:coverage.xml -targetdir:.\report -assemblyfilters:+MyTestedAssembly.dll
report\index.html
https://stackoverflow.com/questions/70321465
复制相似问题