how do I select test to run when using `bazel test ...`

Viewed 2505

I have a repo using bazel as build and test system. This repo has both python and golang. There are two types of tests, unit tests, and integration tests. I would like to run them in two separate test steps in our CI. I would like to automatically discover new tests in the repo when new tests are added. we are currently using bazel test .... but this will not help me to split the unit test and integration test. Is there any rule or existing method to do this? Thanks.

2 Answers

--test_size_filters is the best way, because it is a wide used solution. If you need another separation, then tags are way to go:

py_test(
    name = "unit_test",
    tags = ["unit"],
)

py_test(
    name = "integration_test",
    tags = ["integration"],
)

And then

bazel test --test_tag_filters=unit //...
bazel test --test_tag_filters=integration //...
bazel test --test_tag_filters=-integration,-unit //... # each test which is not "unit" nor "integration"

Bazel doesn't really have a direct concept of unit vs integration testing, but it does have the concept of a test "size", or how "heavy" a test is. This docs page gives an outline of the size attribute on test rules while the Test encyclopedia gives a great overview.

When the tests are appropriately sized, it's then possible to use --test_size_filters flag to run the test for that size.

For example,

bazel test ... --test_size_filters=small for running unit tests

bazel test ... --test_size_filters=large for integration tests

You may want to add additional flags for unit tests vs integration tests, so adding a new config to .bazelrc might be a good idea, then run via bazel test ... --config=integration for example.

Related