Sunday, April 8, 2012

Migrating from NUnit to xUnit

I recently started using xUnit, and I have been really enjoying it!

If you are currently using NUnit to write your unit tests, then it is not at all difficult to migrate to using xUnit. The philosophical difference between the two is simply this: with xUnit you need to think of your tests as objects, rather than of methods. Or if you prefer acronyms: put some more OOP into your TDD. *bu-dum, tish!*

Anyway, here is a visual representation of equivalent commands between NUnit and xUnit:

xUnit

NUnit

[NUnit.Framework.TestFixture]
public class TestFixture
{
  [NUnit.Framework.TestFixtureSetUp]
  public void TestFixtureSetUp()
  {
    // 1) Set up test fixture  -------->
  }
  [NUnit.Framework.TestFixtureTearDown]
  public void TestFixtureTearDown()
  {
    // 8) Tear down test fixture  ----->
  }
 
 
 
  [NUnit.Framework.SetUp]
  public void SetUp()
  {
    // 2) Set up TestA  --------------->
    // 5) Set up TestB  --------------->
  }
  [NUnit.Framework.Test]
  public void TestA()
  {
    // 3) Run TestA  ------------------>
  }
  [NUnit.Framework.Test]
  public void TestB()
  {
    // 6) Run TestB.  ----------------->
  }
  [NUnit.Framework.TearDown]
  public void TearDown()
  {
    // 4) Tear down TestA  ------------>
    // 7) Tear down TestB  ------------>
  }
}
 
public class TestData : IDisposable
{
 
  public TestData()
  {
    // 1) Set up test fixture
  }
 
  public void Dispose()
  {
    // 8) Tear down test fixture
  }
}
public class TestClass
  : IDisposable, Xunit.IUseFixture
{
  public TestClass()
  {
    // 2) Set up TestA
    // 5) Set up TestB
  }
  [Xunit.Fact]
  public void TestA()
  {
    // 3) Run TestA
  }
  [Xunit.Fact]
  public void TestB()
  {
    // 6) Run TestB
  }
 
  public void Dispose()
  {
    // 4) Tear down TestA
    // 7) Tear down TestB
  }
  public TestData TestData { get; set; }
  public void SetFixture(TestData data)
  {
    // 2.5) Set fixture data for TestA
    // 5.5) Set fixture data for TestB
    TestData = data;
  }
}
kick it on DotNetKicks.com

Happy testing!
~Tom

5 comments:

  1. Thank you for sharing.
    Why did you move from nUnit to xUnit? What did you gain?

    ReplyDelete
  2. Theory (or data driven) unit tests. Almost every unit test I make is written as a Theory; this encourages everyone on the team to continuously add more sample data to our tests. I absolutely love theories, and I can not say enough good things about them.

    More details: http://www.tomdupont.net/2012/04/xunit-theory-data-driven-unit-test.html

    ReplyDelete
  3. I'm considering a migration from an old NUnit based unit tests of mine to xUnit.

    Incidentally, at this time, I think NUnit supports Combinatorial, ability to compose your own parameter data sets, etc, even as of v2.6.4. However, I question some of the philosophical drivers motivating the move to v3.

    In contrast, while I don't mind adding an attribute decoration anyway, I do kind of enjoy the simplicity of the xUnit approach, however. Just utilize built-in features of the language to effect the test fixtures, etc. That's it. Plain and simple.

    At any rate, good to know. Thanks!

    ReplyDelete
  4. I'm not sure that Test Fixture Setup/TearDown as compared/contrasted with (Test) Setup/TearDown has quite the same connotation in xUnit as it does in NUnit. I could be wrong about that. Consider, setting up each TEST FIXTURE happens ONCE, where as SETUP happens for EACH test. Whereas, in xUnit Facts, or even Theories, are akin to tests. You wouldn't want to initialize the ctor EACH time, in terms of TEST FIXTURE, so called, in xUnit. Thoughts?

    ReplyDelete
  5. Hello bro tons of thanks for sharing with us.

    Extremely liked way you shared direct line to line comparison

    ReplyDelete

Real Time Web Analytics