Google is committed to advancing racial equity for Black communities. See how.

Testing FIDL protocols


This tutorial builds on the HLCPP getting started tutorials.


This tutorial walks through the process of writing a test for the Echo.EchoString method. This tutorial demonstrates the usage of two utilities available for testing FIDL protocols implemented in HLCPP:

  • The gtest test loop fixture
  • sys::testing::ComponentContextProvider
  • The fidl_test_base.h file provided by the HLCPP bindings

If you would like to write the code as you follow along, feel free to delete the existing example code:

rm -r examples/fidl/hlcpp/testing/*

The test will be written in examples/fidl/hlcpp/testing/

Set up dependencies

  1. Include the libraries that are needed for the test:

    #include <fuchsia/examples/cpp/fidl.h>
    #include <fuchsia/examples/cpp/fidl_test_base.h>
    #include <lib/fidl/cpp/binding.h>
    #include <lib/gtest/test_loop_fixture.h>
    #include <lib/sys/cpp/testing/component_context_provider.h>
  2. Add a build rule for the test in examples/fidl/hlcpp/testing/

    # Copyright 2020 The Fuchsia Authors. All rights reserved.
    # Use of this source code is governed by a BSD-style license that can be
    # found in the LICENSE file.
    executable("bin") {
      testonly = true
      output_name = "example_hlcpp_protocol_test"
      sources = [ "" ]
      deps = [
    fuchsia_unittest_package("example-hlcpp-protocol-test") {
      manifest = "test.cmx"
      deps = [ ":bin" ]
    group("testing") {
      testonly = true
      deps = [ ":example-hlcpp-protocol-test" ]

Create a server implementation

Add an implementation for the Echo protocol that is tested:

  class EchoImpl : public fuchsia::examples::testing::Echo_TestBase {
  void EchoString(std::string value, EchoStringCallback callback) override { callback(value); }
  void NotImplemented_(const std::string& name) override {
    std::cout << "Not implemented: " << name << std::endl;

Rather than inheriting from fuchsia::examples::Echo, this implementation inherits from the corresponding test base class. This means that the implementation only needs to override the methods are being tested (in this case, EchoString), as well as the NotImplemented_ method, which is called if any of the request handler methods that are not overriden gets called.

Create a test class that wraps the logic of publishing the echo protocol:

class EchoServerInstance {
  explicit EchoServerInstance(std::unique_ptr<sys::ComponentContext> context) {
    binding_ = std::make_unique<fidl::Binding<fuchsia::examples::Echo>>(&impl_);
    fidl::InterfaceRequestHandler<fuchsia::examples::Echo> handler =
        [&](fidl::InterfaceRequest<fuchsia::examples::Echo> request) {

  EchoImpl impl_;
  std::unique_ptr<fidl::Binding<fuchsia::examples::Echo>> binding_;

This is similar to the code that is explained in the server tutorial, but the fidl::Binding is owned by the class so that the binding's destructor gets called when the class is destroyed. This enables the code to publish the echo protocol on each test case given a new instance of the test component context.

Implement the text fixture class

class EchoTestFixture : public gtest::TestLoopFixture {
  void SetUp() override {
    echo_instance_.reset(new EchoServerInstance(provider_.TakeContext()));

  void TearDown() override {

  fuchsia::examples::EchoPtr GetProxy() {
    fuchsia::examples::EchoPtr echo;
    return echo;

  std::unique_ptr<EchoServerInstance> echo_instance_;
  sys::testing::ComponentContextProvider provider_;

The test fixture does the following:

  • Holds an instance of a ComponentContextProvider. Each test, it uses it to create a new test context, and binds the Echo implementation to it using the EchoServerInstance class.
  • Provides a GetProxy() method initializes a proxy to the current test component context and returns it.

Add tests

Here is an example test that can be written using the text fixture:

TEST_F(EchoTestFixture, EchoString) {
  fuchsia::examples::EchoPtr proxy = GetProxy();
  proxy->EchoString("hello there",
                    [&](std::string response) { ASSERT_EQ(response, "hello there"); });

Run the test:

  1. Configure your gn build to include the test:

    fx set core.x64 --with //examples/fidl/hlcpp/testing

  2. Run the test:

    fx test -vo example-hlcpp-protocol-test

You should see the test output indicating a success.


  • The gtest::TestLoopFixture removes the need for boilerplate async loop setup code. Each test case can simply call RunLoopUntilIdle() instead of manually managing an async::Loop.
  • The ComponentContextProvider makes it easy to mock the component context during a test. This is useful to e.g. provide specific capabilities to the a component.
  • The HLCPP bindings test scaffolding provides a test base for each protocol class that has a dummy implementation for each method. This allows tests to only implement the methods under test.