是否可以在Azure DevOps中使用Coverlet获得.NET Framework Project的代码覆盖率? [英] Is it possible to get Code Coverage of .NET Framework Project using coverlet in Azure DevOps?

查看:88
本文介绍了是否可以在Azure DevOps中使用Coverlet获得.NET Framework Project的代码覆盖率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以轻松地配置为在Azure DevOps中获取 .NET Core 项目的覆盖率结果,但是 .NET Framework 项目却没有运气.非常感谢您提出建议,因为Coverlet文档明确表示我们也可以将其用于 .NET Framework 项目.这个问题有点类似于我的问题,但是我在那里没有看到任何答案,您可以使用Coverlet吗在.NET Framework项目中获取代码覆盖率数据?

解决方案

最后,我找到了一个更简单的解决方案.在这里,

  • 在测试项目文件中添加< IsTestProject> true</IsTestProject >.

  • 在测试项目位置运行推荐的 dotnet test/dotnet test/p:CollectCoverage = true TestProject.csproj存在)

  • 运行命令后,您可能会收到以下错误消息,

    找不到导入的项目"C:\ Program Files \ dotnet \ sdk \ 3.1.100 \ Microsoft \ VisualStudio \ v16.0 \ WebApplications \ Microsoft.WebApplication.targets".

  • 在项目文件(要进行单元测试并获得代码覆盖率)中,更改以下内容导入声明

    <导入项目="$(VSToolsPath)\ WebApplications \ Microsoft.WebApplication.targets" Condition ='$(VSToolsPath)'!=""/>

    <导入项目="$(VSToolsPath)\ TeamTest \ Microsoft.TestTools.targets" Condition ="Exists('$(VSToolsPath)\ TeamTest \ Microsoft.TestTools.targets')"/>

这在本地和Azure DevOps上都对我有用.

注意:不要忘记在测试项目中安装Coverlet.msbuild.

更新:

仅当您没有收到.Microsoft.WebApplication.targets找不到" 错误时,上述方法才有效.注释建议的import语句将使发布最终失败,这是显而易见的.因此,我最终使用了Coverlet.Console,它运行顺利,没有任何错误.但是,要使用Coverlet.console,我需要 TestProject.dll 文件而不是项目文件(.csproj);所以我不得不为测试项目添加额外的构建任务.此处是如何安装和使用Coverlet.console的文档

希望这对那些最终在这里着陆的人有帮助.

I easily configured to get the coverage result for .NET Core Projects in Azure DevOps, but no luck with .NET Framework Projects.
So, I would be so grateful to get suggestion on this because coverlet documentation is clearly saying that we can also use it for .NET Framework Projects. This question is kinda similar to mine but I didn't see any answer there, Can you use Coverlet to get code coverage data in a .NET Framework project?

解决方案

Finally, I found a simpler solution. Here it is,

  • Add <IsTestProject>true</IsTestProject> in test project file.

  • Run the commend dotnet test/dotnet test /p:CollectCoverage=true being at test project location(where TestProject.csproj exist)

  • You might get following error after running the command,

    The imported project "C:\Program Files\dotnet\sdk\3.1.100\Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets" was not found.

  • In the project file(which you want to unit test and get code coverage), change following Import statement

    This, <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />

    To, <Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />

This worked for me both locally and with Azure DevOps.

Note: Don't forget to install Coverlet.msbuild in your test project.

Update:

Above approach works only if you don't get ".Microsoft.WebApplication.targets was not found" error. Commenting suggested import statement will make publish fail at the end, which is obvious. So, I ended up using Coverlet.Console and it's working smoothly without any error. But, to use coverlet.console I needed TestProject.dll file instead of project file(.csproj); so I had to add extra build task for test project. Here is the documentation how to install and use Coverlet.console

Hope this will be helpful for the ones who end up landing here.

这篇关于是否可以在Azure DevOps中使用Coverlet获得.NET Framework Project的代码覆盖率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆