How to ensure to fail the Quality Gate if there are no UNIT Test cases written on Java

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension) - 9.2
  • what are you trying to achieve - Need to fail the Jenkins build if there are no test cases identified on SonarQube scan
  • what have you tried so far to achieve this

What “command-line” build tool are you using? If it’s Maven, which is likely, then you can configure the Maven pom.xml to fail the build. Just go into the “Surefire” plugin configuration, and set “failIfNoTests” to true.

Generally, if the running of unit tests fail, you shouldn’t even run the SonarQube scan on that.

3 Likes

Hi @sujaykv ,

I agree with @David_Karr : you should fail the Jenkins build earlier than the SonarQube scan. Don’t rely on the SonarQube scan to catch it because SonarQube relies on the unit and coverage reports that are imported through your Sonar scanner.

For example, when you run mvn clean verify or mvn test, you’ll see this in the Maven logs:

[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ sonarscanner-maven-basic-artifact ---
[INFO] No tests to run.

You could check if No tests to run. occurs in the logs and fail the Jenkins build that way.

If you really want to check the Unit Tests count (assuming that you do import unit tests into SonarQube), you can add an “Overall Code” condition in your Quality Gate to check how many unit tests are imported:

There is no quality gate condition for unit tests for the “New Code” period.

Thanks Joe for the explanation and I agree to @David_Karr point as well. Let me fail the build at jenkins itself.

Thanks once again !!!

1 Like

We are using Jenkins and for tests we are running “mvn test verify”. As you said let me update the pom file and test it out :slight_smile: