Tuesday, July 26, 2005

Customized Coverage Stats with nCover

I've been fooling around with nCover in preparation for a Grok Talk at tomorrow's Dayton .NET Developers Group. nCover's a great unit test coverage tool which points out areas of your code base you have or don't have covered with unit tests. It's especially useful with the enhanced stylesheet by Yves Lorphelin and Jeff Key's nCoverBrowser, especially the latter. I generally write my unit tests in patterns: tests of borders (over and under any limits I've set), invalid data, valid data, and constructors. I was wondering if it was possible to have nCover give me stats for each of those groups. The answer's "Yes!" if you make use of nUnit's "Category" attributes. Here's a portion of a border test method:   [Test]   [Category("Border")]   public void CheckTextMaxSize()   {        PersonEntity pEnt = new PersonEntity();        try        {            pEnt.FName = BADVAL_LONGSTRING;        }        catch (ArgumentOutOfRangeException ae)        {            Assert.AreEqual(BADVAL_LONGSTRING, ae.ActualValue);        }     ... //remainder elided Note it's decorated with both [Test] and [Category("Border")] attributes. (Side note: I have waited so long to use the phrase "decorated with attributes." I think it makes me sound way smarter.) You can now use the /include switch of nUnit to run tests decorated only with specific [Category] attributes. Note that nUnit expects input files first with any option switches after. Now invoke nCover with the following command line, and please make careful note of the quotes around the /c arguments. ncover.console /w . /o Coverage-Border.xml /c "nunit-console" "TestDemo.UnitTests.dll /include=Border" The output file Coverage-Border.xml will hold coverage stats for all tests marked with the [Cagegory("Border")] attribute. (Sorry, just couldn't use "decorated" again.) OK, so this is slick and a neat trick, but I'm not really sure if it's useful in the real world or not. This sort of reminds me of Bing Crosby and Fred Astaire in Holliday Inn talking about the peach preserves Bing has brought from his farm. Neither know quite what to do with them: "Why they're great on..." "Yeah, or even on..." Puzzled silence follows. Let me know if you think this is something useful, especially if you're putting anything similar to use already.

No comments:

Subscribe (RSS)

The Leadership Journey