aboutsummaryrefslogtreecommitdiff
path: root/docs/source/test_suite.md
blob: 2bd9d266c9a1af15d2c004d53c2cce23475d9454 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Test suites

The `test_suite` macro is a front-end for easily instantiating groups of
Starlark tests. It can handle both analysis tests and unit tests. Under the
hood, each test is its own target with an aggregating `native.test_suite`
for the group of tests.

## Basic tests

Basic tests are tests that don't require any custom setup or attributes. This is
the common case for tests of utility code that doesn't interact with objects
only available to rules (e.g. Targets). These tests are created using
`unit_test`.

To write such a test, simply write a `unit_test` compatible function (one that
accepts `env`) and pass it to `test_suite.basic_tests`.

```starlark
# BUILD

load(":my_tests.bzl", "my_test_suite")
load("@rules_testing//lib:test_suite.bzl", "test_suite")

my_test_suite(name = "my_tests")

# my_tests.bzl

def _foo_test(env):
  env.expect.that_str(...).equals(...)

def my_test_suite(name):
  test_suite(
    name = name,
    basic_tests = [
      _foo_test,
    ]
  )
```

Note that it isn't _required_ to write a custom test suite function, but doing
so is preferred because it's uncommon for BUILD files to pass around function
objects, and tools won't be confused by it.

## Regular tests

A regular test is a macro that acts as a setup function and is expected to
create a target of the given name (which is added to the underlying test suite).

The setup function can perform arbitrary logic, but in the end, it's expected to
call `unit_test` or `analysis_test` to create a target with the provided name.

If you're writing an `analysis_test`, then you're writing a regular test.

```starlark
# my_tests.bzl
def _foo_test(name):
  analysis_test(
    name = name,
    impl = _foo_test_impl,
    attrs = {"myattr": attr.string(default="default")}
  )

def _foo_test_impl(env):
  env.expect.that_str(...).equals(...)

def my_test_suite(name):
  test_suite(
    name = name,
    tests = [
      _foo_test,
    ]
  )
```

Note that a using a setup function with `unit_test` test is not required to
define custom attributes; the above is just an example. If you want to define
custom attributes for every test in a suite, the `test_kwargs` argument of
`test_suite` can be used to pass additional arguments to all tests in the suite.