繁体   English   中英

如何在Visual Studio中测量ASP.NET Core项目中的代码覆盖率?

[英]How to measure Code Coverage in ASP.NET Core projects in Visual Studio?

我想在ASP.NET Core应用程序中测量我的XUnit-Tests的代码覆盖率。 Visual Studio 2015中的.NET Core工具是预览2,目前代码覆盖率不起作用。

2月份的博客文章http://dotnetthoughts.net/measuring-code-coverage-of-aspnet-core-applications-using-opencover/通过使用打开封面的命令行显示了一种解决方法。 我正在寻找Visual Studio内部更加集成的方式。

有没有人听说过与XUnit结合使用的更好/更集成的代码覆盖率测量方法?

NuGet包 Microsoft.CodeCoverage 1.0.1添加到project.json

我正在为Asp.NET工作模板,现在我正在进行单元测试,所以我看到了你的帖子。 你可以在这里看到项目/配置。

免责声明 :这些步骤来自使用OpenCover测量ASP.NET核心覆盖范围--DotNetThoughts

虽然海报上有关于这个网站的说法,但我认为在这里继续繁荣这些步骤仍然是最好的。

注意 :这些说明虽然适用于Windows操作系统,但应该可以轻松地用于OpenCover和ReportGenerator支持的任何操作系统。

  1. 创建您的ASP.NET核心MVC网站
  2. 确保global.json在项目中具有“测试”
  3. 右键单击解决方案中的测试文件夹并添加新项目
  4. 确保项目类型是.NET Core类库
  5. 将以下内容添加到project.json依赖项节点:
    • “dotnet-test-xunit”:“2.2.0-preview2-build1029”,
    • “xunit”:“2.2.0-beta3-build3402”
    • “Microsoft.CodeCoverage”:“1.0.2”
  6. 将以下内容添加到project.json版本下
    • “testRunner”:“xunit”,
  7. 写下你的单元测试
  8. 下载OpenCover报告生成器
  9. 安装OpenCover
  10. 将Report Generator解压缩到名为Report Generator的文件夹中的OpenCover install dir中
  11. 在项目中创建一个BAT文件,并将其命名为cover.bat
  12. 添加以下内容:
@echo off

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"

SET targetargs="test"  
SET filter="+[*]NAMESPACE.* -[*.Test]* -[xunit.*]* -[FluentValidation]*"  
SET coveragefile=Coverage.xml  
SET coveragedir=Coverage

REM Run code coverage analysis  
%opencover% -oldStyle -register:user -target:%dotnet% -output:%coveragefile% -targetargs:%targetargs% -filter:%filter% -skipautoprops -hideskipped:All

REM Generate the report  
%reportgenerator% -targetdir:%coveragedir% -reporttypes:Html;Badges -reports:%coveragefile% -verbosity:Error

REM Open the report  
start "report" "%coveragedir%\index.htm"
  1. 用项目命名空间替换NAMESPACE。
  2. 如果有多个项目,请根据需要为每个名称空间复制正则表达式+[*]NAMESPACE.*
  3. 保存文件
  4. 打开命令提示符并确保在测试项目中
  5. 键入cover以使您的单元测试运行,并且您的覆盖结果将以HTML格式或您在步骤11中命名为bat文件的任何内容。

代码覆盖对我有用,对于使用Microsoft.CodeCoverage .Net Core,如上所述。

检查是否已将Microsoft.CodeCoverage nuget添加到测试项目中

还要检查主项目上的project.json文件,debugType属性应该是“full”而不是“portable”

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true,
    "debugType": "full"
  },

这对我来说很有用

Coverlet( https://github.com/tonerdo/coverlet )是最近出现的一个新项目。 它与msbuild一起工作,为覆盖提供了直接的解决方案

我得到了一些URL提示。 (底部的网址)

这是“穷人”,但它会在你的默认浏览器中激活一个htm文件,并提供一些好的报道htm报告。 比VS 2017 ENTERPRISE版便宜得多!

显然,您必须将__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=

我的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>

值得注意的是,“coverlet.msbuild”和“dotnet-reportgenerator-cli”。 我知道你需要“coverlet.msbuild”,另一个可能只是你可能或不需要的实验。

我正在使用很多bat变量来驱动东西,并输出到%TMP%文件夹以避免VS / git想要检查文件。

提示来自:

https://holsson.wordpress.com/2018/11/30/test-code-coverage-with-net-core-and-tfs/

https://tattoocoder.com/cross-platform-code-coverage-arrives-for-net-core/

https://medium.com/bluekiri/code-coverage-in-vsts-with-xunit-coverlet-and-reportgenerator-be2a64cd9c2f

我刚刚尝试了ReSharper版本2016.3 EAP 4(早期访问预览)。 除了单元测试执行之外,dotCover还会返回我的.net核心测试项目的代码覆盖率信息。

OpenCover + ReportGenerator。 作为NUGET包安装到您的一个测试项目中(一次,因为它们只需要出现在包文件夹中)。

然后你可以将这个powershel https://github.com/rpokrovskij/opencover4vs.ps1放到你的解决方案文件夹中,并使用路径全局设置你的测试项目,例如

$ TestProjectsGlobbing = @(,'*。Test.csproj')

然后,您可以将其作为常规Powershel脚本运行。 结果应显示在\\ TestsResults \\ report \\ index.html的解决方案文件夹中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM