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