Customize NUnit by yourself (3): integrate ncover to generate code coverage report (zhuan)

Source: Internet
Author: User
Tags net code coverage xsl file

As an important part of software quality assurance, test code coverage rate is an important criterion for evaluating unit tests. Now that you have compiled an automatic regression unit test for your code, you certainly want to know if all the code has been tested. The complete test code coverage can be said to be one of the decisive factors of the zero defect software.

Speaking. net code coverage statistics software. When NCover is well-known, we use it with NAant and Nunit, which is very convenient in the field of daily building and automated testing, as I mentioned earlier, it is easier for many programmers to integrate and use the functions they want to conveniently and quickly call. Therefore, I integrate Ncover into my Nunit.

Because the development is still based on fw1.1, I only use the earlier version of NCover1.3.3, but I have not found any problems. I don't know why. I didn't find the source code for version 1.3 On the NCover website. I only need to decompile it. After all, it is much easier to directly analyze and modify the source code. In the NCover system, Coverlib. dll is the most important component. It is a com component and needs to be registered and used with regsvr32. Of course, if you use the installation package, it will be automatically installed.

So what does. net do? Let's look at the code that calls the Ncover implementation function:

ProfilerSettings ps = new ProfilerSettings ();

Ps. CommandLineExe = "nunit-console.exe ";

Ps. CommandLineArgs = this. _ testLoader. TestFileName;

Ps. CoverageFile = Path. Combine (new FileInfo (this. _ testLoader. TestFileName). Directory. FullName, "coverage. xml ");

ProfilerDriver driver1 = new ProfilerDriver (ps );

Driver1.Start ();

Driver1.WaitForExit ();

System. Diagnostics. Process. Start (ps. CoverageFile );

NCover must be passed in an executable file to check which code is called. The referenced component to be checked must have a pdb debugging file; otherwise, it will not be analyzed. In the above code, I passed in the nunit console program, asked him to run the current test dll, and then set the output file to the directory of the tested component, enable the nunit-console process. After the process is completed, a converage is generated. the xml file is generated. You can view the statistical results of this file.

What happened in the middle. Who has done it all ?. Net code is nothing special:

Public void Start ()

{

ProcessStartInfo info1 = new ProcessStartInfo (this. _ ps. CommandLineExe, this. _ ps. CommandLineArgs );

Info1.UseShellExecute = false;

Info1.ErrorDialog = false;

Info1.CreateNoWindow = true;

Info1.RedirectStandardOutput = true;

Info1.RedirectStandardInput = true;

Info1.RedirectStandardError = true;

If (this. _ ps. WorkingDirectory! = Null)

{

Info1.WorkingDirectory = this. _ ps. WorkingDirectory;

}

This. SetEnvironment (info1.EnvironmentVariables );

This. _ proc = Process. Start (info1 );

}

I made some changes to the ncover code so that the new process can run in the background. Otherwise, a command line will pop up on the nunit-console, which is quite uncomfortable. From the code above, we can see that the system only sets the initial state and environment variables of the process, and overwrite statistics and xml file generation are completed. Let's take a look at the environment variable configuration code:

Private void SetEnvironment (StringDictionary env)

{

Env ["Cor_Enable_Profiling"] = "1 ";

Env ["Cor_Profiler"] = "CvrLib. CoverageProfiler ";

Env ["CoverageAssemblies"] = this. _ ps. Assemblies;

Env ["CoverageLog"] = this. _ ps. LogFile;

Env ["CoverageXml"] = this. _ ps. CoverageFile;

If (this. _ ps. Debug)

{

Env ["CoverageDebug"] = "1 ";

}

If (this. _ ps. VerboseLog)

{

Env ["CoverageVerbose"] = "1 ";

}

If (this. _ ps. DisableSetIL)

{

Env ["CoverageDisableSetIL"] = "1 ";

}

If (this. _ ps. JITLog)

{

Env ["CoverageLogJit"] = "1 ";

}

If (this. _ ps. NoLog)

{

Env ["CoverageNoLog"] = "1 ";

}

}

In fact, this part of the code is to save the relevant variables so that the real owner-ConverLib. dll can process them. ConverLib. dll is responsible for tracking and recording code based on the value of environment variables, and generating relevant XML files. To make xml readable, an xsl file must be automatically copied.

Finally, we add a code coverage report on the tools menu:

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.