Skip to main content

How to set up a Bazel testing configuration: The comprehensive guide for Scala and Java

Picture of Łukasz Wawrzyk, Tooling Expert

Łukasz Wawrzyk

Tooling Expert
Jun 29, 2023|15 min read
How_to_set_up_a_Bazel_testing_configuration_The_comprehensive_guide_for_Scala_and_Java_image-min.jpg
1// example/src/test/java/com/example/ExampleTest.java
2
3package com.example;
4import org.junit.Test;
5import org.junit.Assert;
6
7public class ExampleTest {
8 @Test public void example() {
9 Assert.assertEquals(1, 1);
10 }
11}
1# example/src/test/java/com/example/BUILD.bazel
2
3java_test(
4 name = "ExampleTest",
5 srcs = ["ExampleTest.java"]
6)
1bazel test //example/src/test/java/com/example:ExampleTest
1// example/src/test/scala/com/example/ExampleTest.scala
2
3package com.example
4
5import org.scalatest.funsuite.AnyFunSuite
6
7class ExampleTest extends AnyFunSuite {
8 test("example") {
9 assert(Set.empty.size == 0)
10 }
11}
1# example/src/test/scala/com/example/BUILD.bazel
2
3load("@io_bazel_rules_scala//scala:scala.bzl", "scala_test")
4
5scala_test(
6 name = "tests",
7 srcs = ["ExampleTest.scala", "OtherExampleTest.scala"],
8 deps = ["//example/src/main"]
9)
1bazel test //example/src/test/scala/com/example:tests

Note: Unlike with java_test, you can use any name for the target. Also, Bazel will execute all tests specified in the srcs attribute.

1# example/src/test/scala/com/example/BUILD.bazel
2
3load("@io_bazel_rules_scala//scala:scala.bzl", "scala_test")
4
5scala_test(
6 name = "tests",
7 srcs = ["ExampleTest.scala"],
8 tags = ["unit"]
9)
1$ bazel test //... --test_tag_filters=unit # run only unit tests
2$ bazel test //... --test_tag_filters=unit,integration,lint,-external # run unit, integration and lint tests except those tagged as 'external'
3$ bazel test //... --test_tag_filters=-stress # run all tests except the stress tests
4
1bazel test //... –test_tag_filters=lint
1bazel test //... –test_tag_filters=unit
1# setup environment here
2bazel test //... –test_tag_filters=integration
1--test_timeout=30
1--test_timeout=5,60,180,600
1# example/src/test/scala/com/example/BUILD.bazel
2
3load("@io_bazel_rules_scala//scala:scala.bzl", "scala_test")
4
5scala_test(
6 name = "tests",
7 srcs = ["ExampleTest.scala"],
8 tags = ["unit"]
9 size = "small"
10 deps = ["//test-utils", "//example/src/main"]
11)
1# tools/build_rules/test_rules.bzl
2load("@io_bazel_rules_scala//scala:scala.bzl", "scala_test")
3
4def unit_test(**kwargs):
5 if "size" not in kwargs:
6 kwargs["size"] = "small"
7
8 if "tags" not in kwargs:
9 kwargs["tags"] = []
10 if "unit" not in kwargs["tags"]:
11 kwargs["tags"].append("unit")
12
13 if "deps" not in kwargs:
14 kwargs["deps"] = []
15 if "//test-utils" not in kwargs["deps"]:
16 kwargs["deps"].append("//test-utils")
17
18 return scala_test(**kwargs)
1# example/src/test/scala/com/example/BUILD.bazel
2
3load("//tools/build_rules:test_rules.bzl", "unit_test")
4
5unit_test(
6 name = "tests",
7 srcs = ["ExampleTest.scala"],
8 deps = ["//example/src/main"]
9)
1# example/src/test/scala/com/example/BUILD.bazel
2
3load("//tools/build_rules:test_rules.bzl", "unit_test")
4
5unit_test(
6 name = "tests",
7 srcs = ["ExampleTest.scala"],
8 deps = ["//example/src/main"],
9 timeout = "moderate"
10)
1def default_target_name():
2 _, _, target_name = native.package_name().rpartition("/")
3 return target_name
1# example/src/test/scala/com/example/BUILD.bazel
2
3unit_test(
4 srcs = ["ExampleTest.scala"],
5 deps = ["//example/src/main"]
6)
1unit_test(
2 srcs = ["ExampleTest.scala"],
3 deps = ["//example/src/main"],
4 flaky = True
5)
1build --sandbox_default_allow_network=false
2test --sandbox_default_allow_network=false
1// Path to the empty directory that Bazel prepared for this test target
2var testWorkspace = Paths.get(System.getenv("TEST_TMPDIR"));
3// Path to resources used in this test (as it is inside the jar file)
4var resourcesPath = "/examples";
5// Find URI of the resources directory inside the jar file
6var uri = getClass().getResource(resourcesPath).toURI();
7// Create a new file system for the jar file based on URI
8var jarFs = FileSystems.newFileSystem(uri, new HashMap<>());
9// Copy all files from the jar file to the test workspace
10var paths = Files.list(jarFs.getPath(resourcesPath)).toList();
11for (var source : paths) {
12 // Note that Path objects determine the file system that they belong to
13 var target = testWorkspace.resolve(source.getFileName().toString());
14 Files.copy(source, target);
15}
16var copiedFiles = Files.list(testWorkspace).count();
17Assert.assertEquals(copiedFiles, 3);
1$ bazel test //location/src/test --test_filter="*.InMemoryRepositorySpec" # tests with class named "InMemoryRepositorySpec"
2$ bazel test //location/src/test --test_filter="*.oauth.*" # tests that contain "oauth" component in their package path
1$ bazel test //location/src/test --test_arg='-z' --test_arg='when popped' # run only tests with a name that contains 'when popped'

Subscribe to our newsletter and never miss an article