Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create unit tests in ASP.NET Core

When I right-click a class in my application code in an ASP.NET MVC 4.6 project, I have this option to create a unit test:

Enter image description here

But in my ASP.NET Core (Visual Studio 2015 Core 1.0.0 Tooling Preview 2) I don't have this option available, when I right-click the class.

I have read that xUnit is now the recommended test framework of choice for ASP.NET Core projects. Is it not possible to use the old good Microsoft Unit Testing Framework?

Are we really forced to use xUnit now?

It looks like there will be a compatible version of the MSTest Framework in the future --> Stack Overflow question.

For ASP.NET Core RC2:

Announcing MSTest Framework support for .NET Core RC2 / ASP.NET Core RC2

As I don't want to switch to xUnit.

like image 584
Legends Avatar asked Oct 31 '22 00:10

Legends


1 Answers

At the moment you can use either MSTest, NUnit, and xUnit for .NET Core applications. xUnit is preferred as a modern unit testing framework though. The main pro for xUnit is the way that it instantiates and runs the tests.

xUnit creates a new instance per test method whereas the others create a new instance per test class and run all methods from the same instance. (So they need things like [Setup] and [TearDown] attributes).

This way of running test methods has two advantages:

  1. Test methods wouldn't be dependent on each other as they wouldn't share any field or property, which is a good practice. So developers are spontaneously forced to isolate test methods.

  2. xUnit's parallel execution could perform better as it runs all the test methods in parallel as opposed to say NUnit which cannot run test methods in the same class simultaneously.

Generally, performance doesn't seem to be a big deal in unit tests, but considering integration tests, performance really matters and xUnit can definitely perform better.

Conclusion: You're not forced to use xUnit for your .NET Core applications, but you better do so.

like image 68
akardon Avatar answered Nov 15 '22 06:11

akardon