xref: /aosp_15_r20/external/bazelbuild-rules_testing/lib/test_suite.bzl (revision d605057434dcabba796c020773aab68d9790ff9f)
1"""# Test suite
2
3Aggregates multiple Starlark tests in a single test_suite.
4"""
5
6load("//lib/private:util.bzl", "get_test_name_from_function")
7load("//lib:unit_test.bzl", "unit_test")
8
9def test_suite(name, *, tests = [], basic_tests = [], test_kwargs = {}):
10    """Instantiates given test macros/implementations and gathers their main targets into a `test_suite`.
11
12    Use this function to wrap all tests into a single target.
13
14    ```
15    def simple_test_suite(name):
16      test_suite(
17          name = name,
18          tests = [
19              your_test,
20              your_other_test,
21          ]
22      )
23    ```
24
25    Then, in your `BUILD` file, simply load the macro and invoke it to have all
26    of the targets created:
27
28    ```
29    load("//path/to/your/package:tests.bzl", "simple_test_suite")
30    simple_test_suite(name = "simple_test_suite")
31    ```
32
33    Args:
34        name: (str) The name of the suite
35        tests: (list of callables) Test macros functions that
36            define a test. The signature is `def setup(name, **test_kwargs)`,
37            where (positional) `name` is name of the test target that must be
38            created, and `**test_kwargs` are the additional arguments from the
39            test suite's `test_kwargs` arg. The name of the function will
40            become the name of the test.
41        basic_tests: (list of callables) Test implementation functions
42            (functions that implement a test's asserts). Each callable takes a
43            single positional arg, `env`, which is information about the test
44            environment (see analysis_test docs). The name of the function will
45            become the name of the test.
46        test_kwargs: (dict) Additional kwargs to pass onto each test (both
47            regular and basic test callables).
48    """
49    test_targets = []
50
51    for setup_func in tests:
52        test_name = get_test_name_from_function(setup_func)
53        setup_func(name = test_name, **test_kwargs)
54        test_targets.append(test_name)
55
56    for impl in basic_tests:
57        test_name = get_test_name_from_function(impl)
58        unit_test(name = test_name, impl = impl, **test_kwargs)
59        test_targets.append(test_name)
60
61    native.test_suite(
62        name = name,
63        tests = test_targets,
64    )
65