The opencover of Jenkins in conjunction with. NET Platform tools

Source: Internet
Author: User

First of all, what is Opencover, Opencover is a software for generating unit test coverage reports under the. NET platform, and is one of the few free coverage reporting tools under the. NET Platform. Similar tools include JetBrains's dotcover VisualStudio Unit Test Coverage tool, Dotcover is a paid-for software, VisualStudio's own unit test coverage reporting tool is limited to use in VisualStudio and cannot be automated with Jenkins, and it is only integrated into the Enterprise edition, which is not available in the Community edition.

The following is the GitHub warehouse address for Opencover

Https://github.com/OpenCover/opencover

Here's how to generate unit test coverage reports using Opencover.

Here we are still based on the previous section of the project, where we need to introduce another NuGet package, Opencover

Add reference completed after the project's packages directory there is a directory packages\opencover.xxx \tools (XXX is the version number) below a OpenCover.Console.exe file, It is a code coverage report that we combine with Jekins to generate unit tests to use.

The Unit Test Code Coverage tool, as its name implies, is a tool for analyzing and generating reports on unit tests, so it relies on unit testing, we use NUnit, and we've created unit tests with commands in the previous section, where we write the commands in the previous section as a bat batch file, To be called for Opencover.console.exe

The code is as follows

"E:\personal project\newtest2018\consoleapp1\packages\nunit.consolerunner.3.8.0\tools\nunit3-console.exe" "E:\ Personal Project\newtest2018\consoleapp1\nunittest\bin\debug\nunittest.dll "--work:" E:\personal project\ Newtest2018\consoleapp1\nunittest\bin\debug "

It is important to note that you should not copy the path above, but rather according to your actual situation (the location of the NUnit console tool, the location of the DLL where the project was generated, and the working directory)

Let's name this bat Runnuit.bat (note that the name can be random, not the same as an example)

We put Opencover to execute the command also in the bat file, here named Opcover.bat

"C:\Users\tylerzhou\.nuget\packages\opencover\4.6.519\tools\OpenCover.Console.exe"-target: "E:\personal project\ Newtest2018\consoleapp1\nunittest\bin\debug\runnuit.bat "-targetargs:"/nologo/noshadow "-targetdir:" E:\personal Project\newtest2018\consoleapp1\nunittest\bin\debug "-register:" User "-filter:" +[nunittest*]* "-output:" E:\ Personal Project\newtest2018\consoleapp1\nunittest\bin\debug\results.xml "

The key part of the above code is explained here: the first space before the first is the location of the Opencover.Console.exe file, which is used to execute it. Target is the unit Test command object that it will run, is a batch file, we use the Runnuit.bat file we just created (we all use the absolute path here, in case you don't have to bother with Jenkins). TARGETDIR is the working directory, Output is used to specify where the generated coverage report files are located. The –filter is used to filter the results of the unit test coverage. If not specified, it will also test the coverage of the code in the loaded NUnit library, which is not what we want. We currently only test the NunitTest.dll file, and you can replace it with your own file name.

Next we create a new freestyle project named Opencovertest, add a Windows Batch command step in the build column, and then specify the path to the local opcover.bat file in the command.

Note that when you might see that we actually created two bat, one is runnuit.bat and the other is opcover.bat here we didn't add Runnuit.bat in the step because it was referenced in the target parameter of Opcover.bat, When Opcover.bat executes, Runnuit.bat is executed first.

After adding the command, we click Save and then execute build now.

If the configuration succeeds, a successful build record is generated. Here is a pit:

If you look closely at the console out, you will see a statement like the following

where results (NUNIT3) saved as Testresult.xml is a runnuitt.bat generated log file, the following is Opcover generated, you can see is no resultsxxxxx

If you perform opcover.bat locally, the report is not the same as that generated on Jenkins, and the local execution is successful. This problem has gone through a toss-up to solve. The reason is that opcover to access the local DLL and PDB files when generating the code coverage report.

Registering-register user at local execution is OK, but in the Jenkins environment, it may be that the user is different from the Jenkins execution Environment user and the local default execution executable. This is not too clear for Windows permissions, here is only speculation.

Here's the solution:

Open cmd as an administrator, and then execute regsvr32 "E:\personal project\newtest2018\consoleapp1\packages\opencover.4.6.519\tools\x86\ OpenCover.Profiler.dll "

Note that OpenCover.Profiler.dll "in the packages directory of the project under the Opcoverxxx\tools directory, everyone's project absolute path may be different from mine, do not copy."

After performing the above operation, a popup prompt will be successful.

Then open Jenkins to execute build now, view console out, and you will see a log similar to the following

The report is now successfully generated. We open the locally generated results.xml to see the report generated by Opencover.

The opencover of Jenkins in conjunction with. NET Platform tools

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.