Friday 2 April 2010

xUnit.net vs MSpec?

For my personal projects I use MSpec as a testing framwork. MSpec had it's advantages and disadvantages. Here is an example of a test I wrote.
   37 public class When_setting_the_state_of_a_host
   38 {
   39     private static HostCheckResultCommand command;
   40     private static HostCheckResultCommandHandler commandHandler;
   41     private static IRepository _repository;
   42     private static Host _host;
   43 
   44     Establish context = () =>
   45     {
   46         _host = new Host("asterix");
   47         _repository = MockRepository.GenerateMock<IRepository>();
   48         _repository.Expect(x => x.GetById<Host>(""))
   49                    .IgnoreArguments()
   50                    .Return(_host);
   51         _repository.Expect(x => x.Add<Host>(null)).IgnoreArguments();
   52         command = new HostCheckResultCommand("asterix", HostState.Up);
   53         commandHandler = new HostCheckResultCommandHandler(_repository);
   54     };
   55 
   56     Because of = () => commandHandler.Execute(command);
   57     It should_have_a_HostStateChanged_event = () => ((IEventProvider)_host).GetChanges().First().ShouldBeOfType<HostStateChanged>();
   58     It the_event_should_have_HostState_Up = () => ((IEventProvider)_host).GetChanges().First<HostStateChanged>().NewState.ShouldEqual(HostState.Up);
   59 }
MSpec has a lot of advantages:
  • Once you are used to lambda expresions, it is relative easy to read.
  • Since every test is in it's own class, tests can't conflict with each other.
  • Every test statement (It) is named and run even when another test statement failes.
Disadvantages:
  • Everything in the class should be static, since the use of anonymous functions.
  • Can be difficult to get started.
At work I started using xUnit.net as testing framework.
    1 public class A_Person : XPODatabaseTestFixture
    2 {
    3     [Fact]
    4     public void can_be_created()
    5     {
    6         Person person = ObjectSpace.CreateObject<Person>();
    7         Assert.NotNull(person);
    8     }
    9 }
Well we don't compare the tests, because they are completly different, but we can compare the frameworks.
The main advantages xUnit.Net has are:
  • Every test is performed isolated, for every test method a new instance of the object is created.
  • The test runner is very fast, tests are run in different threads, multiple at a time.
  • Can use the constructor for common setup and IDisposable for teardown. (Setup and teardown contain logic that is run before and after each test. other frameworks use the [Setup] and [Teardown] attributes)
The only disadvantage I found in both (and other) test framework is that there is no VisualStudio testsrunner provided. You either need to use the extenal test runner they provide, or a tool like Testdriven.Net, Resharper or DevExpress Coderush.

Although I like MSpec very much, I am going to dump it for xUnit.net. With xUnit.net I don't have to think about making my members static.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.