Comparing the MSTest and Nunit Frameworks


I haven’t seen much information online comparing the similarities and differences between the Nunit and MSTest Frameworks.  Here I will define the similarities and some of the differences.  If there is anything else which you come upon, please do add it to the comments.









































MSTest Attribute


NUnit Attribute


Purpose


[TestMethod]


[Test]


Indentifies of an individual unit test


[TestClass]


[TestFixture]


Identifies of a group of unit tests, all Tests, and Initializations/Clean Ups must appear after this declaration


[ClassInitialize]


[TestFixtureSetUp]


Identifies a method which should be called a single time prior to executing any test in the Test Class/Test Fixture


[ClassCleanup]


[TestFixtureTearDown]


Identifies a method in to be called a single time following the execution of the last test in a TestClass/TestFixture


[TestInitialize]


[SetUp]


Identifies a method to be executed each time before a TestMethod/Test is executed


[TestCleanUp]


[TearDown]


Identifies a method to be executed each time after a TestMethod/Test has executed


[AssemblyInitialize]


 N/A


Identifies a method to be called a single time upon before running any tests in a Test Assembly


[AssemblyCleanUp]


 N/A


Identifies a method to be called a single time upon after running all tests in a Test Assembly


 The order of execution is similar in both frameworks, but there are some differences between the two:



  • In Nunit, tests are not executed in parallel.  Rather, it appears that all tests execute on a single thread.  In MSTest, each test is instantiated on a separate thread, this results the runs being interleaved.  Therefore, if test A depends on test B for its success, it likely will fail as test B will likely start running as test A is running.

  • The time at which ClassCleanUp/TestFixtureTearDown executes is different between the two frameworks.  In Nunit, TestFixtureTearDown is executed immediately following the completion of the last test in a TestFixture or after TearDown if the attribute exists for the test in question.  In the Whidbey implementation of MsTest, ClassCleanUp executes at the end of a test run, before AssemblyCleanUp but not necessarily immediately after the last test in a TestClass has completed executing.

  • In Whidbey, support for test class inheritance was missing.  In Nunit, it is fully supported.  This will be rectified in Orcas.

I should also mentioned that in MsTest, TestContext exists for passing information about the test run.  There is no equivalent in Nunit tests.  This can serve as a handy tool for pulling information from datasources on the disk to the unit tests, as well as other uses.  More can be read about it here.

Comments (16)

  1. buckh says:

    Are there plans to add support for NUnit’s Category attribute?

    http://nunit.org/index.php?p=category&r=2.4

    Buck

  2. Naysawn Naderi has a matrix of attributes between NUnit and MSTest . To balance MbUnit in this matrix,

  3. I just came across a great blog entry that compares the different attributes in both MSTest and NUnit.

  4. Naderi é PM (Program Manager) do VSTS Test Team e escrever bastante sobre testes unitários utilizando

  5. nnaderi says:

    From Buck: "Are there plans to add support for NUnit’s Category attribute?"

    We have been discussing this.  I think that it would be quite beneficial.  Do you prefer it to the test lists that we have implemented?

  6. sdhebert says:

    What is the MSTest equivilant of ‘Explicit’.  The combination of Explicit and Category is extremely helpful.

  7. Now, I’m not claiming to be anything remotely close to a Test-Driven Development expert – heck, I only

  8. Ross Bradbury says:

    I think that [AssemblyInitialize] and

    [AssemblyCleanUp] in MSTest correspond to

    [SetUp] and [TearDown] in the [SetUpFixture] of NUnit.

  9. Summary Motley: Just use a batch file to execute unit tests – it’s easy and quick. Maven: Use a unit

  10. Summary Motley: Just use a batch file to execute unit tests – it's easy and quick. Maven: Use a unit

  11. acyment says:

    MBUnit’s TestSuiteFixture is a TestClass than can be populated at runtime (e.g. by fetching data from files in folders). Only way I know of doing that with MSTest is by adding an MSBuild task that runs before project compilation, that generates source code (i.e. alla CodeSmith), that will later be compiled by CSC and finally detected as a test by MSTest.

  12. Subrahmanyam K says:

    Some major differences in MSTest compared to NUnit is that:

    1) Inheritance is not supported if the base class is in different assembly

    2) ClassInitialize and ClassCleanup attribute methods MUST be static which enforces a lot of restriction in actually using those attributes

  13. Mahol25 says:

    Hi. I'm wondering about why support for inheritance of test classes is still not supported?? I wrote a question about it on stackoverflow.com, perhaps somebody reads this and can answer it:

    stackoverflow.com/…/is-defining-testmethods-in-test-base-classes-not-supported-by-mstest

    Thanks

  14. Santosh says:

    This might help:

    Unit testing frameworks: MSTest v/s NUnit @ http://santats.blogspot.com/

  15. bahar ghadami says:

    thanks,Actually [TestCleanUp] should be [TestCleanup] and

    [AssemblyCleanUp] should be [AssemblyCleanup].

  16. Sutikshan says:

    Why One Sided review?

    MSTest still don't have "TestCase" attribute which NUnit has, it makes calling the same test method several times with different parameters, very easy.