<<../../components/_v2_banner.md>>
The Fuchsia Component Framework allows developers to create components in a variety of languages and runtimes. Fuchsia's own code uses a diverse mix of programming languages for components, including C/C++, Rust, Dart, and Go.
The Test Runner Framework uses Component Framework runners as an integration layer between various testing runtimes and a common Fuchsia protocol for launching tests and receiving their results. This makes for an inclusive design that on one hand allows developers to bring their language and testing framework of choice, and on the other hand allows building and testing Fuchsia on a variety of systems and targeting different hardware.
The test_manager
component is responsible for running tests on a Fuchsia device. Test manager exposes the fuchsia.test.manager.RunBuilder
protocol, which allows launching test suites.
Each test suite is launched as a child of test manager. Test suites are offered capabilities by test manager that enable them to do their work while maintaining isolation between the test and the rest of the system. For instance hermetic tests are given the capability to log messages, but are not given the capability to interact with real system resources outside of their sandbox. Test manager uses only one capability from the test realm, a controller protocol that test suites expose. This is done to ensure hermeticity (test results aren‘t affected by anything outside of their intended sandbox) and isolation (tests don’t affect each other or the rest of the system).
The test manager controller itself is offered to other components in the system in order to integrate test execution with various developer tools. Tests can then be launched with such tools as fx test
and ffx
.
The test suite protocol, fuchsia.test.Suite
, is used by the test manager to control tests, such as to invoke test cases and to collect their results.
Test authors typically don't need to implement this protocol. Instead, they rely on a test runner to do this for them. For instance, you might write a test in C++ using the GoogleTest framework, and then use gtest_runner
in your component manifest to integrate with the Test Runner Framework.
Test runners are reusable adapters between the Test Runner Framework and common languages & frameworks used by developers to write tests. They implement the fuchsia.test.Suite
protocol on behalf of the test author, allowing developers to write idiomatic tests for their language and framework of choice.
Component manifests for simple unit tests can be generated by the build rules. Generated component manifests for v2 tests will include the appropriate test runner based on their build definition. For instance a test executable that depends on the GoogleTest library will include the GoogleTest runner in its generated manifest.
The following test runners are currently available for general use:
A runner for tests written in C/C++ using the GoogleTest framework. Use this for all tests written using GoogleTest.
Common GoogleTest features are supported, such as disabling tests, running only specified tests, running the same test multiple times, etc'. Standard output, standard error, and logs are captured from the test.
In order to use this runner, add the following to your component manifest:
{ include: [ "//src/sys/test_runners/gtest/default.shard.cml" ] }
By default GoogleTest test cases run serially (one test case at a time).
A runner for tests written in the Rust programming language and following Rust testing idioms. Use this for all idiomatic Rust tests (i.e. tests with modules that set the attribute [cfg(test)]
).
Common Rust testing features are supported, such as disabling tests, running only specified tests, running the same test multiple times, etc'. Standard output, standard error, and logs are captured from the test.
In order to use this runner, add the following to your component manifest:
{ include: [ "//src/sys/test_runners/rust/default.shard.cml" ] }
By default Rust test cases run in parallel, at most 10 cases at a time.
A runner for tests written in the Go programming language and following Go testing idioms. Use this for all tests written in Go using import "testing"
.
Common Go testing features are supported, such as disabling tests, running only specified tests, running the same test multiple times, etc'. Standard output, standard error, and logs are captured from the test.
In order to use this runner, add the following to your component manifest:
{ include: [ "//src/sys/test_runners/gotests/default.shard.cml" ] }
By default Go test cases run in parallel, at most 10 cases at a time.
The simplest test runner - it waits for your program to terminate, then reports that the test passed if the program returned zero or that it failed for any non-zero return value.
Use this test runner if your test is implemented as an ELF program (for instance an executable written in C/C++) but it does not use a common testing framework that‘s supported by existing runners and you’d rather not implement a bespoke test runner.
In order to use this runner, add the following to your component manifest:
{ include: [ "//src/sys/test_runners/elf/default.shard.cml" ] }
Legacy tests are tests that were written before the Test Runner Framework was introduced. The legacy test runner offers a simple adapter between the modern test framework and legacy tests that were not converted to modern ones. For help with migrations see this guide. It is not recommended to use the legacy test runner in new tests.
The legacy test runner detects if a test passed or failed by observing its return code, with zero indicating success and non-zero indicating failure.
You can follow the example below to wrap a legacy test with the legacy test runner.
// Create this new file, simple_test.cml { include: [ "//src/sys/test_runners/legacy_test/default.shard.cml", ], program: { // Reference your existing test component manifest simple_test.cmx legacy_manifest: "meta/simple_test.cmx", }, }
Then change your BUILD.gn
build definitions as follows:
import("//build/components.gni") # This is your existing legacy test fuchsia_test_component("simple_test_legacy") { component_name = "simple_test" manifest = "meta/simple_test.cmx" deps = [ ":simple_test_bin" ] } # Add this wrapper fuchsia_test_component("simple_test_modern") { component_name = "simple_test" manifest = "meta/simple_test.cml" deps = [ ":simple_test_legacy" ] } # Specify the wrapper as the test component in your test package fuchsia_test_package("simple_test") { test_components = [ ":simple_test_modern" ] # Note that if simple_test_legacy was in test_components before, then it's now # replaced with the wrapper. }
The legacy test runner does not understand concepts such as test cases (or filtering on them), running multiple test cases in parallel, etc. It does however forward arguments to the test, so you can pass arguments that are specific to the underlying test framework. For instance, to run just a specific test case from a gtest:
fx test <test> -- --gtest_filter=MyTestCase
To run Rust tests, at most 5 test cases at a time:
fx test <test> -- --test-threads=5
When using fx test
to launch tests, they may run each test case in sequence or run multiple test cases in parallel up to a given limit. The default parallelism behavior is determined by the test runner. To manually control the number of test cases to run in parallel use test spec:
fuchsia_test_package("my-test-pkg") { test_components = [ ":my_test_component" ] test_specs = { # control the parallelism parallel = 10 } }
To override the value specified in the test spec, pass the parallel option when invoking fx test:
fx test --parallel=5 <test_url>
To run a test multiple times use:
fx test --count=<n> <test_url>
If an iteration times out, no further iteration will be executed.
Custom arguments to the tests can be passed using fx test
:
fx test <test_url> -- <custom_args>
Individual test runners have restrictions on these custom flags:
Note the following known behavior change:
--gtest_break_on_failure: As each test case is executed in a different process, this flag will not work.
The following flags are restricted and the test fails if any are passed as fuchsia.test.Suite provides equivalent functionality that replaces them.
fx test --test-filter=<glob_pattern> <test_url>
--test-filter
may be specified multiple times. Tests that match any of the given glob patterns will be executed.
fx test --also-run-disabled-tests <test_url>
The following flags are restricted and the test fails if any are passed as fuchsia.test.Suite provides equivalent functionality that replaces them.
Note the following known behavior change:
-test.failfast: As each test case is executed in a different process, this flag will only influence sub-tests.
The following flags are restricted and the test fails if any are passed as fuchsia.test.Suite provides equivalent functionality that replaces them
fx test --test-filter=<glob_pattern> <test_url>
--test-filter
may be specified multiple times. Tests that match any of the given glob patterns will be executed.
Fuchsia aims to be inclusive, for instance in the sense that developers can create components (and their tests) in their language and runtime of choice. The Test Runner Framework itself is language-agnostic by design, with individual test runners specializing in particular programming languages or test runtimes and therefore being language-inclusive. Anyone can create and use new test runners.
Creating new test runners is relatively easy, with the possibility of sharing code between different runners. For instance, the GoogleTest runner and the Rust runner share code related to launching an ELF binary, but differ in code for passing command line arguments to the test and parsing the test's results.
To use temporary storage in your test, add the following to your component manifest:
{ include: [ "//src/sys/test_runners/tmp_storage.shard.cml" ] }
At runtime, your test will have read/write access to /tmp
. The contents of this directory will be empty when the test starts, and will be deleted after the test finishes.
Tests that don't specify a custom manifest and instead rely on the build system to generate their component manifest can add the following dependency:
fuchsia_unittest_package("foo-tests") { deps = [ ":foo_test", "//src/sys/test_runners:tmp_storage", ] }
A test is hermetic if it uses or offers no capabilities from the test root's parent. As a rule of thumb, tests should be hermetic, but sometimes a test requires a capability that cannot be injected in the test realm.
In the context of hermetic tests, a capability that originates from outside of the test's realm is called a system capability.
There are some capabilities which all tests can use which do not violate test hermeticity:
Protocol | Description |
---|---|
fuchsia.boot.WriteOnlyLog | Write to kernel log |
fuchsia.logger.LogSink | Write to syslog |
fuchsia.process.Launcher | Launch a child process from the test package |
fuchsia.sys2.EventSource | Access to event protocol |
To use these capabilities, there should be a use declaration added to test's manifest file:
// my_test.cml { use: [ ... { protocol: [ "{{ '<var label="protocol">fuchsia.logger.LogSink</var>' }}" ], }, ], }
Tests are also provided with some default storage capabilities which are destroyed after the test finishes execution.
Storage Capability | Description | Path |
---|---|---|
data | Isolated data storage directory | /data |
cache | Isolated cache storage directory | /cache |
temp | Isolated in-memory temporary storage directory | /tmp |
Add a use declaration in test's manifest file to use these capabilities.
// my_test.cml { use: [ ... { storage: "{{ '<var label="storage">data</var>' }}", path: "{{ '<var label="storage path">/data</var>' }}", }, ], }
The framework also provides some capabilities to all the components and can be used by test components if required.
When writing a test runner that launches processes, the runner needs to provide a library loader implementation.
Test runners typically launch individual test cases in separate processes to achieve a greater degree of isolation between test cases. However this can come at a significant performance cost. To mitigate this, the test runners listed above use a caching loader service which reduces the extra overhead per process launched.
Components in the test realm may play various roles in the test, as follows:
fuchsia.test.Suite
protocol. This role may be, but is not necessarily, owned by the test root.