简体   繁体   中英

How to run all JUnit tests with Bazel

I have been trying to run all JUnit tests in a directory with Bazel. As far as I know, the java_test rule is only capable of running a specific class. However, I'm looking for behavior more like mvn test which runs all JUnit tests in the project. How can I accomplish this?

The typical way to organize this is to have a java_test rule per Java test class, or per group of related Java test classes. Then java_test s can be grouped together using test_suite , if that's something you want to do.

You can run all tests in a package with:

bazel test //some/package:all

or in a package and its subpackages:

bazel test //some/package/...

or in the entire workspace:

bazel test //...

More about target patterns: https://docs.bazel.build/versions/master/guide.html#target-patterns

If you just want a java_test that runs all the tests in a directory, you can do something like

java_test(
    name = "tests",
    srcs = glob(["*Test.java"]),
    deps = [ ..... ],
)

but that may or may not be the right thing to do. In particular, if you want to just run one test or one test method (eg using --test_filter ), bazel will still build all the java_test 's dependencies. Also, note that glob s only glob within a build package, and won't cross over into other packages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM