One of the most useful things you can measure in your code base is code coverage. Basically it means how many lines of your production code gets executed by your test suite. In theory developing with TDD should bring 100% coverage, because no code is ever written before there is a test for it. In practice code bases end up with parts that might be untested. For example, code-behind in WebForms is difficult to write tests for, which is why patterns such as Model–view–presenter should be used.
So 100% coverage is not necessarily worth the effort, but coverage reports can help you find problem areas in the codebase.
Most used coverage tool in .NET is prorbably NCover. However, I am going to talk about an open source alternative called OpenCover. Another tool called ReportGenerator is used to generate html-report from the coverage data.
Creating coverage data is just one command:
OpenCover.Console.exe –register:user -target:"NUnit\bin\nunit-console.exe" -targetargs:"src\Projects.Test\bin\Debug\Project.Test.dll /noshadow" -output:output.xml -filter:"+[Project*]* -[Project.Test*]*
The command above uses NUnit to run test project called Project.Test. Filters are used to include and exclude modules. In the example all modules and classes starting with Project are included except Project.Test, which is excluded. If the command completes succesfully it produces output file named output.xml.
OpenCover's output is not very readable, so it needs to be formatted into a report. ReportGenerator is a tool that can generate reports.
ReportGenerator.exe output.xml coverage
The command takes coverage data and creates html report in a directory called coverage.
That's it. Now you can hook those two commands to be run in nightly build and get a fresh coverage report every day.
Software professional with a passion for quality. Likes TDD and working in agile teams. Has worked with wide-range of technologies, backend and frontend, from C++ to Javascript. Currently very interested in functional programming.