Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit vs. xUnit

Tags:

c#

.net

nunit

xunit

At the time of writing this answer, the latest NUnit version is v3.5 and xUnit.net is v2.1.

Both frameworks are awesome, and they both support parallel test running (in a different way though). NUnit has been around since 2002, it's widely used, well documented and has a large community, whereas xUnit.net is more modern, more TDD adherent, more extensible, and also trending in .NET Core development. It's also well documented.

In addition to that, the main difference I noticed is the way that xUnit.net runs the test methods. So, in NUnit, we've got a test class and a set of test methods in it. NUnit creates a new instance of the test class and then runs all of the test methods from the same instance. Whereas xUnit.net creates a new instance of the test class for very each of the test methods. Therefore, one cannot use fields or properties to share data among test methods which is a bad practice, as our test methods would be dependent to each other which is not acceptable in TDD. So if you use xunit.net, you could be sure that your test methods are completely isolated.

If you're willing to share some data among your test methods though, xUnit will let you do so. Therefore, by default all test methods are completely isolated, but you can break this isolation in specific cases intentionally. I fancy this attitude, that's why I like it better.


You're confusing the name of a single tool (xUnit.net) with the name of a whole class of unit testing frameworks (xUnit, the x referring to a language/environment, e.g. JUnit, NUnit, ...).


xUnit Pros:

xUnit follows a new concept by avoiding the old "SetUp" and "TearDown" methods. It forces us to use IDisposable and a constructor as we should do as .NET developers. Also xUnit has a clear context sharing concept.

xUnit Cons:

Availability to get the test context is not implemented yet.


One benefit of xUnit is that it finds tests in different classes and runs them in parallel. This can save a lot of time if you have many test cases.

You can of course turn this off, or control its operation (number of threads, threads per class, tests per assembly, etc).

Check out this sample solution with two test projects, one using xUnit, the other NUnit.

You can read more about parallel tests in xUnit here.