Run only a subset of tests with Sonar Scan for Gradle

Environment

  • Using SonarQube Server Enterprise Edition v10.8.1 and Gradle Plugin 6.0.1.5171
  • Deployed by GitHub Actions

Hello!
I’m running into some issues configuring SonarQube to run only over a subset of tests.

To avoid spending a lot of time while running the unit tests on my app I’m trying to only run and build those related to the code that has been updated on the PR, but SonarQube scan fails because it requests the .jar files generated when running the other tests from the project, which are obviously not generated because the tests are not run.
Since it will depend on the PR I cannot just skip the modules from the Gradle files, but instead I need a command that I can apply on the Github action running the scan.
I have already tried some commands like skipCompile and skipTests, but those seem to do nothing or are deprecated, so none of the solutions I tested worked.

Is it possible to do this?

Thank you!

2 Likes

Hi,

Welcome to the community!

So that we know clearly what you’re dealing with, can you provide the full analysis log, please?

The analysis / scanner log is what’s output from the analysis command. Hopefully, the log you provide - redacted as necessary - will include that command as well.

This guide will help you find them.

 
Thx,
Ann

1 Like

Here are the logs!

First, since I can’t get the analysis log on a failing run, I’ll post the error I get when it fails, just to give a bit of context:

> Task :sonar FAILED
Task listener: task :sonar failed
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':sonar'.
> Could not resolve all files for configuration ':xxxxxxxxxxx:debugUnitTestCompileClasspath'.
   > Failed to transform testing-annotations-jvm-yyyyyyy.jar (project :testing:testing-annotations) to match attributes {artifactType=android-classes-jar, org.gradle.category=library, org.gradle.jvm.environment=standard-jvm, org.gradle.libraryelements=jar, org.gradle.usage=java-api, org.jetbrains.kotlin.platform.type=jvm}.
      > Execution failed for IdentityTransform: /__w/xxxxxxxxx/testing/testing-annotations/build/libs/testing-annotations-jvm-yyyyyyy.jar.
         > File/directory does not exist: /__w/xxxxxxxxx/testing/testing-annotations/build/libs/testing-annotations-jvm-yyyyyyy.jar

For a working build, which includes those logs:

Sensors : JaCoCo XML Report Importer -> Kotlin Sensor -> Gradle Sensor -> KotlinSurefireSensor -> Java Config Sensor -> IaC Docker Sensor -> EnterpriseTextAndSecretsSensor
[org.sonarsource.scanner.lib.internal.ScannerEngineLauncher] Sensor JaCoCo XML Report Importer [jacoco]
[org.sonarsource.scanner.lib.internal.ScannerEngineLauncher] Importing 1 report(s). Turn your logs in debug mode in order to see the exhaustive list.
[org.sonarsource.scanner.lib.internal.ScannerEngineLauncher] Reading report '/__w/xxxxxxxxx/build/reports/jacoco/report.xml'

***
Lots of files, all of them with "generated metadata with charset 'UTF-8'"
***
[org.sonarsource.scanner.lib.internal.ScannerEngineLauncher] Sensor JaCoCo XML Report Importer [jacoco] (done) | time=348ms

Hi,

I’m pretty sure this didn’t come from analysis, but from a prerequisite task:

Can you try adding --info to your command so we can get some logging?

 
Ann

I’m getting some logs, but there’s hundreds of them in-between the lines of the previous error log.

I hope these ones are useful:

Build operation 'Identifying work' started
2025-02-28T11:16:27.017+0000 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationRunner] Completing Build operation 'Identifying work'
2025-02-28T11:16:27.017+0000 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationRunner] Build operation 'Execute unit of work' started
2025-02-28T11:16:27.017+0000 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationRunner] Build operation 'Snapshot transform inputs' started
2025-02-28T11:16:27.017+0000 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationRunner] Build operation 'Snapshot inputs and outputs before executing IdentityTransform: /__w/xxxxxxxxx/testing-annotations/build/libs/testing-annotations-jvm-yyyyyyyyyy.jar' started
2025-02-28T11:16:27.017+0000 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationRunner] Completing Build operation 'Snapshot inputs and outputs before executing IdentityTransform: /__w/xxxxxxxxx/testing-annotations/build/libs/testing-annotations-jvm-yyyyyyyyyy.jar'
2025-02-28T11:16:27.018+0000 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationRunner] Completing Build operation 'Snapshot transform inputs'
2025-02-28T11:16:27.018+0000 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationRunner] Build operation 'Executing IdentityTransform: /__w/xxxxxxxxx/testing-annotations/build/libs/testing-annotations-jvm-yyyyyyyyyy.jar'
2025-02-28T11:16:27.018+0000 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationRunner] Build operation 'IdentityTransform testing-annotations-jvm-yyyyyyyyyy.jar' started
2025-02-28T11:16:27.018+0000 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationRunner] Completing Build operation 'IdentityTransform testing-annotations-jvm-yyyyyyyyyy.jar'
2025-02-28T11:16:27.019+0000 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationRunner] Completing Build operation 'Executing IdentityTransform: /__w/xxxxxxxxx/testing-annotations/build/libs/testing-annotations-jvm-yyyyyyyyyy.jar'
2025-02-28T11:16:27.019+0000 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationRunner] Build operation 'Snapshot outputs after executing IdentityTransform: /__w/xxxxxxxxx/testing-annotations/build/libs/testing-annotations-jvm-yyyyyyyyyy.jar' started
2025-02-28T11:16:27.019+0000 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationRunner] Completing Build operation 'Snapshot outputs after executing IdentityTransform: /__w/xxxxxxxxx/testing-annotations/build/libs/testing-annotations-jvm-yyyyyyyyyy.jar'
2025-02-28T11:16:27.019+0000 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationRunner] Completing Build operation 'Execute unit of work'
2025-02-28T11:16:27.019+0000 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationRunner] Completing Build operation 'Resolve files of configuration ':tooling:tooling-verbal-message-chronometer:debugAndroidTestCompileClasspath''
2025-02-28T11:16:27.020+0000 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationRunner] Completing Build operation 'Snapshot task inputs for :sonar'
2025-02-28T11:16:26.321+0000 [LIFECYCLE] [class org.gradle.internal.buildevents.TaskExecutionLogger] 
2025-02-28T11:16:26.321+0000 [LIFECYCLE] [class org.gradle.internal.buildevents.TaskExecutionLogger] > Task :sonar FAILED

Also, here are some logs describing the exception

* Exception is:
2025-02-28T11:16:27.055+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':sonar'.
2025-02-28T11:16:27.055+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:38)
2025-02-28T11:16:27.055+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.executeTask(EventFiringTaskExecuter.java:77)
2025-02-28T11:16:27.055+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.call(EventFiringTaskExecuter.java:55)
2025-02-28T11:16:27.055+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.call(EventFiringTaskExecuter.java:52)
2025-02-28T11:16:27.055+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:210)
2025-02-28T11:16:27.056+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:205)
2025-02-28T11:16:27.056+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:67)
2025-02-28T11:16:27.056+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:60)
2025-02-28T11:16:27.056+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:167)
2025-02-28T11:16:27.056+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:60)
2025-02-28T11:16:27.056+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.internal.operations.DefaultBuildOperationRunner.call(DefaultBuildOperationRunner.java:54)
2025-02-28T11:16:27.056+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter.execute(EventFiringTaskExecuter.java:52)
2025-02-28T11:16:27.056+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.execution.plan.LocalTaskNodeExecutor.execute(LocalTaskNodeExecutor.java:42)
2025-02-28T11:16:27.056+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:331)
2025-02-28T11:16:27.056+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:318)
2025-02-28T11:16:27.056+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.lambda$execute$0(DefaultTaskExecutionGraph.java:314)
2025-02-28T11:16:27.056+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.internal.operations.CurrentBuildOperationRef.with(CurrentBuildOperationRef.java:85)
2025-02-28T11:16:27.056+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:314)
2025-02-28T11:16:27.056+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:303)
2025-02-28T11:16:27.056+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.execute(DefaultPlanExecutor.java:459)
2025-02-28T11:16:27.056+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.run(DefaultPlanExecutor.java:376)
2025-02-28T11:16:27.056+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
2025-02-28T11:16:27.056+0000 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.internal.concurrent.AbstractManagedExecutor$1.run(AbstractManagedExecutor.java:48)

Hi,

Yes, Gradle DEBUG logs are painful. That’s why I asked for --info :slight_smile:

Unfortunately, the logs you’ve provided aren’t helpful. Can you try it again with a lower log level and provide the full output?

 
Thx,
Ann

Hello!
Sorry, thought I had changed to the --info. Here is the output using that one. I had to remove the Configure project step for the non related parts of the build (company policy). Checked it and all of them are nearly identical to the one that is shown there.

Thank you!

sonar-analysis-info.log (43.9 KB)

Hi,

Thanks for the log. Here’s what I see:

First we have this:

Successfully started process 'command '/__w/xxxxxxxxx/xxxxxxxxx/apps/xxxxxxxxx/build/tmp/sentry-cli-2.33.1.exe''
[sentry] Failed to execute sentry-cli info. Error Output: error: Auth token is required for this request. Please run `sentry-cli login` and try again!

It’s a failure, but it doesn’t seem to be what errors-out the job.

Then we have the stacktrace that ends with this Caused by:


Caused by: java.lang.IllegalArgumentException: File/directory does not exist: /__w/xxxxxxxxx/xxxxxxxxx/testing/testing-annotations/build/libs/testing-annotations-jvm.jar

So again, the failure is nominally attributed to the sonar job because the failing task apparently ran as a precursor. But it’s not SonarQube analysis that’s failing.

 
HTH,
Ann

Hello!

Successfully started process 'command '/__w/xxxxxxxxx/xxxxxxxxx/apps/xxxxxxxxx/build/tmp/sentry-cli-2.33.1.exe''
[sentry] Failed to execute sentry-cli info. Error Output: error: Auth token is required for this request. Please run `sentry-cli login` and try again!

This one is always there, even on successful runs.

The thing is that it only fails when running a subset of tests, and not all of them (since all the .jar files are generated).

But if it’s not Sonar what’s failing, executing only a subset of tests and running a Sonar scan is possible, right? Even if the project is configured with all the tests? Or is there any restriction to that (i.e.: all the tests of the same package need to be executed together or something like that)?

Thank you!

Hi,

I can’t answer your questions. I can only tell you the failure is unrelated to SonarQube analysis.

 
Ann

1 Like