bazel test: Sizes, Filters & Output
bazel test builds the test targets you name, plus everything they need, and runs each test as an action: in a sandbox, with a clean environment, a timeout, and its result cached. A test is any executable that exits with 0 for success. Bazel doesn't care whether it's GoogleTest, unittest, go test or a shell script.
The test contract
Bazel sets a documented environment (the Test Encyclopedia) for every test. The most useful variables:
| Variable | Meaning |
|---|---|
TEST_TMPDIR |
a private, writable, empty directory; use it instead of /tmp |
TEST_SRCDIR, RUNFILES_DIR |
the test's runfiles tree |
XML_OUTPUT_FILE |
where to write a JUnit XML report (if the framework supports it) |
TEST_TARGET |
the label of the test |
TEST_SHARD_INDEX, TEST_TOTAL_SHARDS, TEST_SHARD_STATUS_FILE |
sharding (next lesson) |
TESTBRIDGE_TEST_ONLY |
the value of --test_filter, for frameworks that support filtering |
Each test also has a size, which sets its default timeout and tells a scheduler how many resources it needs:
size |
Default timeout | Intended for |
|---|---|---|
small |
60 s (short) |
unit tests, no I/O beyond the sandbox |
medium (default) |
300 s (moderate) |
tests that touch the local file system or a local server |
large |
900 s (long) |
integration tests |
enormous |
3600 s (eternal) |
end-to-end tests |
If you don't set size, every test is medium, and Bazel prints "There were tests whose specified size is too big" when a test finishes far below its timeout. Set size = "small" on unit tests; it also helps remote execution schedule them.
Passing, failing and output
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_python", version = "2.4.5")' > MODULE.bazel
mkdir -p cart
cat > cart/BUILD.bazel <<'EOF'
load("@rules_python//python:defs.bzl", "py_library", "py_test")
py_library(name = "cart", srcs = ["cart.py"])
py_test(name = "total_test", size = "small", srcs = ["total_test.py"], deps = [":cart"])
py_test(name = "discount_test", size = "small", srcs = ["discount_test.py"], deps = [":cart"])
EOF
cat > cart/cart.py <<'EOF'
def total(prices):
return sum(prices)
def discount(cents, percent):
return cents - cents * percent // 100 + 1 # bug: off by one
EOF
printf 'import unittest\nfrom cart.cart import total\nclass T(unittest.TestCase):\n def test_total(self):\n self.assertEqual(total([100, 250]), 350)\nunittest.main()\n' > cart/total_test.py
printf 'import unittest\nfrom cart.cart import discount\nclass T(unittest.TestCase):\n def test_discount(self):\n self.assertEqual(discount(1000, 10), 900)\nunittest.main()\n' > cart/discount_test.py
bazel test //cart/... 2>&1 | grep -E 'PASSED|FAILED|Executed|test.log'
echo "== --test_output=errors"
bazel test //cart/... --test_output=errors 2>&1 | grep -E '^(AssertionError|FAIL:|Ran)|PASSED|FAILED in'FAIL: //cart:discount_test (Exit 1) (see /home/learner/.cache/bazel/_bazel_learner/d9e2caa2f1afb3930baea60aacfb906e/execroot/_main/bazel-out/k8-fastbuild/testlogs/cart/discount_test/test.log)
INFO: Build completed, 1 test FAILED, 25 total actions
//cart:total_test PASSED in 0.3s
//cart:discount_test FAILED in 0.3s
/home/learner/.cache/bazel/_bazel_learner/d9e2caa2f1afb3930baea60aacfb906e/execroot/_main/bazel-out/k8-fastbuild/testlogs/cart/discount_test/test.log
Executed 2 out of 2 tests: 1 test passes and 1 fails locally.
== --test_output=errors
FAIL: //cart:discount_test (Exit 1) (see /home/learner/.cache/bazel/_bazel_learner/d9e2caa2f1afb3930baea60aacfb906e/execroot/_main/bazel-out/k8-fastbuild/testlogs/cart/discount_test/test.log)
FAIL: test_discount (__main__.T.test_discount)
AssertionError: 901 != 900
Ran 1 test in 0.001s
//cart:total_test (cached) PASSED in 0.3s
//cart:discount_test FAILED in 0.3s
By default Bazel prints a one-line summary per test and the path of the failing test's log. Use --test_output:
| Value | Shows |
|---|---|
summary (default) |
one line per test |
errors |
the log of every failing test, after it finishes: the setting most teams put in .bazelrc |
all |
every test's log, passing or not |
streamed |
live output, one test at a time (implies running tests locally and sequentially) |
In the second run total_test was not run again ((cached)), while the failing test was: Bazel never caches a failure.
Choosing which tests run
mkdir ws && cd ws
echo 'bazel_dep(name = "rules_python", version = "2.4.5")' > MODULE.bazel
mkdir -p t
cat > t/BUILD.bazel <<'EOF'
load("@rules_python//python:defs.bzl", "py_test")
py_test(name = "unit_test", size = "small", srcs = ["probe.py"], main = "probe.py")
py_test(name = "db_test", size = "medium", srcs = ["probe.py"], main = "probe.py", tags = ["integration"])
py_test(name = "e2e_test", size = "large", srcs = ["probe.py"], main = "probe.py", tags = ["manual"])
py_test(name = "env_test", size = "small", srcs = ["probe.py"], main = "probe.py",
env = {"SHOP_REGION": "eu"}, args = ["--fast"])
EOF
cat > t/probe.py <<'EOF'
import os, sys
print(os.environ["TEST_TARGET"], "args=%s" % sys.argv[1:], "region=%s" % os.environ.get("SHOP_REGION", "-"),
"filter=%s" % os.environ.get("TESTBRIDGE_TEST_ONLY", "-"), "tmp=%s" % os.path.isdir(os.environ["TEST_TMPDIR"]))
EOF
echo "== //t/... (manual tests are skipped by patterns)"
bazel test //t/... 2>&1 | grep -E 'PASSED|Executed'
echo "== --test_tag_filters=-integration --test_size_filters=small"
bazel test //t/... --test_tag_filters=-integration --test_size_filters=small 2>&1 | grep -E 'PASSED|Executed'
echo "== a manual target named explicitly"
bazel test //t:e2e_test 2>&1 | grep -E 'PASSED|Executed'
echo "== streamed output, a filter and an extra argument"
bazel test //t:env_test --test_output=streamed --test_filter=Checkout --test_arg=--verbose \
--nocache_test_results 2>&1 | grep -E '^//t:|args='== //t/... (manual tests are skipped by patterns)
//t:db_test PASSED in 0.1s
//t:env_test PASSED in 0.1s
//t:unit_test PASSED in 0.1s
Executed 3 out of 3 tests: 3 tests pass.
== --test_tag_filters=-integration --test_size_filters=small
//t:env_test (cached) PASSED in 0.1s
//t:unit_test (cached) PASSED in 0.1s
Executed 0 out of 2 tests: 2 tests pass.
== a manual target named explicitly
//t:e2e_test PASSED in 0.1s
Executed 1 out of 1 test: 1 test passes.
== streamed output, a filter and an extra argument
//t:env_test args=['--fast', '--verbose'] region=eu filter=Checkout tmp=True
//t:env_test PASSED in 0.1s
What each knob did:
tags = ["manual"]removes a target from wildcard patterns like//...; it still runs when named explicitly. Use it for tests that need special machines or credentials.--test_tag_filters=-integrationand--test_size_filters=smallselect tests by tag and size without touching BUILD files (comma-separated,-excludes).envandargsin the BUILD file are fixed per test;--test_env=NAME=valueand--test_arg=…add more from the command line (--test_argcomes after the BUILD-fileargs).--test_filteris passed throughTESTBRIDGE_TEST_ONLY; GoogleTest, JUnit,go test(via rules_go) and pytest wrappers honour it, a plain script must read it itself.--nocache_test_results(or--cache_test_results=no) forces a rerun even when nothing changed.
Other useful tags: exclusive (run alone, nothing in parallel), requires-network (network access in the sandbox), no-sandbox, no-remote and no-cache (opt a test out of the sandbox, remote execution or caching; use them sparingly, they undo Bazel's guarantees). Flaky tests have their own attribute, covered in the next lesson.
Test yourself
What is the default timeout of a size = "small" test?
- 10 s
- 60 s
- 300 s
- No timeout
Which --test_output setting prints the logs of failing tests only?
- summary
- errors
- all
- streamed
A test is tagged manual. When does it run?
- Never
- Only when named explicitly, not via patterns like //...
- Only with --config=manual
- Only on CI