Blue flashes
04
Dec 09

MSpec

Here at Perfect Image we have just switched from using Test Driven Development (TDD) to Behaviour Driven Development (BDD). The library we have chosen to use for writing our BDD tests is Machine Specifications (MSpec). It’s a great framework and if you are interested in doing some BDD yourself you should take a look at it here and download it from Github here.

This was actually the first time I had used Git and it took some getting used to as a SVN user but after a little resistance (and moaning) I found it’s actually pretty easy to use. Don’t be put off by the command line interface, it does have a GUI but I actually found it best using bits of both. There are plenty of tutorials out there on the web to help you out too.

MSpec includes its own test runner whereas before we used MBUnit. We had no problems wiring it into our NAnt build scripts to work with NCover. It even produces a nice HTML output of the results from the tests ran. The only issue we had was that it did not have the option to produce an XML output which we needed to be able to display reports in Cruise Control .Net - our Continuous Integration server.

My team leader James had looked at the problem a little and was able to point me in the right direction to start off, which saved me having to track down the significant code in the project. What I ended up having to do was add an option to the runner for an XML output and then add the functionality of mapping the results to XML. Luckily, it is very similar to how the HTML is produced so I was able to use that as a skeleton to base my work off. The XmlWriter made this very easy to do:

public void Render(XmlWriter reportBuilder, Dictionary<string, List<ContextInfo>> contextsByAssembly)  
{
      reportBuilder.WriteStartDocument();
      reportBuilder.WriteStartElement("MSpec");


      if (_showTimeInfo)
      {
             RenderTimeStamp(reportBuilder);
      }


      RenderAssemblies(reportBuilder, contextsByAssembly);


      reportBuilder.WriteEndElement();
      reportBuilder.WriteEndDocument();
    }


...


private void RenderAssemblies(XmlWriter reportBuilder, Dictionary<string, List<ContextInfo>> contextsByAssembly)
{
      contextsByAssembly.Keys.ToList().ForEach(assembly =>
                                                 {
                                                   reportBuilder.WriteStartElement("assembly");
                                                   reportBuilder.WriteAttributeString("name", assembly);
                                                   RenderConcerns(reportBuilder, contextsByAssembly[assembly]);
                                                   reportBuilder.WriteEndElement();
                                                 });
}

The XML structure itself was very simple. Basically, all you need are nodes for assemblies, concerns, contexts and specifications. Each of these has a name with the specification also having a status to show whether it passed or failed. I also added a section for displaying the time when the report was generated so that it looked like this:

<MSpec>
<generated>
<date>23 October 2009</date>
<time>17:37:05</time>
</generated>
<assembly name="MyAssembly">
<concern name="MyClass">
<context name="When doing something">
<specification name="should display something" status="passed" />
<specification name="should also do something else" status="passed" />
</context>
</concern>
</assembly>
</MSpec>

Now that we had an XML output to work with, an XSLT was needed to create the report page on the Cruise Control project page plus another one to produce a little summary box for the dashboard. Being the first XSLTs I had written, they were far from being works of art but they were close enough to the original HTML output, give or take some questionable colour choices (James made me pick magenta, honestly). All that was left to do was to tell Cruise Control to add the contents of the XML to its log file and display the summary and full report using the XSLTs which were uploaded to the server.

This is how the summary looks on the dashboard:

MSpec Summary

And here’s how the report page looks:

MSpec Summary

At this point we were finally in a position where MSpec gave us everything that we had when we were running TDD tests on MBUnit and so we thought it would be a good idea to contribute back to the community. 

This is where I was really impressed by Git. I was easily able to make a branch of the project on Github and then commit my changes to this branch. Then it was simply a case of sending a pull request to the owner of the original repository, Aaron Jensen, and waiting for him to respond. A few days later, I received a message from Aaron saying he liked my additions and after I corrected some differences with the formatting, they were added to the trunk.

You can now see for yourself if you are running the latest version of MSpec. Just add “--xml <filepath>” to your MSpec command. The XSLT files were added in the misc folder if you wish to add the reports to your Cruise Control server also.

Barry