基于OpenCover进行代码覆盖率测试

  最近开始接触白盒测试,开发同事对OpenCover(开源C#代码覆盖率统计工具)、ReportGenerator(将XML报告转换成HTML的工具)二次开发出一个代码覆盖率的工具。下面基于该工具,记录对OpenCover、ReportGenerator等的理解。

未使用OpenCover时,被测程序的正常运行流程:

使用OpenCover、ReportGenerator后,被测程序的运行流程:

  OpenCover中的Profiler启动运行被测程序的程序或服务——>运行被测程序——>得到运行结果同时,也得到xml覆盖率结果——>ReportGenerator将xml转成HTML

该工具使用及主要实现流程:

——>配置好OpenCover、ReportGenerator、被测程序的路径、IIS Express config的路径

——>执行的时候就根据配置好的参数执行命令,即相当于在cmd中执行:

H:\白盒测试\Debug\Opencover\OpenCover.Console.exe -target:"C:\Program Files (x86)\IIS Express\iisexpress.exe" -targetdir:"D:\被测站点\xxx.xxx.com\bin" -targetargs:"/site:xxx_bh.xxx.com /config:\"C:\Users\ym\Documents\IISExpress\config\applicationhost.config\"" -register:ym -output:"H:\白盒测试\xml_ym\xxx_bh.xxx.com\xxx_bh.xxx.com.xml"

——>此时IIS Express已经启动,访问在通过IIS Express配置的站点,开始测试。

——>测试完成后,退出IIS Express,生成xml文件。

——>使用ReportGenerator生成HTML文档,相当于在cmd中执行命令:

H:\白盒测试\Debug\ReportGenerator\ReportGenerator.exe -reports:H:\白盒测试\xml\白盒测试.xml -targetdir:H:\白盒测试\xml\html

关于OpenCover:

  OpenCover是用于.NET 2.0及以上应用程序的代码覆盖的开源工具。OpenCover启动后,会收集覆盖结果。但是必须要有的PDB文件以及可执行文件和程序集,因此应该在调试模式下构建测试中的应用程序。如果未找到PDB文件,则不会收集任何覆盖数据。(或者直接获取源代码)

OpenCover基本用法(命令行参数):
-target: 应用程序可执行文件或服务名称的路径  (我的理解:将被测程序运行起来的程序或服务。网上帖子中,大多用的NUnit。本文例子用的是IIS Express)
-filter要应用于选择性地包括或排除coverage结果中的程序集和类的过滤器列表
-output: 输出XML文件的路径,如果为空,则将在当前目录中创建results.xml
-register [:user] - 注册和取消注册代码覆盖率分析器
-targetargs: - 要传递给目标进程的参数
-targetdir: - 目标目录的路径或PDB文件的备用路径  (我的理解:被测程序所在位置)

用法:https://github.com/OpenCover/opencover/wiki/Usage

文档:https://github.com/opencover/opencover/blob/master/main/OpenCover.Documentation/Usage.pdf

参考:http://www.cnblogs.com/binyao/category/477233.html

   http://www.cnblogs.com/tylerzhou/p/9076386.html

   http://blog.alantsai.net/posts/2017/01/devopsseries-opencover-intro

     https://www.codeproject.com/Articles/677691/Getting-code-coverage-from-your-NET-testing-using

什么是IIS Express:

  一个兼具Visual Studio的ASP.NET开发服务器和Windows的IIS Web服务器功能的轻量级web服务器。

  具体描述:https://stackify.com/what-is-iis-express/

  配置:https://blog.csdn.net/zhangjk1993/article/details/36671105

为什么用IIS Express:

文档中有这么一段描述:

Running against IIS
Normally I’d suggest running against IISEXPPRESS as I think it is easier to automate. However for those who really want to run against a full blown IIS then the following instructions (supplied by a user) will hopefully suffice.
“The trick is to start OpenCover to run the w3wp.exe process in debug mode e.g.
OpenCover.Console.exe -target:C:\Windows\System32\inetsrv\w3wp.exe -targetargs:-debug 
-targetdir:C:\Inetpub\wwwwoot\MyWebApp\bin\ -filter:+[*]* -register:user
There are some prerequisites though:
1.All applications running under the site must make use of the same app pool; you'll get errors in the EventLog otherwise.
2.inetserver needs to be stopped, before starting w3wp.exe in debug mode. You can use the following command:
net stop w3svc /y
After testing/code coverage completion you can close the w3wp.exe process and start the inetserver again:
net start w3svc
This procedure was tested on a Win2008 machine with IIS7.5”
You can also run multiple OpenCover instances against separate IIS sites by using the –s option when running IIS to choose the siteid e.g.
OpenCover.Console.exe -target:C:\Windows\System32\inetsrv\w3wp.exe 
    -targetargs:"-debug -s 1" 
    -targetdir:%WebSite_Path% 
    -filter:+[*]* 
    -register:user 
    -output:%CoverageResult_Path%
Then you can use ReportGenerator to merge the coverage results. 

大致意思就是用IIS Express更方便。

而如果要启动完整的IIS,那就要:

1)在调试模式下运行被测程序并启动OpenCover(将代码构建到调试模式获取PDB文件。或者直接获取源代码)

2)所有在站点下运行的应用程序都必须使用相同的应用程序池;否则,会报错

3)在调试模式下启动被测程序前,需要停止intserver(PS:不清楚intserver是什么)

关于PDB文件:

https://blogs.msdn.microsoft.com/vcblog/2016/02/08/whats-inside-a-pdb-file/

https://tpodolak.com/blog/2017/10/12/net-core-calculating-code-coverage-opencover-windows/

使用ReportGenerator 将xml生成HTML报告:

ReportGenerator用于将OpenCoverPartCoverVisual StudioNCover生成的XML报告转换为各种格式的友好可读报告。

使用指南可以在其主页上找到,最有用的命令是(命令行参数):

  • -reports: - 应该解析的覆盖率报告,分号分隔,允许使用通配符
  • -targetdir: - 应保存生成的报告的目录
  • -sourcedirs:[;] [;] - 包含相应源代码的目录,可选,分号分隔
  • -classfilters:<(+ | - )filter> [; <(+ | - )filter>] [; <(+ | - )filter>] - 报表中应包含或排除的类列表,可选,通配符被允许。

  

如下图,为报告部分截图,包含语句覆盖和分支覆盖情况。点击相应的页面,会进入对应程序,可看到具体覆盖到哪一行代码。

  

代码覆盖详情,绿色表示完全覆盖,橙色表示该行代码还有分支未覆盖到,红色则未覆盖。

参考资料:https://www.cnblogs.com/tylerzhou/p/9076537.html

     https://www.cnblogs.com/SivilTaram/p/vs_opencover_unit_coverage.html

猜你喜欢

转载自www.cnblogs.com/minerrr/p/9253960.html
今日推荐