Testing in .NET

This article introduces the concept of testing, and illustrates how different kinds of tests can be used to validate code. There are various tools available for testing .NET applications, such as the .NET CLI or Integrated Development Environments (IDEs).

Test types

Having automated tests is a great way to ensure that application code does what its authors intend it to do. This article covers unit tests, integration tests, and load tests.

Unit tests

A unit test is a test that exercises individual software components or methods, also known as "unit of work". Unit tests should only test code within the developer's control. They do not test infrastructure concerns. Infrastructure concerns include interacting with databases, file systems, and network resources.

For more information on creating unit tests, see Testing tools.

Integration tests

An integration test differs from a unit test in that it exercises two or more software components' ability to function together, also known as their "integration." These tests operate on a broader spectrum of the system under test, whereas unit tests focus on individual components. Often, integration tests do include infrastructure concerns.

Load tests

A load test aims to determine whether or not a system can handle a specified load, for example, the number of concurrent users using an application and the app's ability to handle interactions responsively. For more information on load testing of web applications, see ASP.NET Core load/stress testing.

Test considerations

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's meant to check. TDD is like creating an outline for a book before you write it. It is meant to help developers write simpler, more readable, and efficient code.

Testing tools

.NET is a multi-language development platform, and you can write various test types for C#, F#, and Visual Basic. For each of these languages, you can choose between several test frameworks.

xUnit

xUnit is a free, open source, community-focused unit testing tool for .NET. Written by the original inventor of NUnit v2, xUnit.net is the latest technology for unit testing .NET apps. xUnit.net works with ReSharper, CodeRush, TestDriven.NET, and Xamarin. It is a project of the .NET Foundation and operates under their code of conduct.

For more information, see the following resources:

NUnit

NUnit is a unit-testing framework for all .NET languages. Initially ported from JUnit, the current production release has been rewritten with many new features and support for a wide range of .NET platforms. It is a project of the .NET Foundation.

For more information, see the following resources:

MSTest

MSTest is the Microsoft test framework for all .NET languages. It's extensible and works with both .NET CLI and Visual Studio. For more information, see the following resources:

.NET CLI

You can run a solutions unit tests from the .NET CLI, with the dotnet test command. The .NET CLI exposes a majority of the functionality that Integrated Development Environments (IDEs) make available through user interfaces. The .NET CLI is cross-platform and available to use as part of continuous integration and delivery pipelines. The .NET CLI is used with scripted processes to automate common tasks.

IDE

Whether you're using Visual Studio, Visual Studio for Mac, or Visual Studio Code, there are graphical user interfaces for testing functionality. There are more features available to IDEs than the CLI, for example Live Unit Testing. For more information, see Including and excluding tests with Visual Studio.

See also

For more information, see the following articles: