This is a demo application for the Microsoft .NET Core NUnit
unit testing framework.
- It consists of two projects:
NUnitDemo
andNUnitDemo.Tests
. - The
NUnitDemo
project is a simple ASP.NET Core application consisting of two class files. - The
NUnitDemo.Tests
project is a test project that contains unit tests for theNUnitDemo
project.
How to add NUnit
framework to your ASP.NET Core Web API project.
- Create a new blank solution in Visual Studio.
- Create another project in the solution and select
Test
->Unit Test Project (.NET Core)
and name itNUnitDemo.Tests
.- This will create a new project with a
UnitTest1.cs
file using theMSTest
framework.
- This will create a new project with a
- Right click on the
NUnitDemo.Tests
project and selectManage NuGet Packages
. - Search for
NUnit
using theNuGet
package manager. - Install the following packages:
NUnit
NUnit3TestAdapter
- Click the
Build
menu and selectBuild Solution
to build your project. - Click the
Test
menu and selectRun All Tests
to run your tests. - You should see the following output in the
Test Explorer
window:
Total tests: 2. Passed: 3. Failed: 0. Skipped: 0.
Note
It doesn't matter if you use the default MSTest
framework or the NUnit
framework. You can use both in the same project. The important thing is that you're conducting unit tests to make your code more reliable.
Unit Testing is a pivotal part of the Test Driven Development (TDD) process. It is a software development process that relies on the repetition of a very short development cycle: requirements are turned into very specific test cases, then the software is improved to pass the new tests, only. This is opposed to software development that allows software to be added that is not proven to meet requirements.
Update the following line to true
in the NUnitDemo.Tests/UnitTest1.cs
file to see the tests fail.
bool breakTests = false; // To show failures in the unit tests, change this to true
- NUnit
- NUnit Documentation
- Walkthrough: Creating and Running Unit Tests for Managed Code
- Unit testing with NUnit
- Unit test basics
- What is unit testing?
- Unit Testing Tutorial: What is, Types, Tools, EXAMPLE
- Test Driven Development: By Example
- C# Documentation
This project is released under the terms of The Unlicense, which allows you to use, modify, and distribute the code as you see fit.
- The Unlicense removes traditional copyright restrictions, giving you the freedom to use the code in any way you choose.
- For more details, see the LICENSE file in this repository.
Author: Scott Grivner
Email: scott.grivner@gmail.com
Website: scottgrivner.dev
Reference: Main Branch