Unit testing in .NET Core and .NET Standard
.NET Core makes it easy to create unit tests. This article introduces unit tests and illustrates how they differ from other kinds of tests. The linked resources near the bottom of the page show you how to add a test project to your solution. After you set up your test project, you will be able to run your unit tests using the command line or Visual Studio.
.NET Core 2.0 and later supports .NET Standard 2.0, and we will use it's libraries to demonstrate unit tests.
You are able to use built-in .NET Core 2.0 and later unit test project templates for C#, F# and Visual Basic as a starting point for your personal project.
What are unit tests?
Having automated tests is a great way to ensure a software application does what its authors intend it to do. There are multiple types of tests for software applications. These include integration tests, web tests, load tests, and others. Unit tests test individual software components and methods. Unit tests should only test code within the developer’s control. They should not test infrastructure concerns. Infrastructure concerns include databases, file systems, and network resources.
Also, keep in mind there are best practices for writing tests. For example, Test Driven Development (TDD) is when a unit test is written before the code it is meant to check. TDD is like creating an outline for a book before we write it. It is meant to help developers write simpler, more readable, and efficient code.
The ASP.NET team follows these conventions to help developers come up with good names for test classes and methods.
Try not to introduce dependencies on infrastructure when writing unit tests. These make the tests slow and brittle, and should be reserved for integration tests. You can avoid these dependencies in your application by following the Explicit Dependencies Principle and using Dependency Injection. You can also keep your unit tests in a separate project from your integration tests. This ensures your unit test project doesn’t have references to or dependencies on infrastructure packages.
More information on unit testing in .NET Core projects:
.NET Core unit test projects are supported for:
You can also choose between:
You can learn more in the following walkthroughs:
- Create unit tests using xUnit and C# with the .NET Core CLI.
- Create unit tests using NUnit and C# with the .NET Core CLI.
- Create unit tests using MSTest and C# with the .NET Core CLI.
- Create unit tests using xUnit and F# with the .NET Core CLI.
- Create unit tests using NUnit and F# with the .NET Core CLI.
- Create unit tests using MSTest and F# with the .NET Core CLI.
- Create unit tests using xUnit and Visual Basic with the .NET Core CLI.
- Create unit tests using NUnit and Visual Basic with the .NET Core CLI.
- Create unit tests using MSTest and Visual Basic with the .NET Core CLI.
You can learn more in the following articles:
- Visual Studio Enterprise offers great testing tools for .NET Core. Check out Live Unit Testing or code coverage to learn more.
- For more information on how to run selective unit tests, see Running selective unit tests, or including and excluding tests with Visual Studio.
- How to use xUnit with .NET Core and Visual Studio.