CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Patrick Smacchia [MVP C#]


Are you sure added and refactored code is covered by tests?

I would like to make a point here on the fact that NDepend can answer what is maybe the most important question for an agile programmer:

Does added and refactored code is covered by tests?

There are 4 CQL default rules to address this need:

// <Name>Method where code was changed partially covered by tests</Name>
WARN IF Count > 0 IN SELECT METHODS WHERE 
  
PercentageCoverage < 100 AND PercentageCoverage > 0 AND
  
CodeWasChanged 
  
ORDER BY PercentageCoverage DESC , NbLinesOfCodeCovered , NbLinesOfCodeNotCovered 
 

// <Name>Method added partially covered by tests</Name>
WARN IF Count > 0 IN SELECT METHODS WHERE 
  
PercentageCoverage < 100 AND PercentageCoverage > 0 AND
  
WasAdded  
  
ORDER BY PercentageCoverage DESC , NbLinesOfCodeCovered , NbLinesOfCodeNotCovered 

// <Name>Method where code was changed not covered at all</Name>
WARN IF Count > 0 IN SELECT METHODS WHERE 
PercentageCoverage == 0 AND CodeWasChanged ORDER BY NbLinesOfCode DESC 


// <Name>Method added not covered at all</Name>
WARN IF Count > 0 IN SELECT METHODS WHERE 
PercentageCoverage == 0 AND WasAdded ORDER BY NbLinesOfCode DESC 


Of course, these rules can be refactored at whim, for example, to have just one rule that tells about all added or refactored method that are not covered by tests:

// <Name>Method added or refactored not fully covered by tests</Name>
WARN IF Count > 0 IN SELECT METHODS WHERE 
  
PercentageCoverage < 100 AND
  
(CodeWasChanged OR WasAdded)
  
ORDER BY PercentageCoverage DESC , NbLinesOfCodeCovered , NbLinesOfCodeNotCovered 

 

I wanted to make this point because I figured out that my friend Jason Gorman, which is certainly an advanced NDepend user, wasn't sure about this possibility (Identifying High Risk .NET Code With NDepend).

 

Why knowing if added or refactored code is covered is so important? Because if you can answer yes each time you are about to release a new version you'll end up after N iterations (N not too large) with a code well tested. As a consequence the number of bug will decrease, iterations after iterations, and a lot of time spent doing manual testing will be saved.

Is it pure theory? I can't answer, my common sense say yes it works, however I am not a theorist but a programmer. But I can attest that since we applied this methodology on the development of NDepend, the number of bug reported has decreased dramatically and our productivity increased significantly.

 

Concretely, these CQL rules sits on 2 features of NDepend that are:

Build comparison: the ability to compare 2 builds and to ask for which part of the code have been added/refactored/removed.

 

Test coverage data import from NCover™ or VSTS™ the possibility to ask for code fully, partially or not covered by tests.

And as all NDepend features, this possibility to know about refactored/nont-tested code can be harnessed from the VisualNDepend UI but also from the CI build process by customizing NDepend project properties, as shown below:


 

 



Comments

Dew Drop - July 10, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - July 10, 2008 | Alvin Ashcraft's Morning Dew

# July 10, 2008 8:09 AM

Arjan`s World » LINKBLOG for July 11, 2008 said:

Pingback from  Arjan`s World    &raquo; LINKBLOG for July 11, 2008

# July 11, 2008 3:43 PM

Useful Links #9 | GrantPalin.com said:

Pingback from  Useful Links #9 | GrantPalin.com

# July 13, 2008 6:42 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Patrick Smacchia

Patrick Smacchia is a Visual C# MVP involved in software development for over 15 years. After graduating in mathematics and computer science, he has worked on software in a variety of fields including stock exchange, airline ticket reservation system as well as a satellite base station at Alcatel. He's currently a software consultant and trainer on .NET technologies as well as the lead developer of the tool NDepend which provides numerous metrics and caveats on any compiled .NET application. He is the author of Practical .NET2 and C#2, a .NET book conceived from real world experience with 647 compilable code listings. Check out Devlicio.us!

Our Sponsors

Free Tech Publications