Is it possible to use MSpec to test .net micro-framework projects?

307 Views Asked by At

I'd like to be able to use MSpec or another BDD/TDD framework to test code targeted at the .net micro framework. Unfortunately referencing a MF (micro-framework) assembly isn't possible from a non-MF assembly. It's a shame because it's just C# code and unit tests would be really useful in this scenario. Has anyone worked out a way to do unit testing (preferably with MSpec) for .net micro-framework projects?

2

There are 2 best solutions below

0
On BEST ANSWER

I downloaded the latest Machine.Specifications and moved all the code into a Micro Framework Class Library project. I got only 323 compile errors. The short list of things that would need to change...

  1. No extension methods
  2. No generics
  3. No LINQ
  4. No custom attributes
  5. No expression trees

Is it worth trying to rebuild MSpec on this .NET Framework family? Probably not. Is it something the authors/committers will want to keep up with? Probably not.


I tried customizing the project type GUID and CSharp import based on this goofy forum thread and ended up with a bunch of errors. Including the "unsupported by the compiler error".

Error 4 TestableMicroLibrary.Tests D:\TestableMicroLibrary\TestableMicroLibrary.Tests\MMP 0x81010009


I think you're going to have to roll your own. Use conventions and reflection (about the only thing remaining) for the test classes/methods. You can continue to use a behavior driven style, like test case class per fixture.

public class When_doing_something_neat
{
    public void It_should_frob_the_widget() 
    {
        Assert.IsTrue(_widget.IsFrobbed);
    }
}

And rebuild the whole Assert/Should library (remember, no extension methods!).

public static class Assert
{
    public static void IsTrue(bool thing)
    {
        if(!thing) throw new AssertionException("It is not true");
    }
}
0
On

Another option - which is a bit of a pain and may not suit - is to create a dummy 'Normal' .Net3.5/.Net4.0 project along side the microframework one. Since microframework C# is almost all source compatible, you can just add the source file to the dummy project also - i.e the source files would belong to both projects. Then, some minimal conditional compilation would be needed with #IFDEFS, for example around the Microsoft.Spot namespace inclusions.

At that point the dummy project is fully testable with any normal test framework NUinit, Specflow, whatever. Obviously this approach needs some degree of caution and care - watching out for host byte order issues etc, and of course both projects would have to be maintained in parallel.

The other option is to test via an Emulator project, which would be the more thorough approach. However the dummy project approach certainly can have benifits WRT rapid development, TDD etc.