Unittest Runner

The Unity Test Framework package (formerly the “Unity Test Runner”) is a tool that allows you to test your code in both Edit mode and Play mode, and also on target platforms such as Standalone, Android, or iOS Apple’s mobile operating system. See in Glossary. For more information on other versions of the Test Framework package.

When I run the test file, the console shows 'No tests were found'. This problem seems to go away when I: (1) Enclose the unittest.main inside an if name 'main' (I sort of understand how this clause works, but it makes no sense for me in this case, when the unittest.main module runs properly when there's an if clause, versus. Sep 22, 2021 In this area, you can configure a list of Name-Value pairs of environment variables that would be passed to the process started by the unit test runner and to all its child processes. These environment variables will override variables with the same names defined in your system, if any.

-->

This tutorial takes you through an interactive experience building a sample solution step-by-step to learn unit testing concepts. If you prefer to follow the tutorial using a pre-built solution, view or download the sample code before you begin. For download instructions, see Samples and Tutorials.

This article is about testing a .NET Core project. If you're testing an ASP.NET Core project, see Integration tests in ASP.NET Core.

Prerequisites

  • The .NET 5.0 SDK or later

Create the source project

Open a shell window. Create a directory called unit-testing-using-mstest to hold the solution. Inside this new directory, run dotnet new sln to create a new solution file for the class library and the test project. Create a PrimeService directory. The following outline shows the directory and file structure thus far:

Make PrimeService the current directory and run dotnet new classlib to create the source project. Rename Class1.cs to PrimeService.cs. Replace the code in the file with the following code to create a failing implementation of the PrimeService class:

Change the directory back to the unit-testing-using-mstest directory. Run dotnet sln add to add the class library project to the solution:

Create the test project

Create the PrimeService.Tests directory. The following outline shows the directory structure:

Make the PrimeService.Tests directory the current directory and create a new project using dotnet new mstest. The dotnet new command creates a test project that uses MSTest as the test library. The template configures the test runner in the PrimeServiceTests.csproj file:

The test project requires other packages to create and run unit tests. dotnet new in the previous step added the MSTest SDK, the MSTest test framework, the MSTest runner, and coverlet for code coverage reporting.

Add the PrimeService class library as another dependency to the project. Use the dotnet add reference command:

You can see the entire file in the samples repository on GitHub.

The following outline shows the final solution layout:

Change to the unit-testing-using-mstest directory, and run dotnet sln add:

Create the first test

Write a failing test, make it pass, then repeat the process. Remove UnitTest1.cs from the PrimeService.Tests directory and create a new C# file named PrimeService_IsPrimeShould.cs with the following content:

The TestClass attribute denotes a class that contains unit tests. The TestMethod attribute indicates a method is a test method.

Save this file and execute dotnet test to build the tests and the class library and then run the tests. The MSTest test runner contains the program entry point to run your tests. dotnet test starts the test runner using the unit test project you've created.

Your test fails. You haven't created the implementation yet. Make this test pass by writing the simplest code in the PrimeService class that works:

In the unit-testing-using-mstest directory, run dotnet test again. The dotnet test command runs a build for the PrimeService project and then for the PrimeService.Tests project. After building both projects, it runs this single test. It passes.

Add more features

Now that you've made one test pass, it's time to write more. There are a few other simple cases for prime numbers: 0, -1. You could add new tests with the TestMethod attribute, but that quickly becomes tedious. There are other MSTest attributes that enable you to write a suite of similar tests. A test method can execute the same code but have different input arguments. You can use the DataRow attribute to specify values for those inputs.

Instead of creating new tests, apply these two attributes to create a single data driven test. The data driven test is a method that tests several values less than two, which is the lowest prime number. Add a new test method in PrimeService_IsPrimeShould.cs:

Run dotnet test, and two of these tests fail. To make all of the tests pass, change the if clause at the beginning of the IsPrime method in the PrimeService.cs file:

Continue to iterate by adding more tests, more theories, and more code in the main library. You have the finished version of the tests and the complete implementation of the library.

You've built a small library and a set of unit tests for that library. You've structured the solution so that adding new packages and tests is part of the normal workflow. You've concentrated most of your time and effort on solving the goals of the application.

See also

-->

Use Visual Studio to define and run unit tests to maintain code health, ensure code coverage, and find errors and faults before your customers do. Run your unit tests frequently to make sure your code is working properly.

Msbuild Run Unit Tests

In this article, the code uses C# and C++, illustrations are in C#, but the concepts and features apply to .NET languages, C++, Python, JavaScript, and TypeScript.

Create unit tests

This section describes how to create a unit test project.

  1. Open the project that you want to test in Visual Studio.

    For the purposes of demonstrating an example unit test, this article tests a simple 'Hello World' C# or C++ Console project named HelloWorld (HelloWorldCore in C#). The sample code for such a project is as follows:

  2. In Solution Explorer, select the solution node. Then, from the top menu bar, select File > Add > New Project.

  3. In the new project dialog box, find the unit test project to use.

    Type test in the search box to find a unit test project template for the test framework you want to use, such as MSTest (C#) or the Native Unit Test project (C++), and select it.

    Expand the Installed node, choose the language that you want to use for your test project, and then choose Test.

    Starting in Visual Studio 2017 version 14.8, the .NET languages include built-in templates for NUnit and xUnit. For C++, in this example select the Native Unit Test project, which uses Microsoft Native Unit Test Framework. (To use a different C++ test framework, see Writing unit tests for C/C++). For Python, see Set up unit testing in Python code to set up your test project.

    Tip

    For C# only, you can create unit test projects from code using a faster method. For more information, see Create unit test projects and test methods. To use this method with .NET Core or .NET Standard, Visual Studio 2019 is required.

    The following illustration shows an MSTest unit test, which is supported in .NET.

    Click Next, choose a name for the test project, and then click Create.

    Choose a name for the test project, such as HelloWorldTests, and then click OK.

    The project is added to your solution.

  4. In the unit test project, add a reference to the project you want to test by right-clicking on References or Dependencies and then choosing Add Reference or Add Project Reference.

  5. Select the project that contains the code you'll test and click OK.

  6. Add code to the unit test method.

    For example, you might use the following code by selecting the correct documentation tab that matches your test framework: MSTest, NUnit, or xUnit (supported on .NET only), or C++ Microsoft Native Unit Test Framework.

Run unit tests

  1. Open Test Explorer.

    To open Test Explorer, choose Test > Test Explorer from the top menu bar (or press Ctrl + E, T).

    To open Test Explorer, choose Test > Windows > Test Explorer from the top menu bar.

  2. Run your unit tests by clicking Run All (or press Ctrl + R, V).

    After the tests have completed, a green check mark indicates that a test passed. A red 'x' icon indicates that a test failed.

Tip

You can use Test Explorer to run unit tests from the built-in test framework (MSTest) or from third-party test frameworks. You can group tests into categories, filter the test list, and create, save, and run playlists of tests. You can also debug tests and analyze test performance and code coverage.

View live unit test results (Visual Studio Enterprise)

If you are using the MSTest, xUnit, or NUnit testing framework in Visual Studio 2017 or later, you can see live results of your unit tests.

Note

To follow these steps, Visual Studio Enterprise is required, along with .NET code and one of the following test frameworks: MSTest, xUnit, or NUnit.

  1. Turn live unit testing from the Test menu by choosing Test > Live Unit Testing > Start.

  2. View the results of the tests within the code editor window as you write and edit code.

  3. Click a test result indicator to see more information, such as the names of the tests that cover that method.

Unittest Runner

Dotnet Core Run Unit Tests

For more information about live unit testing, see Live unit testing.

Use a third-party test framework

You can run unit tests in Visual Studio by using third-party test frameworks such as NUnit, Boost, or Google C++ Testing Framework, depending on your programming language. To use a third-party framework:

Python Unittest Runner

  • Use the NuGet Package Manager to install the NuGet package for the framework of your choice.

  • (.NET) Starting in Visual Studio 2017 version 14.6, Visual Studio includes pre-configured test project templates for NUnit and xUnit test frameworks. The templates also include the necessary NuGet packages to enable support.

  • (C++) In Visual Studio 2017 and later versions, some frameworks like Google C++ Testing Framework are already included. For more information, see Write unit tests for C/C++ in Visual Studio.

To add a unit test project:

  1. Open the solution that contains the code you want to test.

  2. Right-click on the solution in Solution Explorer and choose Add > New Project.

  3. Select a unit test project template.

    In this example, select NUnit

    Click Next, name the project, and then click Create.

    The project template includes NuGet references to NUnit and NUnit3TestAdapter.

  4. Add a reference from the test project to the project that contains the code you want to test.

    Right-click on the project in Solution Explorer, and then select Add > Reference. (You can also add a reference from the right-click menu of the References or Dependencies node.)

  5. Add code to your test method.

  6. Run the test from Test Explorer or by right-clicking on the test code and choosing Run Test(s) (or Ctrl + R, T).

Unit Test Runner Download

Next steps