Simple BDD Reporting with NUnit

I do BDD with just plain NUnit. I haven’t yet invested the time to mess with any of the frameworks like MSpec or whatever.

I use a ContextSpecification class that I probably ripped off from someone long ago. Everyone in the BDD demos always have fancy reporting though, and I got jealous today so I wrote my own reporting tool.

   1: using System;
   2: using System.Reflection;
   3: using NUnit.Framework;
   4:  
   5: namespace Falcon.Server.Tests
   6: {
   7:     [TestFixture]
   8:     public class ReportGenerator
   9:     {
  10:         [Test]
  11:         [Ignore("Cuz I said so")]
  12:         public void GenerateBddReport()
  13:         {
  14:             var assembly = Assembly.GetExecutingAssembly();
  15:  
  16:             foreach (var type in assembly.GetExportedTypes())
  17:             {
  18:                 if (type.Name.StartsWith("when_"))
  19:                 {
  20:                     Console.WriteLine("{0}:", type.Name.Replace('_', ' '));
  21:                     foreach (var methodInfo in type.GetMethods())
  22:                     {
  23:                         if (methodInfo.Name.StartsWith("should_"))
  24:                         Console.WriteLine("\t{0}", methodInfo.Name.Replace('_', ' '));
  25:                     }
  26:                     Console.WriteLine();
  27:                 }
  28:             }
  29:         }
  30:     }
  31: }

Yup. That’s it.  Reflect over the current assembly, and grab everything that starts with “should_” and “when_” and spray it all over my screen.

Now you could take that and gussy it up with html like some 2-bit floozy, but I see no need. I made it a test that I can just run anytime I want from R# because I didn’t want the weight of some silly assembly/exe file laying around. This was easy.

And it was amazingly valuable.  Getting a report that looks like this:

   1: when daemons are started and stop is called:
   2:     should stop listening for client connections
   3:     should stop listening for bridge connections
   4:     should stop listening for gateway connections
   5:     should raise stopped event

makes it so much easier to find specs that don’t make sense or are bad or whatever.

All in all, I highly recommend running a report such as this now and then to give you a view of what you’re doing.