Vous êtes sur la page 1sur 4

MSTest Vs NUnit

 Nunit contains a [TestCase] attribute that allows implementing parametrized tests. this does
not exist in msTest
 MsTest's ExpectedException attribute has a bug where the expected message is never really
asserted even if it's wrong - the test will pass.
 Nunit has an Assert.Throws API to allow testing an exception on a specific line of code
instead of the whole method (you can easily implement this one yourself though)
 Nunit contains a fluent version of Assert api (as already mentioned - Assert.That..)
 Nunit is much faster
 NUnit can run tests in 32 and 64 bit (MSTest only runs them in 32 bit IIRC)
 NUnit allows abstract classes to be test fixtures (so you can inherit test fixtures). MsTest does
not.
 NUnit allows non public classes to be test fixtures (as of the latest version)
 NUnit was created SOLELY for the idea of unit testing. MsTest was created for Testing - and
also a bit of unit testing.
 NUnit contains PNunit (running prallel tests with Nunit). MsTest only adds this ability in vs
2010
 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.
 MSTest is integrated with VS so it'll be easy to use. NUnit will require third party tools (some
are free, some are paid).
 VS will give you Code Coverage in MSTest. NUnit requires DotCover (which is paid tool).
 MSTest has a option to execute your tests in parallel if they don't dependent on each other.
This isn't a feature that NUnit provides.
 NUNit has TestCaseSourceAttribute which helps you to achieve parametrized test cases but
in MSTest you'll need DataSourceAttribute which would be in xml file and will be difficult to
manage when you have complex logic in method.
 NUnit is faster as compared to MSTest.
 Suite attribute - can aggregate tests and execute them separately (useful for large projects
with fast and slow tests for example)
 Readable Assert method, e.g. Assert.AreEqual(expected, actual) vs Assert.That(actual,
Is.EqualTo(expected))
 NUnit has frequent version updates - MS-Test has only one per VS version.
 Many integrated runners including Resharper and TestDriven.NET
 Expected exception message assertion - can be done using attribute in NUnit but must be
done using Try-Catch in MS-Test
 [TestCase]! NUnit allows for parameter-ized tests.
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

Startup
MsTest
Needs a specific project type, ‘Test Project’, which means you will automatic have the right reference
and the correct project type guid.

NUnit
Does not require any specific project type, but most of the time people will add a class library to
separate their code from their unittests. You need to reference the nunit.framework.dll yourself. But if you use
nuget this will be done for you automatic while installing NUnit.

Conclusion: If you go for out of the box experience i would say MsTest wins this section, otherwise i would say
a draw between MsTest and NUnit.

Attributing classes and methods


Both frameworks separate the apples from the pears trough attributes.

MsTest
AssemblyInitialize, AssemblyCleanup are two special attributes that can be used to bootstrap or
teardown your test assembly
TestClass with ClassInitialize -> TestInitialize -> TestMethod -> TestCleanup -> ClassCleanup

NUnit
TestFixture with TestFixtureSetup -> Setup -> Test -> TearDown -> TestFixtureTearDown
Conclusion: Apart for the missing AssemblyInitialize and Cleanup both frameworks are on par with each other,
i must admit MsTest wins this section also, but i would like to point out the fact that having to bootstrap or
teardown your test assembly is probably a smell that you are doing something wrong.

Categorize, Ignore, …
MsTest
TestCategory
Ignore
Timeout
ExpectedException
NUnit
Category
Ignore
Timeout
ExpectedException : Although this attribute has been deprecated and replaced by Assert.Throws it
has more options than the MsTest version

Conclusion: I think both frameworks are on par with each other if you look at the basic attributes, but NUnit
still has some gems, like Explicit, which will make the test run only when explicitly told so, but there are many
more like SetCulture and SetUiCulture. In terms of language completeness i think NUnit wins this section.

Assert
MsTest
Assert, StringAssert and CollectionAssert
NUnit
Assert, StringAssert and CollectionAssert, Exception Asserts ( Assert.Throws, Assert.Catch etc), File
Asserts, Directory Asserts
Conclusion: Again the basics are present in both frameworks but the language richness of NUnit makes it to
win this section.

Extensibility
Clearly NUnit wins this section see http://www.nunit.org/index.php?p=extensibility&r=2.5.10

Data driven tests


MsTest
Takes a file based approach with DataSource to provide testing values, which is nice but you will
always have to need to add csv, excel or xml data file
NUnit
Does not have a build in support for filebased datasources but it does have many attributes that can
be used to provide values in your test code directly like TestCase and TestCaseSource

Conclusion: This section depends on personal preference. For me i prefer the easy way of NUnit because i see
no real point in using a file based approach

Conclusion:

Both the frameworks will probably do 99% of the things that you need to test on a day-to-day basis. 3 or 4
years ago the lack of certain features in Ms Test made NUnit a better consideration. Today, that gap has
narrowed so the choice between NUnit and MsTest is less.
At least in combination with Resharper, MS Test is useful. And I hope that they finally find out how the test
runner should be written and won't do this kind of breaking changes when we update Visual Studio next time.
Regardless of which of the unit testing frameworks you use, you’re going to be getting all the basics. However,
there are a few differences between them that I hope I’ve highlighted so you can choose the right one for your
project. Whether it’s the convenience of Microsoft’s built in unit testing framework, the solid and well proven
status of NUnit theres always something out there that will give you exactly what you need!

Vous aimerez peut-être aussi