<<../../../_common/fidl/_testing_intro.md>>
Below is an example component manifest for a simple integration test component:
meta/integration_tests.cml
:
{ include: [ "//pkg/syslog/client.shard.cml", "//pkg/sys/testing/elf_test_runner.shard.cml", ], program: { binary: "bin/client_test", }, children: [ { name: "service", url: "fuchsia-pkg://fuchsia.com/foo-package-tests#meta/mock_service.cm", }, { name: "client", url: "fuchsia-pkg://fuchsia.com/foo-package-tests#meta/foo_client.cm", }, ], offer: [ { protocol: "fuchsia.example.Foo", from: "#service", to: [ "#client" ], }, ], }
This test component declaration contains the following key elements:
include
of the necessary language-specific test runner shard. This enables the test manager to properly execute the test suite.children
.The Fuchsia SDK provides additional templates to facilitate the creation of integration test components:
fuchsia_cc_test()
: Compiles the C++ source code into a test binary.fuchsia_test_component()
: Generates a Fuchsia component containing tests using the provided component manifest. You can combine multiple test components into the same fuchsia_test_package()
.Here is an example of how the above integration test could be included in the BUILD.bazel
file:
load( "fuchsia_cc_test", "fuchsia_component", "fuchsia_component_manifest", "fuchsia_test_component", "fuchsia_test_package", ) # Component under test fuchsia_component( name = "foo_client", manifest = ":foo_client_manifest", visibility = ["//visibility:public"], ) # Test dependencies fuchsia_component( name = "mock_service", manifest = ":mock_service_manifest", visibility = ["//visibility:public"], ) # Component containing integration tests fuchsia_cc_test( name = "client_integration_test", srcs = [ ... ], deps = [ ... ], ) fuchsia_component_manifest( name = "integration_test_manifest", src = "meta/integration_tests.cml", ) fuchsia_test_component( name = "integration_test_component", manifest = ":integration_test_manifest", test_name = "client_integration_test", visibility = ["//visibility:public"], deps = [":client_integration_test"], ) # Hermetic test package fuchsia_test_package( name = "integration_test_pkg", visibility = ["//visibility:public"], deps = [ ":foo_client", ":mock_service", ":integration_test_component", ], )
This integration test build configuration contains the following key elements:
fuchsia_test_component()
target describing the integration test component and its component manifest.fuchsia_component()
targets representing component dependencies requited by the integration tests.fuchsia_test_package()
that bundles the test component and all dependencies together.In this exercise, you'll add an integration test component to exercise the FIDL protocol interface of the echo_server
component with the Test Runner Framework and run those tests in a FEMU environment.
To begin, create a new project directory in your Bazel workspace:
mkdir -p fuchsia-codelab/echo-integration
After you complete this section, the project should have the following directory structure:
//fuchsia-codelab/echo-integration |- BUILD.bazel |- meta | |- echo_integration_test.cml | |- echo_integration_test.cc
Create the echo-integration/meta/echo_integration_test.cml
test component manifest to declare the echo-server
component as a child and route the Echo
protocol capability to the test component.
echo-integration/meta/echo_integration_test.cml
:
{% includecode gerrit_repo="fuchsia/sdk-samples/getting-started" gerrit_path="src/routing/integration_tests/meta/echo_integration_test.cml" region_tag="example_snippet" adjust_indentation="auto" %}
Notice that the echo-server
instance comes from the same package as the integration test. This practice promotes test packages that are hermetic by avoiding dependencies on components from other packages.
Add the following rules to your BUILD.bazel
file to include the integration test component in the build configuration:
echo-integration/BUILD.bazel
:
{% set build_bazel_snippet %} {% includecode gerrit_repo=“fuchsia/sdk-samples/getting-started” gerrit_path=“src/routing/integration_tests/BUILD.bazel” region_tag=“imports” adjust_indentation=“auto” %}
{% includecode gerrit_repo=“fuchsia/sdk-samples/getting-started” gerrit_path=“src/routing/integration_tests/BUILD.bazel” region_tag=“cc_test” adjust_indentation=“auto” %}
{% includecode gerrit_repo=“fuchsia/sdk-samples/getting-started” gerrit_path=“src/routing/integration_tests/BUILD.bazel” region_tag=“component” adjust_indentation=“auto” %}
{% includecode gerrit_repo=“fuchsia/sdk-samples/getting-started” gerrit_path=“src/routing/integration_tests/BUILD.bazel” region_tag=“test_package” adjust_indentation=“auto” %} {% endset %}
{{ build_bazel_snippet |replace("//src/routing/fidl","//fuchsia-codelab/echo-fidl") |replace("//src/routing/cpp/echo_server","//fuchsia-codelab/echo-server") |trim() }}
The integration test connects to the Echo
protocol exposed by the echo-server
in the same way as the client component, sends a string request, and validates the expected response.
Add the following code to implement an integration test:
echo-integration/echo_integration_test.cc
:
{% includecode gerrit_repo="fuchsia/sdk-samples/getting-started" gerrit_path="src/routing/integration_tests/echo_integration_test.cc" region_tag="example_snippet" adjust_indentation="auto" %}
Build and publish the integration test package to the fuchsiasamples.com
repository:
bazel run //fuchsia-codelab/echo-integration:test_pkg.publish -- \ --repo_name fuchsiasamples.com
The fuchsia_test_package()
rule generates a package with the test component and its dependencies. The integration test component has the following URL:
fuchsia-pkg://fuchsiasamples.com/echo_integration_test#meta/echo_integration_test.cm
Use the ffx test
command to execute the integration tests. Verify that the tests pass:
ffx test run \ fuchsia-pkg://fuchsiasamples.com/echo_integration_test#meta/echo_integration_test.cm