gradle-scalatest
gradle-scalatest copied to clipboard
Test filtering
I'm struggling a bit with test filtering, not sure of what should and shouldn't work.
./gradlew test
runs all my tests, as expected.
./gradlew test --tests 'chap01.FirstStringTests'
only runs the specified class.
./gradlew test --tests 'FirstStringTests'
also runs the specified class, but runs it twice.
./gradlew test --tests 'chap01.FirstString*'
runs nothing.
./gradlew test --tests '*FirstStringTests'
crashes with an exception:
Exception in thread "ScalaTest-main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 3
.*(*FirstStringTests)$
^
at java.base/java.util.regex.Pattern.error(Pattern.java:2038)
at java.base/java.util.regex.Pattern.sequence(Pattern.java:2213)
at java.base/java.util.regex.Pattern.expr(Pattern.java:2079)
at java.base/java.util.regex.Pattern.group0(Pattern.java:3060)
at java.base/java.util.regex.Pattern.sequence(Pattern.java:2134)
at java.base/java.util.regex.Pattern.expr(Pattern.java:2079)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1793)
at java.base/java.util.regex.Pattern.<init>(Pattern.java:1440)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1079)
at org.scalatest.tools.ArgsParser$.genSuffixesPattern(ArgsParser.scala:175)
at org.scalatest.tools.ArgsParser$.parseArgs(ArgsParser.scala:474)
at org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:832)
at org.scalatest.tools.Runner$.main(Runner.scala:775)
at org.scalatest.tools.Runner.main(Runner.scala)
The exception seems to suggest that *
is used as regex syntax, not wildcard. Indeed:
./gradlew test --tests '.*FirstStringTests'
runs the class (twice). On the other hand,
./gradlew test --tests '.*FirstString.*'
./gradlew test --tests '.*FirstString*'
./gradlew test --tests 'FirstString.*'
./gradlew test --tests 'FirstString*'
./gradlew test --tests 'chap01.FirstString.*'
./gradlew test --tests 'chap01.FirstString*'
./gradlew test --tests '*'
./gradlew test --tests '.*'
run nothing.
This is all a bit puzzling. Ultimately, my goal is to run all the test classes in a specific package, like chap01
.
My setup is a little complicated, but I'm using Gradle 7.5, Scala 3.1.3, and ScalaTest 3.2.12.
If you want gradle test filtering to work naturally then you probably need to migrate to https://github.com/helmethair-co/scalatest-junit-runner.
This plugin attempts to shim between gradle and the scalatest runner, which have different approaches to filtering.
I had a look at the plugin you mentioned, but it doesn't seem to support Scala 3, and I didn't want it to bring a whole new host of problems. I can run tests in individual packages using IntelliJ, so I can probably figure out a way to run Scalatest directly without going through Gradle.
In my experience nothing works perfectly - I tend to use IntelliJ's native build and test, but this too fails to run certain types when individual tests are selected.
On Scala 3 support via the scalatest-junit-runner, it seems to work out of the box on a simple test project.