Show number of tests executed and passed in SonarQube with Jest, Vitest and Pytest

Tiempo de lectura: 2 minutos

Today we are going to learn how to pass and display the number of tests that have been covered in SonarQube.

SonarQube total of unit tests

For React with Vite:

First, we install:

npm install -D vitest-sonar-reporter

Now, we go to the vitest.config.ts

We add:

html

reporters: ['default', 'vitest-sonar-reporter'], // JUnit reporter to generate test reports outputFile: 'test-results/test-results.xml', // XML path that SonarQube will read

Within tests:

And within our sonar-project.properties file

sonar.testExecutionReportPaths=test-results/test-results.xml

We must also indicate the correct path where we have our tests:

html

sonar.tests=tests

We will now only need to run Sonar Scanner to make it work.

Important, please ensure that test files are not ignored:

sonar.exclusions=.vscode/**

For React Native with Jest

First we install:

npm install --save-dev jest-sonar 

Now we go to the vitest.config.ts file

We add:

 reporters: [ "default", ["jest-sonar", { outputDirectory: "test-results", outputName: "test-results.xml", }] ],

We also need to specify the correct path for our tests:

sonar.testExecutionReportPaths=test-results/test-results.xml
sonar.tests=tests

We’ll only need to run Sonar Scanner now.

Make sure test files are not ignored:

sonar.exclusions=.vscode

For Python with Pytest

We run the Pytest command with this parameter:

--junitxml=xunit-reports/xunit-result.xml

We put this in our sonar-project.properties file

sonar.junit.reportPaths=xunit-reports/xunit-result.xml

We also need to indicate the correct path where we have our tests:

sonar.tests=tests

With this, we will only need to run Sonar Scanner to make it work.

Important: Make sure that test files are not ignored:

sonar.exclusions=.vscode/

*Important: the generated file must have a structure of <testsuite> <testcase>

<testsuite name="...." errors="0" failures="0" skipped="0" timestamp="2025-05-21T09:02:49" time="5.575" tests="4"> <testcase>...

Also, I recommend adding the report folder to .gitignore.

Leave a Comment