整合測試拓撲

整合測試情境包含兩個以上的元件 相同的領域和交換能力大多數測試都是單元 例如只針對單一元件進行整合測試 定義領域拓撲和能力路徑

測試 Runner 架構整合

整合測試元件會透過以下方式與 Test Runner Framework 整合 包括與支援語言特定測試架構相符的測試執行元件資料分割。

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",
    "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 計畫設為針對特定測試提供的測試執行元件 這個架構的重點在於

測試領域拓撲

整合測試元件可宣告測試領域的拓撲 當做父項這樣一來,測試控制器就會負責 能轉送測試元件及其依附元件

以下是 echo_server 整合測試的拓撲範例 元件:


整合測試拓撲

這是繫結至 fidl.examples.routing.echo.Echo 的簡易測試領域 由 echo_server 元件公開的通訊協定 echo_integration_test 套件包含下列元件:

  • echo_integration_test - 測試控制器元件
  • echo_server - 測試中的元件

您可以透過下列方式定義測試領域拓撲:

請參閱下表,判斷哪種方法最適合您的整合測試:

整合測試案例 運作範圍建構工具 靜態 CML
使用靜態依附元件進行簡易整合測試
每個測試案例的專屬元件執行個體
每個測試案例繫結的受測試元件生命週期
適用於受測試元件的動態虛假、模擬和虛設常式執行個體
測試案例之間的動態轉送與設定

運作範圍建構工具

必須在執行階段定義領域拓撲,或建立元件時 需改為本機模擬實作,您可以使用 Realm Builder 程式庫,在測試程式碼中動態建立拓撲。

測試控制器元件的資訊清單中包含 Realm Builder 程式庫,其中採用 的元件資訊清單資料分割:

Rust

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

C++

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

測試控制器程式碼會建構測試領域拓撲,並將 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>

// ...

auto 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{"fidl.examples.routing.echo.Echo"}},
                       .source = ChildRef{"echo_server"},
                       .targets = {ParentRef()}});

測試控制器程式碼會透過功能與 echo_server 互動 新建立的領域:

Rust

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

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

C++

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

auto echo = realm.component().ConnectSync<fidl::examples::routing::echo::Echo>();
fidl::StringPtr response;
echo->EchoString("hello", &response);
ASSERT_EQ(response, "hello");

如需使用 Realm Builder 導入測試的完整資訊,請參閱 Realm Builder 開發人員指南。

元件資訊清單

如果測試中的所有元件都是靜態的,您可以定義整個拓撲 。

測試控制器元件的資訊清單會以靜態方式宣告 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: [
        {
            protocol: [ "fuchsia.inspect.InspectSink" ],
            from: "parent",
            to: "#echo_server",
        },
    ],
}

C++

{
    include: [
        "//src/sys/test_runners/gtest/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_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",
        },
    ],

    // Capabilities required by components under test.
    offer: [
        {
            protocol: [ "fuchsia.inspect.InspectSink" ],
            from: "parent",
            to: "#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);
}

測試套件

受測試的所有元件都包含在相同的密封測試套件中。 這麼做的好處是能在不同環境中執行及更新測試 無需擔心依附元件未保持同步的問題

請參閱以下範例定義 fuchsia_test_package() 目標的 BUILD.gn 檔案:

Rust

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

  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 範本

測試元件單元件

元件的路徑名稱可識別元件拓撲中不重複的執行個體。 對於在測試拓撲內運作的元件,路徑名稱路徑相對於 測試領域中的根元件在上述範例中,根元件是 公開 fuchsia.test.Suite 通訊協定的測試控制器元件。

子路徑名稱格式取決於您的測試領域拓撲

  • 靜態 CML:以靜態方式宣告為根測試控制器子項的元件 只是以其元件 name 識別。
  • Realm Builder:Realm Builder 提供 測試控制器和子項元件進一步瞭解測試元件 看看 Realm Builder 開發人員指南