Multi-module tests running problem

Need your help.
There is a multi-module project with the following Gradle-structure:

├── build.gradle
├── settings.gradle
├── /sub1
│ └── build.gradle
├── /sub2
│ └── build.gradle

The main goal is to run tests from a commandline by passing some variables inside. I want to run tests from each module and specific task separately. For example, to run tests from ‘sub1’ and with ‘task1’. Or from ‘sub1’ and with ‘task2’ and etc. Tasks should contain some specific logic to run with.
The problem is that I can not solve a problem of using task and module. Because, as I think, task should be in a root build.gradle. So, what logic should be stored in a module gradle config or it should be empty at all?
Can anyone help to solve it?

It’s a bit unclear to me what you mean.

To run task1 from sub1 you would just run ./gradlew :sub1:task1, same for ./gradlew :sub1:task2.

Because, as I think, task should be in a root build.gradle.

Task should only bei in root build.gradle if it is for the root project.
You should not add tasks to subprojects from root build script.
That is called cross-project configuration and is highly discouraged bad practice
which introduces project coupling and and disturbs or disables more sophisticated Gradle features or optimizations.

So no, the subprojects build scripts should never be empty.

If you want to share common build logic, you should use convention plugins in buildSrc or an included build, for example implemented as precompiled script plugin.

Then you apply this convention plugin where you want its effect to be in place.