-->

Today we’re here to talk about JavaScript unit testing in Visual Studio. If you’re a.NET developer creating web applications in Visual Studio, it makes sense for you to use just a single environment to test both your C# (or another.NET language) code and your JavaScript code. Type unit test in the search box, select C# as the language, and then select the C# Unit Test Project for.NET Core template, and then click Next. Note Starting in Visual Studio 2019 version 16.9, the MSTest project template name changed from MSTest Unit Test Project (.NET Core) to Unit Test Project.

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.

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

Visual Studio Unit Test

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# project named HelloWorldCore. 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 a unit test project template for the test framework you want to use, such as MSTest, and select it.

    Starting in Visual Studio 2017 version 14.8, the .NET languages include built-in templates for NUnit and xUnit. For C++, you will need to select a test framework supported by the language. For Python, see Set up unit testing in Python code to set up your test project.

    Tip

    For C#, 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.

  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).

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

Visual studio unit test data

To follow these steps, Visual Studio Enterprise is required.

  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.

Visual Studio Unit Test C#

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

Unit

Use a third-party test framework

Visual

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

  • 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 Boost 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).

Visual Studio Unit Test Data

Next steps