NUnit vs. xUnit

C#.NetNunitXunit

C# Problem Overview


What are the differences between NUnit and xUnit.net? What's the point of developing two of them, not only one?

I've read that xUnit is being developed by inventor of NUnit:

> xUnit.net is a unit testing tool for the .NET Framework. Written by > the original inventor of NUnit

On the other hand:

> NUnit is a unit-testing framework for all .Net languages .. the > current production release, version 2.6, is the seventh major release > of this xUnit based unit testing tool

So where is the truth?

C# Solutions


Solution 1 - C#

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 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.

Solution 2 - C#

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, ...).

Solution 3 - C#

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.

Solution 4 - C#

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.

Solution 5 - C#

There's one feature that makes me switching from XUnit (2.x) to NUnit (3.x), is that:

XUnit doesn't work with Console.WriteLine(), while NUnit does.

I can't describe how frustrated I am when I found that there's no easy way to get Console.WriteLine working in XUnit, especially when I'm trying get a short piece of code to work.

I think this is a standard benchmark user case that you should always make standard output work with your testing framework. I know it's not good practice, I know there're alternatives like output handler and stuff. Users trying out Console.WriteLine are especially new users, and failing to print anything to screen is very, very disappointing and frustrating.

Solution 6 - C#

While this was asked 10 years ago, the situation was really changed.

Here is good full video which compares xUnit, NUnit and MSTest with code examples: https://www.youtube.com/watch?time_continue=3654&v=rLbF8u46tfE&feature=emb_logo Once you'll see it that here and there and there Nunit has something, while others don't or have less.

I will not say some tolerant things about xUnit (as well as about about MSTest) - a bad documented green sandbox with broken understanding of TDD, bad documentation and lack of cool features. Also, TDD is a concept, not a framework or smth to be limited by IDE or framework. If 'you' need borders to follow TDD and not to write a bad code, then you need to read books, not to design new framework or use xUnit.

what is xUnit?

  • almost no documentation
  • no setupt and teardown, manually written wrappers instead
  • no onetimesetup (imagine you write integration or e2e tests and need to setup default DB data)
  • no writeline
  • no test context
  • no other cool attributes which helps to control and decorate tests better
  • poor assertions
  • poor parallelism settings

MSTest is the only one concurrent for it. Does anyone in enterprise replace framework with MSTest? I think no.

NUnit is a modern full power easy to use and to learn framework which is top 1 for 20+ years. It has full documentation, good architecture (xUnit doesn't have Setup and onetimesetup? Seriously replace it with constructor? It's like to name a bug as feature instead of fixing), good community and no childhood problems.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRuslanView Question on Stackoverflow
Solution 1 - C#akardonView Answer on Stackoverflow
Solution 2 - C#JoeyView Answer on Stackoverflow
Solution 3 - C#TakinosaJiView Answer on Stackoverflow
Solution 4 - C#RadView Answer on Stackoverflow
Solution 5 - C#jagtttView Answer on Stackoverflow
Solution 6 - C#Artem GView Answer on Stackoverflow