C# 如何在Visual Studio中测量ASP.NET核心项目中的代码覆盖率?
我想测量ASP.NET核心应用程序中XUnit测试的代码覆盖率。Visual Studio 2015中用于.NET Core的工具是preview 2,到目前为止,代码覆盖率不起作用 2月份的博客文章通过使用opencover命令行显示了一个解决方法。我正在寻找一种在VisualStudio内部更集成的方式 有没有人听说过与XUnit结合使用可以更好/更集成地测量代码覆盖率的方法?将添加到您的C# 如何在Visual Studio中测量ASP.NET核心项目中的代码覆盖率?,c#,visual-studio-2015,asp.net-core,code-coverage,.net-core,C#,Visual Studio 2015,Asp.net Core,Code Coverage,.net Core,我想测量ASP.NET核心应用程序中XUnit测试的代码覆盖率。Visual Studio 2015中用于.NET Core的工具是preview 2,到目前为止,代码覆盖率不起作用 2月份的博客文章通过使用opencover命令行显示了一个解决方法。我正在寻找一种在VisualStudio内部更集成的方式 有没有人听说过与XUnit结合使用可以更好/更集成地测量代码覆盖率的方法?将添加到您的项目.json 我正在为Asp.NET开发模板,现在我正在进行单元测试,所以我看到了你的帖子。您可以看到
项目.json
我正在为Asp.NET开发模板,现在我正在进行单元测试,所以我看到了你的帖子。您可以看到项目/配置。我刚刚试用了ReSharper版本2016.3 EAP 4(早期访问预览)。除了单元测试执行之外,dotCover现在还返回my.net核心测试项目的代码覆盖率信息。免责声明:这些步骤是从中给出的 尽管海报上写的是这个网站,但我认为最好还是把这些步骤放在这里实现繁荣 注意:这些说明虽然是针对windows操作系统定制的,但应该可以轻松地适用于OpenCover和ReportGenerator支持的任何O/S
- “dotnet测试xunit”:“2.2.0-preview2-build1029”
- “xunit”:“2.2.0-beta3-build3402”
- “Microsoft.CodeCoverage”:“1.0.2”
- “testRunner”:“xunit”
@echo关闭
SET dotnet=“C:\Program Files\dotnet\dotnet.exe”
SET opencover=“C:\Program Files(x86)\opencover\opencover.Console.exe”
SET-reportgenerator=“C:\Program Files(x86)\OpenCover\reportgenerator\reportgenerator.exe”
设置targetargs=“测试”
SET filter=“+[*]名称空间。*-[*.Test]*-[xunit.*-[FluentValidation]*”
设置Coverage file=Coverage.xml
SET Coverage dir=覆盖率
REM运行代码覆盖率分析
%opencover%-oldStyle-注册:用户-目标:%dotnet%-输出:%coveragefile%-目标:%TargetAgs%-过滤器:%filter%-SkipaToprops-隐藏:全部
REM生成报告
%reportgenerator%-targetdir:%coveragedir%-reporttypes:Html;徽章-报告:%coveragefile%-详细信息:错误
请打开报告
开始“报告”“%coveragedir%\index.htm”
+[*]命名空间。*
codecoverage适用于我,用于.Net Core,使用上面提到的
Microsoft.codecoverage
检查是否已将Microsoft.CodeCoverage
numget添加到测试项目中
还要检查主项目上的project.json文件,debugType属性应该是“full”而不是“portable”
这对我来说是固定的OpenCover+ReportGenerator。作为NUGET软件包安装到您的一个测试项目中(一次,因为它们只需要出现在软件包文件夹中) 然后,您可以将此PowerShell放在解决方案文件夹中,并使用路径全局化(例如 $TestProjectsGlobbing=@(,'*.Test.csproj')
然后可以将其作为常规PowerShell脚本运行。结果应该出现在\TestsResults\report\index.html的解决方案文件夹中。Coverlet()是最近出现的一个新项目。它与msbuild一起工作,并为覆盖率提供了一个直接的解决方案我使用了一些URL提示。(URL位于底部) 这是“穷人的”,但它会在您的默认浏览器中启动一个htm文件,其中包含一些覆盖率良好的htm报告。而且比VS 2017企业版便宜得多 显然,您必须将uu unitTestProject设置为正确的值
REM I call this file zzzCoverageRun.bat , or whatever name you want, but is a .bat file
REM PREREQUISITES
REM In Visual Studio, go to Tools / Nuget Package Manager / Package Manager Console
REM and run below line
REM dotnet tool install --global dotnet-reportgenerator-globaltool --version 4.0.15
set __currentDirectory=%cd%
set __sln=%__currentDirectory%\..\Solutions\My.Solution.sln
set __unitTestProject=%__currentDirectory%\..\UnitTests\My.UnitTests.csproj
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set __mydate=%%c-%%a-%%b)
For /f "tokens=1-3 delims=/:" %%a in ('time /t') do (set __mytime=%%a%%b%%c)
echo %__mydate%_%__mytime%
set __thisRunTag=%__localRepositoryName%%__mydate%_%__mytime%
set __coverageShortFileName=coverage.cobertura.xml
set __Common_TestResultsFileName=%TMP%\Zzz.TestResultsDirectory\%__thisRunTag%\%__coverageShortFileName%
REM set __ReportOutputDirectory=$(Build.SourcesDirectory)\TestResults\Coverage\Reports
set __ReportOutputDirectory=%TMP%\Zzz.CoverageResults\%__thisRunTag%
set __BuildConfiguration=Debug
REM build not needed
REM dotnet build "%__sln%"
dotnet test "%__unitTestProject%" --configuration %__BuildConfiguration% --logger:trx /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput="%__Common_TestResultsFileName%"
REM the below works because of the dotnet-reportgenerator-globaltool above
reportgenerator "-reports:%__Common_TestResultsFileName%" "-targetdir:%__ReportOutputDirectory%" -tag:%__thisRunTag% -reportTypes:htmlInline
start "" "%__ReportOutputDirectory%\index.htm"
set __coverageShortFileName=
set __currentDirectory=
set __ReportOutputDirectory=
set __sln=
set __unitTestProject=
set __Common_TestResultsFileName=
set __BuildConfiguration=
set __thisRunTag=
My UnitTests.csproj中包含以下内容:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<IsPackable>false</IsPackable>
<AssemblyName>My.UnitTests</AssemblyName>
<RootNamespace>My.UnitTests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<None Remove="StyleCop.Cache" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="2.6.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
<PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
<PackageReference Include="Moq" Version="4.10.1" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="dotnet-reportgenerator-cli" Version="4.0.15" />
</ItemGroup>
</Project>
netcoreapp2.1
假的
fyi:dotnet/corefx使用OpenCover来获取其测试的覆盖率。@DavidOsborne-不,商业工具也是可以接受的。我急切地等待着下一个Resharper EAP(早期访问预览)。那么dotCover呢?最近NCrunch也给我留下了深刻的印象,它也报道了覆盖范围。@DavidOsborne-两者都还不支持.net Core。对于Resharper,我希望它将出现在下一个版本中。NCrunch在这里讨论了这个特性啊。我不知道。对不起,你能详细说明一下你是怎么做到的吗?2016.3 EAP 4(在单独的.NET核心库中测试)对我不起作用@Wayne-我没有做任何特殊的事情。在安装了所有的Resharper Ultimate之后,我可以执行上下文菜单“Cover”并查看结果。所以在我的案例中没有什么特别的魔力。有趣的是,这对我来说根本不起作用,我的dotcover只是挂起了。您能用解决方案设置和project.json的描述更新您的答案吗?我将为OpenCover创建一个答案,作为那些没有jetbrains订阅的用户的一个选项。该选项现在在EAP6中工作-您能提供实际的Powershell脚本吗?@MichaelFredgeim,完成。谢谢你的关注真棒,谢谢你!!我为此奋斗了这么久。fwiw现在已满…
另请参见
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<IsPackable>false</IsPackable>
<AssemblyName>My.UnitTests</AssemblyName>
<RootNamespace>My.UnitTests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<None Remove="StyleCop.Cache" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="2.6.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
<PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
<PackageReference Include="Moq" Version="4.10.1" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="dotnet-reportgenerator-cli" Version="4.0.15" />
</ItemGroup>
</Project>