Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run all tests in Bazel from a single java_test() rule?

Tags:

bazel

I am adding tests in Bazel, but I don't want to write a test rule for every single test file. However, each test rule requires a test_class - the test class that is being ran, so there is no easy way to just run all tests with a single java_test rule. Is there a work around for where I wouldn't need to specify a test_class and just run all tests at once?

like image 421
Zeitgeist Avatar asked Sep 22 '17 13:09

Zeitgeist


People also ask

Does Bazel run tests in parallel?

By default, Bazel runs tests in a parallel fashion to speed things up.

How does Bazel test work?

When running a build or a test, Bazel does the following: Loads the BUILD files relevant to the target. Analyzes the inputs and their dependencies, applies the specified build rules, and produces an action graph. Executes the build actions on the inputs until the final build outputs are produced.

How do you run a Bazel run?

To run Bazel, go to your base workspace directory or any of its subdirectories and type bazel . % bazel help [Bazel release bazel-<version>] Usage: bazel <command> <options> ... Available commands: analyze-profile Analyzes build profile data. aquery Executes a query on the post-analysis action graph.

How do I run Bazel in debug mode?

Debugging Bazel To debug the C++ client, run it from gdb or lldb as usual. However, to debug Java code, attach to the server using the following: Run Bazel with the debugging option --host_jvm_debug before the command (such as bazel --host_jvm_debug build //src:bazel ). Attach a debugger to the port 5005.


1 Answers

In Bazel, we wrote a custom Junit Suite that finds all the Junit classes on the classpath in or under the package of the annotated class. You can find the code here. It's pretty short and straight forward and you can copy it into your project or do something similar.

You can then have your rules like this:

java_library(
    name = "tests",
    testonly = 1,
    srcs = glob(["*.java"])
)

java_test(
   name = "MyTests",
   test_class = "MyTests",
   runtime_deps = [":tests"],
)

and the MyTests.java file should look like this:

import package.ClasspathSuite;

import org.junit.runner.RunWith;

@RunWith(ClasspathSuite.class)
public class MyTests { } 
like image 82
Irina Iancu Avatar answered Jan 01 '23 19:01

Irina Iancu