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.

Comments (3) left to “Simple BDD Reporting with NUnit”

  1. Ben Scheirman wrote:

    You still need to tie in to see if these are actually passing or not!

    Have you used RSpec? It’s beautiful how it arranges your describes/shoulds (they can be infinitely nested).

    It also shows you which ones FAIL, and on an ANSI terminal window, you can even see reg/green colors.

  2. James Thigpen wrote:

    I haven’t used RSpec yet, it’s on my gigantic list of things to check out. *need more hours!*

    And as far as whether the tests are passing or not, I still use r# for my test runner, and I kind of see the passing of the tests as an orthogonal concern to what my specs are/what the report tells me, so I’m not sure if it’s worth the effort.

    I could be wrong.

  3. Deran wrote:

    I played with NBehave for a bit and it seemed pretty cool
    http://nbehave.org/

Post a Comment

*Required
*Required (Never published)