PartCover

Written by

in

An Introduction to PartCover for .NET Code Coverage Code coverage is a vital metric in modern software development. It measures the percentage of your source code executed during automated testing. High code coverage provides confidence that your tests are thorough and that hidden bugs are less likely to slip into production. For .NET developers, numerous tools exist to measure this metric. While commercial solutions like dotCover or NCover are widely known, open-source alternatives offer great value without licensing costs.

One such tool is PartCover, an open-source code coverage utility designed specifically for the .NET framework. What is PartCover?

PartCover is a lightweight, open-source code coverage tool for .NET applications. It monitors the execution of your application during a test run and reports exactly which lines of code, blocks, and functions were executed.

Originally created as an alternative to expensive commercial coverage tools, PartCover targets the .NET Common Language Runtime (CLR). It injects itself into the runtime process to track execution paths at the intermediate language (IL) level. Key Features of PartCover

Line and Block Coverage: Tracks coverage at both the individual sequence point (line) level and the basic block level.

XML Reporting: Generates detailed coverage reports in a standard XML format, making it easy to parse and manipulate.

Build Pipeline Integration: Functions as a command-line tool, allowing seamless integration into automated build scripts like NAnt, MSBuild, or CruiseControl.NET.

Inclusion/Exclusion Rules: Offers robust filtering options to include or exclude specific assemblies, namespaces, classes, or methods from the coverage report.

XSLT Style Sheets: Includes default XSLT sheets to transform raw XML data into human-readable HTML reports. How PartCover Works

PartCover utilizes the .NET Profiling API. When you run your test runner (such as NUnit or MSTest) through PartCover, the tool acts as a profiler.

Interception: PartCover starts the target application or test runner process.

Instrumentation: As the CLR loads assemblies, PartCover modifies the IL code in memory to insert hooks (counters) at every sequence point.

Execution: Your automated tests run normally. When a hook is hit, PartCover records the execution.

Output: Once the process exits, PartCover compiles the tracking data into an XML file. Setting Up and Using PartCover

Because PartCover is a command-line tool, using it involves configuring an execution command that specifies your test runner, target assemblies, and output rules. 1. Basic Command Line Syntax A typical command-line invocation looks like this:

PartCover.exe –target “path\to\nunit-console.exe” –target-args “path\to\YourTests.dll” –output coverage.xml Use code with caution. 2. Using Configuration Files

For complex projects, passing parameters via the command line becomes unmanageable. PartCover supports XML configuration files to define settings cleanly:

C:\Program Files\NUnit\bin\nunit-console.exe C:\Project\bin\Debug\MyProject.Tests.dll C:\Project\Coverage\results.xml + [MyProject] - [MyProject.Tests] Use code with caution. In this configuration:

+ [MyProject] tells PartCover to measure coverage for all classes inside the MyProject assembly.

- [MyProject.Tests] excludes the test code itself from the coverage metrics, ensuring your statistics reflect actual business logic. Generating Human-Readable Reports

The raw XML output generated by PartCover contains precise data but is difficult to read manually. To view the results, you need to transform the XML.

ReportGenerator: A popular open-source tool that can read PartCover XML files and convert them into highly detailed, color-coded HTML reports.

XSLT Transformations: PartCover historically shipped with .xslt files. You can use standard command-line XSLT processors to convert the results.xml into a basic HTML summary. Limitations to Consider

While PartCover is a capable tool, it is important to understand its limitations:

Framework Compatibility: PartCover was primarily built for the classic .NET Framework (up to version 4.x). It lacks native, modern support for modern .NET (Core) 5/6/7/8+.

Maintenance Status: The project is largely legacy and no longer sees active development.

Performance Overhead: Because it instruments IL in memory, running tests under PartCover is significantly slower than running them normally. Modern Alternatives

If you are working on modern .NET Core or .NET 5+ applications, PartCover may not fit your ecosystem. Instead, look into these modern open-source alternatives:

Coverlet: The current standard for modern .NET. It integrates directly with the dotnet test infrastructure.

AltCover: Another excellent, highly compatible open-source cross-platform coverage tool. Conclusion

PartCover remains a foundational piece of open-source .NET history. For legacy .NET Framework applications, it provides a functional, cost-effective way to introduce code coverage metrics into a continuous integration pipeline. By understanding how to configure its rules and transform its XML output, developers can easily pinpoint untested code paths and improve overall software reliability. To help tailor this guide or explore further, let me know: Are you working with legacy .NET Framework or modern .NET?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *