Skip to content

Testing Q# Code in VS Code

Alex Hansen edited this page Jan 28, 2025 · 1 revision

Unit Testing in Q#

Overview

Q# supports a @Test() annotation which, when applied to a callable, turns it into a unit test.

Requirements for a Q# Unit Test

A Q# callable can be a unit test if it takes no parameters -- no type parameters (no generics) and no value parameters. Consider the following example:

function TestCase() : Unit {
    Std.Diagnostics.Fact(2 * 2 == 4, "2*2 should be 4");
}

This test case can be turned into a runnable unit test by adding the @Test() annotation:

@Test()
function TestCase() : Unit {
    Std.Diagnostics.Fact(2 * 2 == 4, "2*2 should be 4");
}

Running the Unit Test

Upon annotating the callable, a green "run" button will show up next to it:

image

Clicking this button will run the callable and report its results. Additionally, the tests will show up in the VS Code Test Explorer. The Test Explorer can be found in the VS Code Activity Bar, and it looks like a conical flask:

image

The Test Explorer shows all discovered tests for all currently active Q# documents and projects, and provides more ways to run and interact with groups of tests. See the Test Explorer docs for more information.

image