Testing in .NET
This article introduces the concept of testing and illustrates how different kinds of tests can be used to validate code. Various tools are available for testing .NET applications, such as the .NET CLI or Integrated Development Environments (IDEs).
Test types
Automated tests are a great way to ensure that the application code does what its authors intend. 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 a "unit of work." Unit tests should only test code within the developer's control. They don't 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 you write a unit test before the code it's meant to check. TDD is like creating an outline for a book before you write it. The unit test helps developers write simpler, 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. The original inventor of NUnit v2 wrote xUnit.net. xUnit.net is the latest technology for unit testing .NET apps. It also works with ReSharper, CodeRush, TestDriven.NET, and Xamarin. xUnit.net is a project of the .NET Foundation and operates under its code of conduct.
For more information, see the following resources:
NUnit
NUnit is a unit-testing framework for all .NET languages. Initially, NUnit was ported from JUnit, and the current production release has been rewritten with many new features and support for a wide range of .NET platforms. It's 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:
MSTest runner
The MSTest runner is a lightweight and portable alternative to VSTest for running tests in continuous integration (CI) pipelines, and in Visual Studio Test Explorer. For more information, see MSTest runner overview.
.NET CLI
You can run a solutions unit test from the .NET CLI with the dotnet test command. The .NET CLI exposes most 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 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: