【Azure DevOps系列】Azure DevOps生成代码覆盖率
前言
在做单元测试时,代码覆盖率可以作为我们衡量代码质量的一个指标,本章我们将使用Azure DevOps帮助我们生成代码覆盖率的结果.Azure DevOps构建管道还是具有代码覆盖率选项的,在Visual Studio测试平台在已经集成了Coverlet格式的数据收集器,它其实并不难,它是可以开箱即用的。获取Coverlet格式报告几乎都是可以拿命令行参数去解决的。
在单元测试项目中需要引入nuget包coverlet.collector,当然只需要在单元测试项目中引用他,下面这个代码片段是单元测试模板自动生成的,我只是引入了一个我自己的类库。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" />
</ItemGroup>
</Project>
如何在Azure DevOps中使用?
第一步是在构建之前对项目进行还原nuget包,这会将所有的包拉到构建代理的本地文件夹中.
还原项目包(dotnet restore)
- task: DotNetCoreCLI@2
displayName: 'dotnet restore'
inputs:
command: restore
构建项目(dotnet build)
- task: DotNetCoreCLI@2
displayName: 'dotnet build'
inputs:
command: build
运行单元测试,其实上面的管道任务都是非常简单的,但是对于单元测试,我们需要设置dotnet cli将测试结果进行收集,搜集为cobertura格式,这是通过命令行参数来完成的。
正如下所示:
运行单元测试
- task: DotNetCoreCLI@2
displayName: 'dotnet test'
inputs:
command: test
projects: '**/XUnitTestProject1.csproj'
arguments: '--configuration $(BuildConfiguration) --collect "XPlat Code coverage" -- RunConfiguration.DisableAppDomain=true'
当然我们可以在coverlet中了解更多的信息https://discoverdot.net/projects/coverlet
安装报告生成工具
- task: DotNetCoreCLI@2
displayName: Install ReportGenerator Global Tool
inputs:
command: custom
custom: tool
arguments: install dotnet-reportgenerator-globaltool -g
使用reportgenerator工具生成报告
- script: 'reportgenerator -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/coverlet/reports -reporttypes:"Cobertura"'
displayName: 'Create reports'
代码报告发布到Azure DevOps
最后这一步做的是将刚才生成的所有信息上传到Azure DevOps管道,这样我们就可以在Azure DevOps Ui中查看覆盖率的相关信息了。
- task: PublishCodeCoverageResults@1
displayName: 'Publish code coverage'
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: '$(Build.SourcesDirectory)/coverlet/reports/Cobertura.xml'
查看报告
执行构建管道后,结果将在构建的“代码覆盖率报告”选项卡中可见。


常见问题
1.如果在Azure DevOps Linux镜像系统中是不可以的,可能会出现No executable found matching command "dotnet-reportgenerator" ,这个问题很头疼,我们在安装生成报告工具的时候并没有将全局命令安装成功,安装后并没有更新PATH,要解决这个问题要在调用reportgenerator另外执行一个CommandLine脚本:
- task: CmdLine@2
inputs:
script: 'echo "##vso[task.prependpath]$HOME/.dotnet/tools"'
2.如何生成覆盖率图标?
- 覆盖率图标:
https://img.shields.io/azure-devops/coverage/{组织名称}/{项目名称}/2/{分支} - 单元测试个数:
https://img.shields.io/azure-devops/tests/{组织名称}/{项目名称}/2/{分支}
Reference
https://github.com/microsoft/azure-pipelines-tasks/issues/9472
https://github.com/hueifeng/AzureDevOpsDemo/tree/demo2
【Azure DevOps系列】Azure DevOps生成代码覆盖率的更多相关文章
- Azure DevOps to Azure AppServices
Azure DevOps is a complete solution for software development, from planning to building to deploymen ...
- [Azure Devops] 使用 Azure Boards 管理工作
1. 什么是 Azure Boards 通过 Azure Boards 网络服务,团队可以管理其软件项目.它提供了丰富的功能,包括 Scrum 和看板的本地支持.可定制的仪表板和集成报告.这些工具可以 ...
- DevOps系列——Jenkins/Gitlab自动打包部署
前面只说了DevOps的两个基础组件Jenkins和GitLab,客官也不要着急,我们玩就玩的深入一点,Gitlab和Jenkins的各种配置和 插件很多,也够啃一阵子的,不要照着操作一通就感觉万事大 ...
- Azure Storage 系列(六)使用Azure Queue Storage
一,引言 在之前介绍到 Azure Storage 第一篇文章中就有介绍到 Azure Storage 是 Azure 上提供的一项存储服务,Azure 存储包括 对象.文件.磁盘.队列和表存储.这里 ...
- Microsoft Azure News(6) Azure新F系列虚拟机
<Windows Azure Platform 系列文章目录> 我们知道Azure虚拟机的CPU和内存是固定搭配的,不可以按照用户的想法随意更改. 在有些时候,我们需要虚拟机CPU核心数量 ...
- Microsoft Azure News(4) Azure新D系列虚拟机上线
<Windows Azure Platform 系列文章目录> Update 2016-05-07 注意事项: Azure的数据中心建设是有先后顺序的,最早是落地了A系列的虚拟机,然后是D ...
- Microsoft Azure News(5) Azure新DV2系列虚拟机上线
<Windows Azure Platform 系列文章目录> 在之前的文章中,笔者了Azure VM虚拟机,分为A系列和D系列2种 Microsoft Azure News(4) Azu ...
- Windows Azure 入门系列课程Windows Azure 入门系列课程
Windows Azure 入门系列课程 https://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/NewSeries/A ...
- 微软云平台windows azure入门系列八课程
微软云平台windows azure入门系列八课程: Windows Azure入门教学系列 (一): 创建第一个WebRole程序与部署 Windows Azure入门教学系列 (二): 创建第一个 ...
随机推荐
- 网络安全 - SSL/TLS协议运行机制的概述
大学时也系统学过相关的网络安全,但那时并没有理论联系实践,稀里糊涂的,现在才意识到所学的东西都是好东西,可惜已晚. 来自http://www.ruanyifeng.com/blog/2014/02/s ...
- Linux的VMWare14中配置Centos7桥接网络环境(网络一)
1.查看当前初始环境如下:在windows端先查看本机ip ifconfig
- .NET三层架构各项目之间的引用关系及如何添加引用?
1.1三层之间的引用 整个.NET框架的项目创建完成之后,在开发之前,还需要建立各个项目之间的引用关系,以便在编写C#业务逻辑代码时调用相关项目的类库. 1.1.1 引用关系规则 .NET三层架构中的 ...
- 002_centos7关闭防火墙
防火墙是比较烦人的,在自己做实验,或者实际应用中,如果配置不好的话,会出现各种匪夷所思的问题,那么如何关闭呢 在centos7里,防火墙改为了firewalld进程 首先用命令firewall-cmd ...
- JS 弹出框拖拽
css代码 body { margin:; text-align: center; } .box { display: none; background-color: #fff !important; ...
- ALGEBRA-前言
“当你读一页不到一个小时的话,可能是你读太快了” 哈哈 可以 慢慢品
- C#LeetCode刷题之#589-N叉树的前序遍历(N-ary Tree Preorder Traversal)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4090 访问. 给定一个 N 叉树,返回其节点值的前序遍历. 例如 ...
- C#设计模式之5-单例模式
单例模式(Singleton Pattern) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/397 访问. 单例模式属 ...
- NodeJs path.resolve的使用
__dirname __dirname 指向运行代码的文件夹 console.info('__dirname', __dirname) // C:\Leslie\Web_learning\Daily- ...
- vue跳转页面问题记录
跳转到别的页面带参数 const space = this.pageHelperspace['search'] = this.searchconst query_params = Object.ass ...