App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Home Guide Top C# Testing Frameworks for every Developer

Top C# Testing Frameworks for every Developer

By Neha Bharati, Community Contributor -

There are a number of applications developed using C#. The .NET framework helps developers build applications for Windows. These applications also need to be tested just like any other software. There are many choices available for testers to test their application. However, the ones that stand out are MSTest, NUnit, and xUnit.Net. In this guide, let’s dive deep into these three C# Testing Frameworks and tabulate their differences.

MSTest

MSTest comes with Visual Studio Code IDE which makes it very easy to get started. It is used for unit testing. Testers can create their tests and once they run the tests, they can view the results in the IDE. Apart from that, testers can also analyze the code coverage with no additional tools required.

While it is a very attractive choice of the testing framework, it lacks performance. The experience can be sluggish and doesn’t have interoperability. It only works well with Microsoft tools and third-party integrations are not as easy.

NUnit

NUnit originally started out as a port from Java’s JUnit so it has a rich history of unit testing along with C# functionality. Apart from this NUnit has good interoperability features and can be compatible with third-party tools as well. It also has fast testing execution speeds. The only con would be that one would have to install additional tools to make it work since it doesn’t integrate into Visual Studio as MSTest does.

xUnit.Net

xUnit.Net is an open source testing framework that is based on the .NET framework. In fact, NUnit was extended by one of its creators to create xUnit. xUnit is popular for its intuitive terminology and its excellent extensibility. xUnit also has a good community hence it is well maintained and easy to expand upon. It does have a learning curve and lacks good documentation.

Implementation

Let’s have a look at how each of these C# Testing Frameworks is implemented with some examples.

MSTest

In the below class there is a Player who has 3 lives and the agenda of the game is to shoot the Enemy. If they’re unable to hit the enemy then they lose 1 life.

There is also an Enemy class that has the ability to dodge the Player. If the Enemy dodges then, the Player loses a life, else the Enemy is hit and is dead.

public class Player {
private int lives = 3;

public void FireAt(Enemy enemy) {
if (HasLives()) {
if (enemy.IsDodging()) {
enemy.Miss();
life--;
}
else {
enemy.Hit();
}


}
}

public void Recharge() {
ammo = 3;
}

public bool HasAmmo() {
return ammo > 0;
}
}


public class Enemy {

private bool dodging;
private bool dead;

public void Dodge() {
dodging = true;
}

public void Hit() {
dead = true;
}

public void Miss() {
dodging = false;
}

public bool IsDodging() {
return dodging;
}

public bool IsDead() {
return dead;
}
}

These are the two classes and now let us write test cases to test them. Let’s first test the scenario where the Player shoots at the Enemy and hits them. The expected result is that the Enemy will be dead.

[TestClass]
public class Class1
{
[TestMethod]
public void TryShootEnemy() {
Enemy enemy = new Enemy();
Player gun = new Player();
gun.FireAt(enemy);
Assert.IsTrue(enemy.IsDead());
}
}

In the above snippet there are a few terms like TestClass and TestMethod tags. These tags allow Visual Studio’s built-in testing framework to recognize this particular class as a class that contains unit tests, and to treat the method TryShootEnemy() as a test case, instead of just an ordinary method. To run the tests all you have to do is right-click on the TestMethod tag and click on Run Tests. 

Run Tests

This is a very basic level of understanding of the testing methods used in MSTest. There are other tags like TestInitialize and TestCleanup that allows you to specify if code is run before (initialize) and after(cleanup). 

NUnit

NUnit is installed via the NuGet package which can be searched for within Visual Studio and is the most popular unit test framework for C#. NUnit uses attributes similar tp MSTest, except that [TestClass] is referred to as a [TestFixture], and a [TestMethod] as simply a [Test]. Now let’s have a look at the same example. This time lets test if the dodges and lives are working properly.

[TestFixture]
public class NUnitTests {

[Test]
public void TryShootDodgingEnemy() {

Enemy enemy = new Enemy();
Player gun = new Player();

enemy.Dodge();
gun.FireAt(enemy);

enemy.Dodge();
gun.FireAt(enemy);

enemy.Dodge();
gun.FireAt(enemy);

Assert.IsFalse(enemy.IsDead());
Assert.IsFalse(gun.HasLives());
}
}

Runtime Ev1An example of a test report (Source)

In order to run the test cases, you need the command line and once everything is set up the NUnit console runner will run all the tests in your project and generate a report with the results. Apart from this, NUnit allows you to add parameters to your tests. Using these parameters, one can write a test case with arguments, then easily run the same test with a range of unique data. By doing so, one can avoid writing unique test cases for every set of arguments you want to test.

xUnit

xUnit has a much more modern and unique style of testing by doing away with the conventional [TestFixture] and [Test] tags and using new tags like Facts and Theories. In order to use XUnit, one can install it via a NuGet package much like NUnit, which you can search for within Visual Studio. 

Just like how there’s the [TestCase] tag in NUnit, xUnit also has its own method to provide parameters to a test case. This is done using the new [InLineData] tag and Theories. A test case that has no parameters is referred to as a Fact, meaning it will always execute the same. Theories are test cases that can take data directly from [InLineData] tags or even from an Excel spreadsheet. Here’s an example of the test case which covers the cases when the enemy dodges and survives or when the enemy gets hit and dies.

[Theory]
[InlineData(true, false)]
[InlineData(false, true)]
public void TestBugDodges(bool didDodge, bool shouldBeDead) {

Enemy enemy = new Enemy();
Player gun = new Player();

if (didDodge) {
enemy.Dodge();
}

gun.FireAt(enemy);

if (shouldBeDead) {
Assert.True(enemy.IsDead());
}
else {
Assert.False(enemy.IsDead());
}
}

Just like NUnit, xUnit is to be run in the command line and once the setup is done, the xUnit console runner runs all the tests and generates a report in the end.

Runtime Ev2xUnit running tests in the console (Source)

MSTest vs NUnit vs xUnit: A comparison

FeatureMSTestNUnitxUnit
Test class[TestClass][TestFixture]NA
Test Method[TestMethod][Test][Fact]
Initialization[TestInitialize][Setup]NA(constructor of the class is used for initialization)
Data driven test method[DataTestMethod]NA[Theory]
Add parameters[DataRow( _ , _)][TestCase( _ , _)][InlineData( _ , _)]
DocumentationWell DocumentedWell DocumentedDoesn’t have good documentation
Tests IsolationBy defaultCan be configuredBy default

In this article, we’ve learnt about the most popular unit test framework for C# and their usage. Each with its own pros and cons. While MSTest comes inbuilt with Visual Studio, NUnit makes testing faster and more extensible. Among the top 3, xUnit is considered the best unit testing framework for C# simply because it has fewer attributes and makes the code clean and easy to maintain, it is also extensible just like NUnit. So one should consider the use case for testing and accordingly use the appropriate tools.

No matter which C# testing framework you choose, it is important to test the application on real device cloud for more accurate test results. By testing under real user conditions you can identify the bottlenecks in the real user experience and rectify them in time before release.

Test on Real Devices

Tags
Automation Testing

Featured Articles

Selenium with C# : How to start running Automated Tests

NUnit Vs XUnit Vs MSTest: Core Differences

Curated for all your Testing Needs

Actionable Insights, Tips, & Tutorials delivered in your Inbox
By subscribing , you agree to our Privacy Policy.
thank you illustration

Thank you for Subscribing!

Expect a curated list of guides shortly.