Code Coverage with NCover

I can’t remember exactly what drove me to seriously look into it, but for the last two weeks, I’ve been using NCover to calculate the coverage of my unit tests. Boy, am I glad I finally did. I’ve added it to the list of Apps I Use Everyday, and you should too. You shouldn’t use the xsl file it gives you, though: it sucks; use this xsl file instead, by Yves Lorphelin. A good overview of how to setup NCover can be found here, as well as a preview of the good xsl file in action. I think my biggest beef with NCover is the pain involved in setting the damn thing up. I had to put both NCover and NUnit program directories in my path for it to start working. I also tried CoverageEye.NET, but I could not get the damn thing to work. There is almost no documentation for setting it up. But there is a good comparison of the two here; he likes CoverageEye better.

As far as I can tell, NCover is the best free tool that doesn’t muck up you’re project settings. (There is another code coverage tool called NCover at sourceforge that requires you to add a reference to it to use it. Um, no thanks.) Another thing I don’t like is there is no GUI. I’m a GUI guy: I like to look at pictures, shapes, you know: shiny shit; I’m a visual animal. However, I’m not entirely out of luck though: Jeff Key has written a quick little GUI wrapper for NCover. But, alas, I couldn’t get it to work either: It always throws a PathNotFoundException, even after I got NCover working on the command line. And, I note, found in my Google research for this post, and put here for future reference, that he has posted another NCover oriented tool: NCover Browser. I haven’t looked at it yet, but I will soon.

When I started with NCover, we had about 80% of our code covered by unit tests. It took about a day, maybe a bit more, to get that up to hover around 90%, where it sits now. I was surprised it took so long to get what seemed like an insignificant jump. I’m noticing that much beyond 90% isn’t providing much benefit. In other words, the effort of writing the test is not worth the benefit having the test gives me. So my goal now is to stay around 90%. Now that I’m using this tool, I’m finding that I write tests that cover all paths through a method automatically, making my efforts a tad more focused and efficient.

One thing that becomes glaringly obvious with this tool is that overriden GetHashCode() methods NEVER get called. In fact the only reason I override it is the warning I get if I override Equals() but not GetHashCode(). Consequently, I just call base.GetHashCode(). That’s one thing I’ve wondered about for a while. Anybody know?

FxCop Globalization Rules and Exception Constructors

Update:

Looks like I was wrong again. (You know what they say about assuming?) Reflector is an even greater tool than I first realized: not only does it show the classes in an assembly, it also shows the resources for the assembly; something my eye does not see and brain does not look for, because I just haven’t been in the situation to really use them. Anyway, it shows them and every assembly in the framework that I looked at (um, two of them) have resources that are filled with? Exception messages. Interestingly, I found a way around the FxCop violation, but you shouldn’t use it unless you really, really intend to fix it later. Do you? Ok, here it is:

… = new MyException( String.Format( System.Globalization.CultureInfo.CurrentCulture, “my exception message“ ) );

End Update

I’ve been using FxCop a lot lately to fix up some of the templates in CodeSmith and some of my code at work. I get this violation a lot:

Do not pass literals as localized parameters.

Most of the cases, all if I’m not mistaken, are for messages in Exception constructors. This got me wondering:

If I install the .NET framework in French (or German, or Italian, or Thai, or Chinese,  or Brazilian Portuguese or …) do I get stack traces from thrown exceptions in that language?

If the answer is no (that’s what I think it is; especially after inspecting the framework with Reflector), FxCop should check to see to which method the literal is being passed, shouldn’t it? And if it should, I should just ignore those violations when I pass a string to an Exception constructor.

This is a tough one: 30 jk.ca points to whomever gets it. I’ll update this post once the answer is found. Good luck. 🙂

Apps I use everyday

[Update: forgot a link to the excellent NUnit Gui Make Over. I figured to make up for it, I’ll link to it twice.]

I recently got an upgrade to a much faster machine at work. Doing so made me consider all the apps that I use daily for development. So here they are in no particular order:

  • RSSBandit: for the first two hours at work when I’m supposed to be working. Seriously, the best aggregator for the best price.
  • CLR Profiler: for profiling allocations in applications that run on the CLR.
  • Ethereal: network analysis tool that knows every network protocol known to man; very useful and very free.
  • TcpView: check out what ports are in use at a glance
  • TestDriven.NET: do TDD right in Visual Studio; and attach a debugger to the unit test case that is giving you some trouble
  • NUnit: to see that green, baby! I still use the NUnit GUI. Made over, of course.
  • VS 2003: duh!
  • FxCop: are you designing your libraries with the MS .NET design guidelines? You should be, and this will help you.
  • Regulator: excellent regular expression tool. Let’s you doodle.
  • Reflector: find out how MS did their thing with the Framework.
  • Codesmith: generate strongly-typed collections, data objects, or anything else with this template-based coding generator
  • Resharper: the only good refactoring tool for .NET. It’s not perfect but it’s the best out there. The only developer tool that I’ve been willing to shell out money for. (The rest on the list are all free; well VS isn’t but I got it for free.)
  • Notepad2 and Notepad++: two light weight text editors that do a lot more than Notepad.

Got any others that are indispensible?

Windows Socket Version 2 API error code documentation

[Update: Looks Mike, future PM of System.Net and all ’round genius, cleared things up in the comments. Thanks, Mike. He gets 20 jasonkemp.ca points! Save those up and you may get a toaster. Or a beer since I know you and you live in Victoria. Incidently, before I checked for comments to this post, I opened up MSDN help, typed in “socket“ without quotes and no filter, and found the error codes in a few links. Who knew? 🙂 Now we just have to correct that documentation UdpClient.]

So I’m trying to play with UdpClient from the System.Net.Sockets namespace in good ol’ .NET 1.1.

I’m using the code from the Receive() example in MSDN Help. Almost exactly. Only in my version, the fucking thing always throws a SocketException. Luckily the SocketExpection has an ErrorCode property so that I can “refer to the Windows Socket Version 2 API error code documentation in MSDN for a detailed description of the error.” And yet, I still don’t know what’s going wrong because as far as my local MSDN Library, MSDN for the Internet, and Google are concerned there is no such fucking Windows Socket Version 2 API error code documentation. (In fact, if Google is worth its salt, this post will begin coming up when that string is searched.) All I can use to debug is the stack trace. And it’s not my code that’s being difficult so break points won’t work. Could it be XP SP2? Don’t know; ’cause I can’t for the life of me find that info on MSDN either. I know it’s not Windows Firewall because I tried this code with the Firewall turned off. I haven’t used the excellent Reflector on this either, but I don’t see how that’ll help right now. And I know how to use Google, in case you were wondering! Sometimes Microsoft really pisses me off.

     1:         [STAThread]
     2:         static void Main(string[] args)
     3:         {
     4:             UdpClient udpClient = new UdpClient();
     5:  
     6:             IPEndPoint endPoint = new IPEndPoint(IPAddress.Any,  0);
     7:             try
     8:             {
     9:                 byte[] received = udpClient.Receive( ref endPoint );
    10:  
    11:                 string recvMessage = Encoding.ASCII.GetString(received);
    12:  
    13:                 Console.WriteLine("recvMessage = {0}", recvMessage);
    14:             }
    15:             catch ( SocketException e)
    16:             {
    17:                 Console.WriteLine(e);
    18:             }
    19:             catch( Exception e )
    20:             {
    21:                 Console.Out.WriteLine("e = {0}", e);
    22:             }
    23:  
    24:             Console.ReadLine();
    25:         }

It always throws an exception on line 9. Can anyone tell me why this is failing? 10 jasonkemp.ca points, and my eternal gratitude, to anyone who can. 10 jasonkemp.ca points to anyone can point to good Winsock 2 API documenation also.