集成测试拓扑

集成测试场景涉及在同一 realm 中运行并交换功能的两项或多项组件。虽然大多数测试都是仅涵盖单个组件的单元测试,但集成测试场景需要定义 realm 拓扑和功能路由。

测试运行程序框架集成

集成测试组件通过包含与支持的特定于语言的测试框架相匹配的测试运行程序分片,与测试运行程序框架集成。

Rust

include: [
    "//src/sys/test_runners/rust/default.shard.cml",
    "syslog/client.shard.cml",
],

// Information about the program to run.
program: {
    // The binary to run for this component.
    binary: "bin/echo_integration_test_rust",
},

C++

include: [
    "//src/sys/test_runners/gtest/default.shard.cml",
    "inspect/offer.shard.cml",
    "syslog/client.shard.cml",
],

// Information about the program to run.
program: {
    // The binary to run for this component.
    binary: "bin/echo_integration_test_cpp",
},

此分片提供两个关键元素:

  1. 公开了框架发现和执行测试用例所需的 fuchsia.test.Suite 协议。
  2. 将程序 runner 设置为针对给定测试框架提供的测试运行程序。

测试 realm 拓扑

集成测试组件声明了以自身为父级的测试 realm 的拓扑。这样一来,测试控制器就可以负责被测组件及其依赖项之间的功能路由。

以下是用于对 echo_server 组件进行集成测试的拓扑示例:


集成测试拓扑

这是一个简单的测试 realm,它绑定到 echo_server 组件公开的 fidl.examples.routing.echo.Echo 协议。echo_integration_test 软件包包含以下组件:

  • echo_integration_test - 测试控制器组件
  • echo_server - 受测组件

您可以通过以下方式定义测试 realm 拓扑:

下表可帮助您确定哪种方法最适合您的集成测试:

集成测试用例 Realm Builder 静态 CML
使用静态依赖项进行简单集成测试
每个测试用例的唯一组件实例
受测组件的生命周期与每个测试用例绑定
被测组件的动态虚构、模拟和桩实例
测试用例之间的动态路由和配置

Realm Builder

如果需要在运行时定义 realm 拓扑,或者需要使用本地模拟实现替换组件,您可以使用 Realm Builder 库在测试代码中动态创建拓扑。

测试控制器组件的清单使用其组件清单分片包含 Realm Builder 库:

Rust

include: [
    "sys/component/realm_builder.shard.cml",
    // ...
],

C++

include: [
    "sys/component/realm_builder.shard.cml",
    // ...
],

测试控制器代码构建测试 realm 拓扑,添加 echo_server 作为子组件,并声明返回到父组件的必要功能路由:

Rust

use {
    // ...
    fuchsia_component_test::{
        Capability, ChildOptions, LocalComponentHandles, RealmBuilder, Ref, Route,
    },
    futures::{StreamExt, TryStreamExt},
};

// ...

let builder = RealmBuilder::new().await?;

// Add component to the realm, which is fetched using a URL.
let echo_server = builder
    .add_child(
        "echo_server",
        "fuchsia-pkg://fuchsia.com/realm-builder-examples#meta/echo_server.cm",
        ChildOptions::new(),
    )
    .await?;

builder
    .add_route(
        Route::new()
            .capability(Capability::protocol_by_name("fidl.examples.routing.echo.Echo"))
            .from(&echo_server)
            .to(Ref::parent()),
    )
    .await?;

C++

#include <lib/sys/component/cpp/testing/realm_builder.h>

// ...

RealmBuilder builder = RealmBuilder::Create();

// Add component server to the realm, which is fetched using a URL.
builder.AddChild("echo_server",
                 "fuchsia-pkg://fuchsia.com/realm-builder-examples#meta/echo_server.cm");

builder.AddRoute(Route{.capabilities = {Protocol{.name = "fidl.examples.routing.echo.Echo"}},
                       .source = ChildRef{"echo_server"},
                       .targets = {ParentRef()}});

测试控制器代码通过所创建 Realm 公开的功能与 echo_server 进行交互:

Rust

let realm = builder.build().await?;

let echo: fecho::EchoProxy = realm.root.connect_to_protocol_at_exposed_dir()?;
assert_eq!(echo.echo_string(Some("hello")).await?, Some("hello".to_owned()));

C++

RealmRoot realm = builder.Build(dispatcher());

zx::result<fidl::ClientEnd<fidl_examples_routing_echo::Echo>> client_end =
    realm.component().Connect<fidl_examples_routing_echo::Echo>();
ASSERT_FALSE(client_end.is_error()) << client_end.status_string();
fidl::SyncClient echo_client(std::move(*client_end));

fidl::Result<::fidl_examples_routing_echo::Echo::EchoString> result =
    echo_client->EchoString({"hello"});
ASSERT_EQ(result->response(), "hello");

如需详细了解如何使用 Realm Builder 实现测试,请参阅 Realm Builder 开发者指南。

组件清单

如果测试中的所有组件都是静态的,您可以使用测试控制器组件清单中的 CML 以声明方式定义测试 realm 的整个拓扑。

测试控制器组件的清单静态声明了被测 echo_server 组件作为子级,并将必要的功能路由回父级:

Rust

{
    include: [
        "//src/sys/test_runners/rust/default.shard.cml",
        "syslog/client.shard.cml",
    ],

    // Information about the program to run.
    program: {
        // The binary to run for this component.
        binary: "bin/echo_integration_test_rust",
    },


    // Child components orchestrated by the integration test.
    children: [
        {
            name: "echo_server",
            url: "#meta/echo_server.cm",
        },
    ],

    // Capabilities used by this component.
    use: [
        {
            protocol: [ "fidl.examples.routing.echo.Echo" ],
            from: "#echo_server",
        },
    ],

    // Capabilities required by components under test.
    offer: [
        {
            dictionary: "diagnostics",
            from: "parent",
            to: "#echo_server",
        },
    ],
}

C++

{
    include: [
        "//src/sys/test_runners/gtest/default.shard.cml",
        "inspect/offer.shard.cml",
        "syslog/client.shard.cml",
    ],

    // Information about the program to run.
    program: {
        // The binary to run for this component.
        binary: "bin/echo_integration_test_cpp",
    },


    // Child components orchestrated by the integration test.
    children: [
        {
            name: "echo_server",
            url: "#meta/echo_server.cm",
        },
    ],

    // Capabilities used by this component.
    use: [
        {
            protocol: [ "fidl.examples.routing.echo.Echo" ],
            from: "#echo_server",
        },
    ],
}

测试控制器代码通过其公开的功能与 echo_server 进行交互:

Rust

use anyhow::Error;
use fidl_fidl_examples_routing_echo as fecho;
use fuchsia_component::client;

#[fuchsia::test]
async fn echo_integration_test() -> Result<(), Error> {
    const ECHO_STRING: &str = "Hello, world!";

    let echo = client::connect_to_protocol::<fecho::EchoMarker>()
        .expect("error connecting to echo server");
    let out = echo.echo_string(Some(ECHO_STRING)).await.expect("echo_string failed");

    assert_eq!(ECHO_STRING, out.unwrap());
    Ok(())
}

C++

#include <fidl/examples/routing/echo/cpp/fidl.h>
#include <lib/fidl/cpp/string.h>
#include <lib/sys/cpp/component_context.h>

#include <string>

#include <gtest/gtest.h>

TEST(EchoIntegrationTest, TestEcho) {
  ::fidl::examples::routing::echo::EchoSyncPtr echo_proxy;
  auto context = sys::ComponentContext::Create();
  context->svc()->Connect(echo_proxy.NewRequest());

  ::fidl::StringPtr request("Hello, world!");
  ::fidl::StringPtr response = nullptr;
  ASSERT_TRUE(echo_proxy->EchoString(request, &response) == ZX_OK);
  ASSERT_TRUE(request == response);
}

测试软件包

受测的所有组件都包含在同一封闭式测试软件包中。这样一来,您就可以在不同环境中运行和更新测试,而无需担心依赖项不同步。

请参阅以下 BUILD.gn 文件,其中定义了本示例的 fuchsia_test_package() 目标:

Rust

rustc_test("bin") {
  name = "echo_integration_test_rust"
  edition = "2024"

  deps = [
    "//examples/components/routing/fidl:echo_rust",
    "//src/lib/fuchsia",
    "//src/lib/fuchsia-component",
    "//third_party/rust_crates:anyhow",
  ]

  sources = [ "src/lib.rs" ]
}


fuchsia_component("echo_integration_test_component") {
  testonly = true
  component_name = "echo_integration_test"
  manifest = "meta/echo_integration_test.cml"
  deps = [ ":bin" ]
}


fuchsia_test_package("echo_integration_test_rust") {
  test_components = [ ":echo_integration_test_component" ]
  deps = [ "//examples/components/routing/rust/echo_server:echo_server_cmp" ]
}

C++

executable("bin") {
  output_name = "echo_integration_test_cpp"
  sources = [ "echo_integration_test.cc" ]
  deps = [
    "//examples/components/routing/fidl:echo_hlcpp",
    "//sdk/lib/async-loop:async-loop-cpp",
    "//sdk/lib/async-loop:async-loop-default",
    "//sdk/lib/sys/cpp",
    "//sdk/lib/sys/cpp/testing:unit",
    "//src/lib/fxl/test:gtest_main",
    "//third_party/googletest:gtest",
  ]
  testonly = true
}


fuchsia_component("echo_integration_test_component") {
  testonly = true
  component_name = "echo_integration_test"
  manifest = "meta/echo_integration_test.cml"
  deps = [ ":bin" ]
}


fuchsia_test_package("echo_integration_test_cpp") {
  test_components = [ ":echo_integration_test_component" ]
  deps = [ "//examples/components/routing/cpp/echo_server:echo_server_cmp" ]
}

组件使用以下变量内置到 fuchsia_test_package() 中:

  • test_components:包含公开 fuchsia.test.Suite 协议的测试的组件。
  • deps:集成测试所需的其他组件依赖项。

如需了解详情,请参阅测试软件包 GN 模板

测试组件别名

组件的 moniker 用于标识组件拓扑中的唯一实例。 对于在测试拓扑中运行的组件,monicker 路径相对于测试 realm 中的根组件。在上面的示例中,根组件是公开 fuchsia.test.Suite 协议的测试控制器组件。

子别名的格式取决于您的测试 realm 拓扑

  • 静态 CML:静态声明为根测试控制器子级的组件只需通过其组件 name 即可识别。
  • Realm Builder:Realm Builder 在测试控制器和子组件之间引入了一个中间集合。如需详细了解使用 Realm Builder 的测试组件 Moniker,请参阅 Realm Builder 开发者指南。