How do wildcard patterns work with Gradle's `--tests` flag, and what can you match with them?
answer
- `*` only, no regex
- matches FQN + method name
- should* picks method prefix
- quote against shell glob
- no-match -> task fails
basics
~10 sThe * wildcard matches any sequence of characters. So --tests 'com.example.*Test' runs all classes in com.example ending in Test, and --tests '*OrderTest.should*' runs methods starting with should.
solid answer
~40 s`--tests` accepts simple glob-style patterns where `*` matches any number of characters (including none) across package, class, and method segments. Examples: `--tests 'com.example.*'` runs every test under that package prefix; `--tests '*ServiceTest'` matches any class ending in `ServiceTest` regardless of package; `--tests 'com.example.OrderTest.should*'` runs all methods on that class starting with `should`. Patterns are matched against the fully-qualified class name and method name. Always single-quote the pattern so the shell doesn't glob-expand `*`. For parameterized/JUnit 5 tests Gradle also supports matching against the display/iteration name, so a method-level wildcard can pick specific data-driven cases. This makes `--tests` a flexible local-iteration selector without editing the build script.
code
bash · 8 lines# all classes in a package ending in Test
./gradlew test --tests 'com.example.*Test'
# any class ending in ServiceTest, any package
./gradlew test --tests '*ServiceTest'
# all 'should...' methods of one class
./gradlew test --tests 'com.example.OrderTest.should*'go deeper
Know that * matches any characters and you can target a package, class, or method-name prefix.
Explain that * is the only metacharacter (not regex), it matches FQN+method, and quoting protects it from the shell.
Discuss matching against parameterized display names and the no-match failure behavior.
Advise teams on naming conventions that make wildcard selection ergonomic across a large suite.
## Wildcard semantics The only special character is `*`, which matches **any sequence of characters, including the empty string**. There is no `?`, no regex, no brace expansion. Matching is performed against the **fully-qualified class name** and, after the class portion, the **method name** (and for some engines the display/iteration name). ## Common patterns - `--tests 'com.example.*'` — everything under the `com.example` prefix (the trailing `*` spans sub-packages, classes, and methods). - `--tests '*ServiceTest'` — any class ending in `ServiceTest`, in any package. - `--tests 'com.example.OrderTest.should*'` — all methods of `OrderTest` whose names start with `should`. - `--tests '*OrderTest'` — a class named `OrderTest` regardless of its package. ## Class vs. method boundary Gradle resolves the pattern by first finding candidate classes, then applying the remainder to method names. A pattern without a recognizable method segment selects whole classes; once a class is fully matched, the trailing text is treated as a method filter. ## Parameterized / data-driven tests With the JUnit Platform, parameterized test invocations have **display names** (e.g. `add(1, 2)`). Gradle can match a `--tests` pattern against the iteration/display name, so `--tests '*CalculatorTest.add*'` can pick specific generated invocations. The exact form depends on the engine's naming. ## Shell quoting ```bash # WRONG: unquoted * may be expanded by the shell against local files ./gradlew test --tests com.example.*Test # RIGHT: single quotes keep the literal pattern ./gradlew test --tests 'com.example.*Test' ``` ## Failure on no match If a wildcard matches nothing, the task fails with "No tests found for given includes: [pattern]" unless empty matching is permitted (see `filter.isFailOnNoMatchingTests = false`). Useful to know when a typo silently looks like "all tests skipped".
- Are these regular expressions?No. The only metacharacter is `*` (match any sequence). No `?`, anchors, character classes, or alternation — it's a simple glob, not a regex.
- Why must you quote the pattern in bash/zsh?An unquoted `*` is glob-expanded by the shell against files in the working directory before Gradle runs, so Gradle would receive an unintended, possibly empty, argument.
- Treating the pattern as a full regular expression (using `.*`, anchors, or `|`).
- Forgetting to quote, then blaming Gradle when the shell mangled the pattern.