Running unit tests in Visual Sudio

Guide to running unit tests

Many of the programming-related worksheets provide a (limited) set of unit tests to assist you in testing your solutions. Please note these tests are provided for formative pruposes only. The unit tests used for evaluation will be different, and not all tasks and cases (especially extention tasks) will be covered by the provided tests.

This guide assumes you are using the Microsoft Editor, Visual Studio. If you are using Jetbrains Rider, you should use Running unit tests in Rider instead. Please note that Visual Studio is the officially supported editor for this module.

Open the project

Visual Studio organises code into solutions, and solutions into projects. This module tends to have two projects - one for the code you are expected to implement and one for the formative tests you can use to evaluate your code.

Opening the Solution

For the unit tests to work, and the code to compile you need to open the solution, not the file itself. Many Visual Studio features will not work when opening the file.

To open the project, you must locate the solution file (.sln) in the root of the project. It should look like this:

Windows explorer, showing the solution file

Once the project is open, the run button should show the name of the project:

Visual Studio Run Button

Attach vs Run

If Visual Studio shows 'Attach...' rather than the name of the executable, you probably have opened the file not the solution.

Using Unit Tests

Locate the project which contains the unit tests. It should be listed on the left-hand side, next to the place were you are expected to write your code.

Running unit tests

Running the Unit Tests

Right click on test project in order to open the context menu. In this menu select the ‘Run Unit Tests’ option. The IDE will then run the tests and provide a report.

Running unit tests

Test Report

The panel which shows test results should show up at the bottom of the editor. This will show the current test status for each of the unit tests.

Test Result

The tests will not run if your code cannot compile. Please fix any compliation issues before attempting to run the unit tests.

Individual Test Results

You can expand tests in the test explorer to see individual test results. For each test, the panel should tell you which tests passed, which failed and what the expected outcome is.

note tests are provided for formative purposes. Test cases used for marking will be different - do not try to ‘game’ the tests.

You can also see the test output in this view. This will allow you to find out where each test case is located for debugging purposes.

Test Result Detail

Running unit tests in Rider


Last updated 0001-01-01

Graduation Cap Book Open book GitHub Info chevron-right Sticky Note chevron-left Puzzle Piece Square Lightbulb Video Exclamation Triangle Globe