<<../../../_common/components/_tests_intro.md>>
Test runners are reusable adapters between the Test Runner Framework and common frameworks used by developers to write tests in their preferred language. Each test runner component exposes the fuchsia.test.Suite
capability that enables the test_manager
to enumerate and execute individual tests, and declares the appropriate execution runner
with test framework support.
{ // Execute tests using language-specific runner program: { runner: "elf_test_runner", }, // Expose test suite protocol to test manager capabilities: [ { protocol: "fuchsia.test.Suite" }, ], expose: [ { protocol: "fuchsia.test.Suite", from: "self", }, ], }
To simplify integration, the Test Runner Framework provides manifest shards for each language-specific runner. The following is an equivalent test runner CML for declaring the capabilities from the previous example component tests.
{ include: [ "sys/testing/elf_test_runner.shard.cml" ] }
Note: For complete details on the Test Runner Framework and component testing, see the testing documentation.
Unit testing{:.external} focuses on validating the individual units of code within your component and isolated from other components on the system. Unit tests should be hermetic, meaning that they do not require or provide additional capabilities outside of the test.
The Fuchsia SDK provides additional templates to facilitate unit testing components:
cc_test()
: Compiles the C++ source code into a test binary.fuchsia_test_component()
: Generates a Fuchsia component containing tests. You can combine multiple test components into the same fuchsia_package()
.Below is an example BUILD.bazel
snippet for including unit tests:
load( "fuchsia_cc_binary", "fuchsia_package", "fuchsia_test_component", ) cc_test( name = "hello_world_test", srcs = [ "hello_world_test.cc", ], ) fuchsia_test_component( name = "unit_test_component", test_name = "hello_world_test", deps = [ ":hello_world_test", ], ) fuchsia_package( name = "unit_test_pkg", visibility = ["//visibility:public"], deps = [ ":unit_test_component", ], )
In this exercise, you'll add unit tests to the echo
component with the Test Runner Framework and run those tests in a FEMU environment.
Unit tests verify that the internal functions of the component behave as expected. For the echo
component, you'll validate that the greeting()
function used in the previous exercise returns the expected values.
Add the following unit test functions to validate the behavior of the greeting()
function when supplied with one, two, or three arguments:
echo/echo_unittest.cc
:
{% includecode gerrit_repo="fuchsia/sdk-samples/getting-started" gerrit_path="src/echo/echo_unittest.cc" region_tag="imports" adjust_indentation="auto" %} #include "echo_component.h" {% includecode gerrit_repo="fuchsia/sdk-samples/getting-started" gerrit_path="src/echo/echo_unittest.cc" region_tag="test_mod" adjust_indentation="auto" %}
Add the following to your BUILD.bazel
file to include your tests in the build configuration:
BUILD.bazel
:
{% includecode gerrit_repo="fuchsia/sdk-samples/getting-started" gerrit_path="src/echo/BUILD.bazel" region_tag="imports" adjust_indentation="auto" %} # ... {% includecode gerrit_repo="fuchsia/sdk-samples/getting-started" gerrit_path="src/echo/BUILD.bazel" region_tag="unittest" adjust_indentation="auto" %}
This rule packages your unit tests into a component with the following URL:
fuchsia-pkg://fuchsiasamples.com/echo_unittests#meta/echo_unittests.cm
Run bazel build
and verify that the build completes successfully:
bazel build --config=fuchsia_x64 //fuchsia-codelab/echo:test_pkg \ --publish_to=$HOME/.package_repos/sdk-samples
Use the ffx test
command to execute the unit tests inside this package. Verify that the tests pass:
ffx test run \ fuchsia-pkg://fuchsiasamples.com/echo_unittests#meta/echo_unittests.cm