Introduction
I wrote NUnit tutorial for beginner. This tutorial should give you your first unit test with NUnit. Finally, for those who want to learn more seriously, I also recommend Udemy’s recommeded courses, so please read on.
What is Unit testing?
Component testing (also known as unit testing) focuses on testing components in isolation. It often requires specific support, such as test harnesses or unit test frameworks. Component testing is normally performed by developers in their development environments.
ISTQB CTFL syllabus

Unit testing is one of the software testing phases. It is typically the first phase and focuses on the smallest units of code with the finest level of granularity.
The purpose of testing is to assure quality of business logic.
As a definition of ISTQB CTFL, unit testing requires test harnesses or unit test framework.
In my case, I use Visual Studio Test explorer, some frameworks NUnit, Moq. Of course, there are other tools available for unit testing. For example, test runners include MSTest and xUnit, in addition to NUnit.
What is NUnit?
NUnit is a unit-testing framework for all .NET languages. Test Runner is a tool that is used to run or execute tests and report results. (BrowserStack What is a Test Runner)
For example, I develop simple add() method and it’s test using NUnit, as shown belows.
// System under test
public class SampleService
{
public int add(int a, int b)
{
return a + b;
}
}
// test code
[Test]
public void Add_ReturnSum()
{
// Arrange
var _sut = new ConsoleApp3.SampleService();
// Act
var result = _sut.add(1, 2);
// Assert
Assert.That(result, Is.EqualTo(3));
}
Run the test in Visual Studio Test Explorer to see the results, like this:

Visual Studio Test Explorer helps developers execute unit test and get result conveniently.
As MSDN referred, Test Explorer can run third-party and open source unit test frameworks that have implemented Test Explorer add-on interfaces.(MSDN, unit test basics) So, I assume, NUnit implements these interfaces, which allows Test Explorer to provide developers with useful functionality.
How to use it?
There are three steps.
- (Create SUT project)
- Create Test project
- Write Code
- Execute!
Create SUT project
You can create any kind of project, such as Client app, Web app, mobile app and also Console app. I have tried only Console and Client app, but I assume It doesn’t depend on the kind of application, even if there are little additional procedures.
1. Create Test project

Right click on Solution -> Add -> New Project

I choose NUnit3 Test Project and I select some properties, Projectname, location, Framework version

Visual Studio created the test project as “TestProject2” because this was my second time.
2. Write code
To write test code with NUnit3, you should use NUnits attribute and Assertion method.
Attribute
NUnit provides developers a lot of attributes. Of course, it would be ideal if you understood and could use many of them, but for starters, you need to know the following to write tests.
Test
It is one way of marking a method inside a TestFixture class as a test.
TestCases
TestCase serves the dual purpose of making a method with parameters as a test method and providing inline data to be used when invoking that method.
Setup
SetUp is used inside a TestFixture to provide a common set of functions that are performed just before each test method is called. (NUnit.org)
Assertion
NUnit provides two models of Assertion, Constraint Model and Classic Model. I recommend to use Constraint Model .
In NUnit, we can assert results as belows.
Assert.That(result == 5);
Assert.That(result, Is.EqualTo(5));
The two lines do the same thing.
Is.EqualtTo is a type of Constraints, which NUnit provides.
I believe we should use Constraints as second line of code because it improves readability and provides better error messages. When you want to assert some result, you can look for Constraints list by NUnit.
3. Execute!
When you wrote test codes, you just execute it.

Test (in header) -> Test Explorer

RightClick -> Run
Example: Entrance Fee Calculator
I write the code for Calculate entrance fee and its unit testing by using NUnit as I introduced above. Below table is decision table for Calculate entrance fee rule.
Rule1 | Rule2 | Rule3 | |
Condition | |||
Age | 6 > | 18> | 18≦ |
Result | |||
Fee | 0 | 600 | 1000 |
I’ve included the soulution files here, so if you are interested in it, download it and run in your environment.
Recommend resource
I learned NUnit, mocking and testing theory from the course Unit Testing for c# Developers c# by Mosh Hamedani on Udemy.
I highly recommended it because:
- It uses high-quality visuals
- The explanations are clear and begineer-friendly
- The source code is provided
Although it was last updated in March 2018, I confirmed that the content still works correctly with .NET8. So if you are not sure which course to take, I recommend it.
Conclusion
NUnit is one of the test runner libraries for .NET App. To execute unit test, you should know some Attributes, Assert method in NUnit and Visual Studio Test Explorer.
コメント