blob: 3bbe8516b383899b89f0ba466ce712c785fbf147 [file] [view]
# Test metadata
Fuchsia uses `TESTING.json5` files to provide additional metadata for tests on
a per-directory basis. A `TESTING.json5` file can contain two independent kinds
of metadata:
- [Coverage metadata](#coverage): Categories and tags used to organize and
visualize test coverage in dashboards and reporting tools.
- [Presubmit tests](#presubmit): Which tests must run in presubmit when files
in a directory change, and which team owns them. This section is
experimental and subject to change until it is stabilized.
## Location
`TESTING.json5` files should be placed at the root of a component,
subsystem, or major directory in the source tree. For example:
- `src/diagnostics/TESTING.json5`
Files placed in a directory apply to that directory and all its
subdirectories, unless overridden by another `TESTING.json5` file
deeper in the tree.
## File format
The file is a JSON5 document containing a single object with the following
optional top-level fields:
- `coverage`: Coverage metadata. See [Coverage metadata](#coverage).
- `presubmit`: Presubmit test declarations. This field is experimental. See
[Presubmit tests](#presubmit).
The full schema is defined in
[`//tools/testing_metadata/testing_input.schema.json`](/tools/testing_metadata/testing_input.schema.json).
Every `TESTING.json5` file must be formatted by `fx format-code`.
`TESTING.json5` files are validated during the build by
[`//tools/testing_metadata`](/tools/testing_metadata), which writes the
computed metadata of every directory to `testing_metadata.json` in the build
directory.
## Coverage metadata {#coverage}
The primary purpose of coverage metadata is to associate a directory (and the
tests within or under it) with a specific category and optionally other
metadata.
The primary use cases include:
- Visualizing test coverage across different areas of the operating system.
- Identifying areas that lack tests or have uncategorized tests.
### Format {#coverage-format}
The `coverage` field contains zero or more of the following fields:
- `category`: An optional string representing the category.
- `subcategory`: An optional string providing a more specific subcategory.
- `tags`: An optional list of strings representing tags.
- `inherit_tags`: An optional boolean, `true` by default. See
[Overriding behavior](#coverage-overriding).
Here is the schema:
```json5
{
coverage: {
category: "Category Name",
subcategory: "Subcategory Name",
tags: ["tag1", "tag2"],
},
}
```
### Overriding behavior {#coverage-overriding}
Coverage metadata applies to all subdirectories, but individual fields can be
overridden as follows:
- The `coverage.category` and `coverage.subcategory` fields are exclusive.
If a subdirectory has one of those fields set, it overrides the matching
field from the nearest `TESTING.json5` parent.
- The `coverage.tags` field is additive. If a subdirectory has a
`coverage.tags` field, the tags are merged with the tags from each ancestor
`TESTING.json5` file, unless `coverage.inherit_tags` is set to `false`.
### Viewing categories
The `fx test-category` tool can be used to view the computed categories for
any directory in the tree. Use this tool to verify that the computed categories
look reasonable and to find uncategorized directories.
Pass a directory to the tool to see the final computed categories:
```posix-terminal
fx test-category src/diagnostics/archivist
```
This shows output:
```none {:.devsite-disable-click-to-copy}
src/diagnostics/archivist:
category: "Diagnostics"
subcategory: "Archivist"
tags: []
```
Pass the `--stats` flag to show instead the aggregated statistics
for the given subdirectory or the whole tree:
```posix-terminal
fx test-category src/diagnostics --stats
```
This shows output:
```none {:.devsite-disable-click-to-copy}
Categories and Subcategories:
Diagnostics: 214
Archivist: 45
Test Only: 38
None: 32
Libraries: 26
Detect: 18
Utilities: 16
Sampler: 16
Persistence: 9
Triage: 9
Tools: 5
Tags:
lib: 26
```
Pass the `--web` flag to launch a simple web page that allows you
to interactively navigate and view categories for the tree:
```posix-terminal
fx test-category --web
```
This shows output:
```none {:.devsite-disable-click-to-copy}
View categories at http://localhost:6240?files=metadata.json
Not seeing the categories you expect? Run `touch BUILD.gn` and then `fx build`
Press enter to stop the server.
```
### Category style guide
The format of coverage metadata is intentionally loose to support incremental
refinement of categories over time, and getting categories wrong temporarily
is very low cost.
There are only a few hard rules to follow, which are enforced automatically:
- Every directory has a category.
This is satisfied by the top-level `//TESTING.json5` file.
- A directory may not have a subcategory if its category is "Uncategorized".
#### Suggested category layout
Consider the following recommendations:
- Provide categories for the top-level directory of a subsystem or component,
but not for every subdirectory.
For example, `src/diagnostics` can set `category: "Diagnostics"`, and
subdirectories `src/diagnostics/lib` and `src/diagnostics/archivist` simply
inherit that category.
- Categories may generally map to a team name, but do not have to. They should
generally match the directory naming.
For example, category "Diagnostics" matches the team who owns the directory.
- Set subcategories in subdirectories of a categorized directory. These should
relate to the purpose of the subdirectory.
For example, `src/diagnostics/archivist` sets
`subcategory: "Archivist"` to group all Archivist-related tests
together.
- Set tags for cross-team or cross-component concerns.
For example, `src/diagnostics/lib` and `src/lib/diagnostics` both set the
`"lib"` tag. We can then group coverage for all "libs" across the tree.
### Examples {#coverage-examples}
Given the following source layout:
```
src/my_team/
src/my_team/component1/
src/my_team/component2/
src/my_team/lib/
src/my_team/lib/component1_client
src/lib/my_team/server_library
```
Consider creating the following files:
```json5
// src/my_team/TESTING.json5
{
coverage: {
category: "My Team",
},
}
// src/my_team/component1/TESTING.json5
{
coverage: {
subcategory: "Component1",
},
}
// src/my_team/component2/TESTING.json5
{
coverage: {
subcategory: "Component2",
},
}
// src/my_team/lib/TESTING.json5
{
coverage: {
tags: ["lib"],
},
}
// src/lib/my_team/TESTING.json5
{
coverage: {
category: "My Team",
tags: ["lib"],
},
}
```
## Presubmit tests {#presubmit}
Caution: The `presubmit` section is experimental. Its format, defaults and
validation rules are subject to change until it is stabilized.
The `presubmit` field declares tests that must run in presubmit when files in
or under the directory change, and the team that owns them. This keeps
presubmit test selection next to the code it covers.
### Teams {#teams}
Every presubmit test is owned by a team, named by the `presubmit.team` field.
`TESTING.json5` files only contain team names: there is no registry of teams
in the source tree. Team names are mapped to their configuration, such as
contact information, out of band. This lets teams and subteams be named
anywhere in the tree without changing a central file.
### Format {#presubmit-format}
The `presubmit` field contains the following fields:
- `team`: An optional string naming the team that owns these tests. See
[Teams](#teams). If unset, the team is inherited from the nearest ancestor
that sets one. It is an error to declare `tests` in a directory without an
owning team.
- `inherit_tests`: An optional boolean, `true` by default. See
[Overriding behavior](#presubmit-overriding).
- `tests`: An optional list of test entries, described below.
Each entry in `tests` contains the following fields:
- `targets` (required): A non-empty list of test labels to run. Labels may
be absolute (`//src/foo:foo_tests`) or relative to the directory of the
`TESTING.json5` file that declares them (`:foo_tests`, `bar:bar_tests`),
including when the test entry is inherited by a subdirectory. Labels must
not have a toolchain suffix and must not contain `..`. Labels are matched
against the `source_label` of the entries in `tests.json`, so they work for
both GN and Bazel tests. If a label matches several tests (for example, the
host and device builds of the same test), all of them run. Labels may also
name a GN `group()` (such as the conventional `group("tests")`) or a Bazel
`test_suite()`, which is expanded recursively to all the tests it contains.
See [Test groups](#test-groups).
- `paths`: A list of globs, relative to the directory containing the
`TESTING.json5` file, of changed files that trigger these tests. Globs must
not start with `/` or contain `..`. Defaults to `["**"]`, which matches
every file in the directory and its subdirectories.
- `external_paths`: A list of globs, relative to the source root and starting
with `//`, of changed files outside of the directory that trigger these
tests. Defaults to `[]`. See [External paths](#external-paths).
- `exclude_paths`: A list of globs of changed files that never trigger these
tests, even if they match `paths` or `external_paths`. Globs may be relative
to the directory containing the `TESTING.json5` file or absolute (starting
with `//`). Defaults to `["**/*.md", "**/OWNERS", "**/TESTING.json5"]`.
Setting this field replaces the default list, so include the defaults if
you still want them. Set it to `[]` to exclude nothing.
Each test runs in all the environments declared for it in the build. There
is no way to restrict the environments of a test entry yet. See
[Decisions](#presubmit-decisions).
A changed file triggers a test entry if it matches at least one glob in
`paths` or `external_paths` and does not match any glob in `exclude_paths`.
Relative globs are always resolved against the directory of the
`TESTING.json5` file that declares them, including when the test entry is
inherited by a subdirectory. Relative `exclude_paths` globs are matched
against the path relative to that directory for files under it, and against
the path relative to the source root for files matched by `external_paths`.
### Overriding behavior {#presubmit-overriding}
Presubmit declarations apply to all subdirectories, but individual fields can
be overridden as follows:
- The `presubmit.team` field is exclusive and overrides the team from the
nearest ancestor that sets one.
- The `presubmit.tests` field is additive. Tests from each ancestor
`TESTING.json5` file are added to the tests listed, unless
`presubmit.inherit_tests` is set to `false`, in which case only the tests
listed in this file apply to this directory and its subdirectories.
- A `TESTING.json5` file without a `presubmit` field, for example one that
only contains coverage metadata, inherits the team and all the tests of
its ancestors.
### Test groups {#test-groups}
To avoid listing many individual tests, `targets` can reference existing
test aggregation targets instead of a new rule type:
- A GN `group()` is expanded to every test in its transitive dependencies
that appears in `tests.json`.
- A Bazel `test_suite()` is expanded recursively, as with
`bazel query 'tests(//path:suite)'`. Following the
[Bazel test style guide](/docs/development/languages/bazel/style/tests.md),
tests outside the current package must be referenced through their
aggregation `test_suite()` targets.
```json5
{
presubmit: {
tests: [
// GN group, expanded recursively.
{ targets: [":tests"] },
// Bazel aggregation test_suite(), expanded recursively.
{ targets: ["//src/connectivity/bluetooth:bluetooth_tests"] },
],
},
}
```
The expanded set is always intersected with `tests.json`, so only tests that
are part of the current build run. It is an error if a label expands to no
tests. If a team needs a presubmit-specific subset of its tests, define a
dedicated GN `group()` or Bazel `test_suite()` (for example,
`presubmit_tests`) and reference it here.
Broad groups combined with the default `paths` can select many tests. Use
narrower groups or `paths` to limit which tests run.
### External paths {#external-paths}
`external_paths` lets a test be triggered by changes outside of the directory
that declares it, including changes in another repository, without that other
repository knowing about the test. For example, a test in `fuchsia.git` can be
triggered by changes to a library under `//third_party` that it depends on.
This is also how tests in non-public repositories such as `//vendor/google`
are triggered by changes in `fuchsia.git`, without exposing their structure
in public files:
```json5
// vendor/google/connectivity/wlan/TESTING.json5
{
presubmit: {
team: "wlan",
tests: [
{
targets: [":wlan_hw_integration_tests"],
external_paths: [
"//src/connectivity/wlan/**",
"//sdk/fidl/fuchsia.wlan.*/**",
],
},
],
},
}
```
The following rules apply:
- References to non-public repositories must point from the non-public
repository to the public one, never the other way around. `TESTING.json5`
files outside of `//vendor` must not reference `//vendor` in any field.
- Avoid overly broad globs such as `//**`, which would trigger the tests on
every change.
- Files in another repository can be moved or deleted without updating the
globs that reference them. Globs that match no files are reported to the
owning team.
### Examples {#presubmit-examples}
Given the following source layout:
```
src/my_team/
src/my_team/component1/
src/my_team/tools/
```
Consider creating the following files:
```json5
// src/my_team/TESTING.json5
{
presubmit: {
team: "my-team",
tests: [
// Run all the tests of the team when any file changes.
{ targets: [":tests"] },
],
},
}
// src/my_team/component1/TESTING.json5
{
presubmit: {
tests: [
// Also run the hardware tests of the component when its Rust sources
// change.
{
targets: [":component1_hw_tests"],
paths: ["**/*.rs"],
},
],
},
}
// src/my_team/tools/TESTING.json5
{
presubmit: {
// The tools are self-contained: only run their own tests.
inherit_tests: false,
tests: [
{ targets: [":tests"] },
],
},
}
```
### Validation and output {#presubmit-validation}
Note: These rules and the output format are experimental and subject to
change until the `presubmit` section is stabilized.
In addition to the schema, `//tools/testing_metadata` checks the following
rules during the build:
- Every directory that declares `presubmit.tests` has an owning team.
- Globs are valid.
- `TESTING.json5` files outside of `//vendor` do not reference `//vendor`.
The `testing_metadata.json` output contains a `presubmit_tests` list with
every test entry, with defaults applied and `paths` and `targets` made
absolute. Each directory has a `presubmit` object with its owning `team` and
the indices of the `presubmit_tests` entries that apply to it. Expanding test
groups, checking `targets` against `tests.json` and mapping team names to
their configuration are not done by this tool. Checks that depend on
`tests.json` are done across all builds in presubmit before `TESTING.json5`
changes are merged.
### Decisions {#presubmit-decisions}
This section records design decisions about the `presubmit` section and their
rationale.
- **No `environments` filter.** An earlier proposal let each test entry list
the environments, such as `vim3_env` or `linux_env`, that its tests run in.
To keep the format simple, the field was dropped: each test runs in all the
environments declared for it in the build. A filter can be added later if
a concrete need arises, in which case omitting it must keep meaning "all
environments".