peer
peer

Reputation: 4729

How to run one test from a subproject with gradlew?

I have a multi-project build in gradle. It is an android project and the test in question does run when I execute it manually in android studio. The structure of the project is as follows:

.../my-app$ ./gradlew -q projects
[...]
Root project 'my-app'
+--- Project ':myapp'
\--- Project ':mymodel'
[...]

I want to execute a single test (class) in :myapp. Due to version constraints I have to use gradlew from the project root directory.

The right command to execute a single test seems to be ./gradlew test --tests my.project.MyTest but I don't know how to do it with subprojects.

If I run ./gradlew test --tests my.project.MyTest I get

FAILURE: Build failed with an exception.

* What went wrong:
Problem configuring task :myapp:test from command line.
> Unknown command-line option '--tests'.

with the suggestion to run gradle help --task :myapp:test That yields

> Task :help
Detailed task information for :myapp:test

Path
     :myapp:test

Type
     Task (org.gradle.api.Task)

Description
     Run unit tests for all variants.

Group
     verification

and no command line options at all, so I assume I can just leave it out.

./gradlew test my.project.MyTest gives
Task 'my.project.MyTest' not found in root project 'my-app'.
same for ./gradlew :myapp:test my.project.MyTest.

This answer to an older question suggests spelling out the variant name which is ./gradlew :myapp:testReleaseUnitTest --tests my.project.MyTest for me and that yields

> Task :myapp:testReleaseUnitTest FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':myapp:testReleaseUnitTest'.
> No tests found for given includes: [my.project.MyTest](--tests filter)

./gradlew --version is 7.1, I've just upgraded to it with ./gradlew wrapper --gradle-version 7.1 and gradle wrapper.
I'm out of ideas at this point. What am I doing wrong? How can I run a single test in the command line?

Upvotes: 5

Views: 7130

Answers (1)

M.E
M.E

Reputation: 998

I was looking for similar issue and came across your question which provided me with some hints. After some trials, I found out that the syntax to run a specific task of a subproject from root seems to be:

./gradlew sub_project:task_name

So, for a project structure similar to this:

Root project: app
+--- subProject :subapp1
+--- subProject :subapp2

The command to run MyTest test in subapp1 sub-project from root is:

./gradlew subapp1:test --tests MyTest

Edit: The source of confusion is:
When running ./gradlew -q projects, gradle reports sub-projects with a leading : character. Don't use it in your commands.

Upvotes: 5

Related Questions