Share via


Create and run unit tests

Create unit tests and run them frequently to make sure your code is working properly.

Create a unit test

  1. Create a unit test project.

    Add a unit test project to your solution

  2. Name the project.

    Unit test project template

    The project has now been added to your solution.

    Unit test project in Solution Explorer

  3. In the unit test project, add a reference to the project you want to test.

    Add a reference to your unit test project

  4. Select the project that contains the code you'll test.

    Select the reference to add

  5. Code your unit test.

    Add code to your unit test 

Run unit tests

  1. Open the test explorer.

    Open test explorer from the test menu 

  2. Run unit tests.

    Run unit tests in the test explorer 

    You can see the unit tests that passed or failed in the test explorer.

    Review unit test results in the test explorer 

Q&A

Q:Can I run unit tests in Visual Studio if I use a different unit test framework?

A:Yes. Use the plug-in for that framework so that Visual Studio's test runner can work with that framework. Here are the unit testing framework plug-ins for Visual Studio that are available right now.

  1. Use Visual Studio's extension manager to download your plug-in.

    Select 3rd-party unit test plug-ins with extension manager 

  2. Download your plug-in from the Visual Studio Gallery under Tools/Testing, or search for it if you know the name.

    Download your plug-in 

  3. Create a class library project.

    Create a class library project 

    Add the project to your solution.

    Name the class library project and add it 

  4. In the class library project, run NuGet to install the plug-in.

    Manage NuGet packages to install the plug-in 

    NuGet is an extension of Visual Studio that you can use to add and update libraries and tools for your projects.

  5. Install your plug-in. If you know the name, you can search for it online.

    Install your 3rd-party framework 

    The framework is referenced in your project.

    The reference for the 3rd-party unit test framework is added into your solution 

  6. In the class library project, add a reference to the project you want to test.

    Add a reference to the project  

  7. Select the project that contains the code you'll test.

    Select the project to be tested 

  8. Code your unit test.

    Add code to your unit test    

Try this next

False