Sunday, March 01, 2009

Code Coverage Delta with Team System and PowerShell

My last post documented my first venture into working with Visual Studio's Code Analysis output with PowerShell to find classes that need more testing. Since then I've taken the idea further to analyse how coverage has changed over a series of builds from TFS.

What resulted is a PowerShell script that takes, as a minimum, the name of a TFS Project and the name of a Build Definition within that project. The script will then, by default, locate the two most recent successful builds, grab the code coverage data from the test runs of each of those builds and output a list of classes whose coverage has changed between those builds, citing the change in the number of blocks not covered.

Additional parameters to the script allow partially successful or failed builds to be considered and also to analyse coverage change over a span of several builds rather just two consecutive builds.

The primary motivator behind developing this script was to be able to identify more accurately where coverage was lost when a new build has an overall coverage percentage lower than the last. This then helps to locate, among other things, where new code has been added without testing or where existing tests have been deleted or disabled.

A code base with a strong commitment to the Single Responsibility Principle should find class-level granularity sufficient but extending the script to support method-level reporting should be trivial given that the Coverage Analysis DataSet already includes all the required information.

The script requires the Team Foundation Server PowerShell Snapin from the TFS October 2008 Power Tools and the Visual Studio Coverage Analysis assemblies presumably available in Visual Studio 2008 Professional or higher. These dependencies only support 32-bit PowerShell so my script unfortunately suffers the same constraint.

Download the script here, and use it something like this:

PS C:\TfsWorkspace\> C:\Scripts\Compare-TfsBuildCoverage.ps1 -project Foo -build FooBuild | sort DeltaBlocksNotCovered

Tuesday, March 03, 2009 5:41:15 AM (Cen. Australia Standard Time, UTC+09:30)
Thanks Jason! This was great timing!

Our environment is VSTS 2008, TFS 2008, MSBuild, PowerShell V2 (CTP3)...

Below are the tasks to one of my stories during our current Agile Scrum sprint:

Build code deltas:
1. List of files in the changesets (get list of files changed after a specific label and/or build)
2. List of defects in the changesets (DE***) - we use http://RallyDev.com to track defects...all defect comments have a "DE" prefix...
3. List of tasks in the changesets (TA***) - we use http://RallyDev.com to track tasks...all defect comments have a "TA" prefix...

Do you have any advice?
Ken Bartell
Comments are closed.