集成测试

集成测试 侧重于验证组件在与其他组件交互时的行为 系统组件。因此,集成测试通常与主组件分开构建,并且可以将被测组件和其他依赖项声明为子项。根据测试的性质 依赖项组件可能会以模拟或桩的形式提供, 案例仍是封闭的

测试组成部分

以下是一个简单集成测试组件的组件清单示例:

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" ],
        },
    ],
}

此测试组件声明包含以下关键元素:

  1. 必要的特定语言测试运行程序分片的 include。这个 使测试管理器能够正确执行测试套件。
  2. 将被测组件和依赖组件列为 children
  3. 在测试领域的组件之间路由所需的功能。

Fuchsia SDK 提供了额外的模板来 集成测试组件:

  • fuchsia_cc_test():将 C++ 源代码编译为测试二进制文件。
  • fuchsia_test_component():生成包含测试的 Fuchsia 组件 使用提供的组件清单您可以将多个测试组件组合到同一 fuchsia_test_package() 中。

以下示例展示了如何将上述集成测试添加到 BUILD.bazel 文件中:

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",
    ],
)

此集成测试 build 配置包含以下关键元素:

  1. 用于描述集成测试组件及其组件清单的 fuchsia_test_component() 目标。
  2. 其他 fuchsia_component() 目标,表示组件依赖项 所需的全部费用
  3. 捆绑测试组件的单个封闭 fuchsia_test_package() 以及所有依赖项在一起。

练习:Echo 服务器集成测试

在本练习中,您将添加一个集成测试组件,以便使用测试运行器框架来调用 echo_server 组件的 FIDL 协议接口,并在 FEMU 环境中运行这些测试。

添加集成测试组件

首先,在 Bazel 工作区中创建一个新的项目目录:

mkdir -p fuchsia-codelab/echo-integration

完成本部分后,项目应具有以下目录结构:

//fuchsia-codelab/echo-integration
                  |- BUILD.bazel
                  |- meta
                  |   |- echo_integration_test.cml
                  |
                  |- echo_integration_test.cc

创建测试组件清单

创建 echo-integration/meta/echo_integration_test.cml 测试组件 清单,用于将 echo-server 组件声明为子组件,并路由 Echo 测试组件的协议功能

echo-integration/meta/echo_integration_test.cml

{
    include: [
        "syslog/client.shard.cml",
        "sys/testing/elf_test_runner.shard.cml",
    ],

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

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

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

    // Capabilities required by components under test.
    offer: [
        {
            protocol: [ "fuchsia.logger.LogSink" ],
            from: "parent",
            to: "#echo_server",
        },

    ],
}

请注意,echo-server 实例来自与集成测试相同的软件包。这种做法可提升由以下 API 构成的封闭测试软件包: 避免依赖其他软件包中的组件

将以下规则添加到 BUILD.bazel 文件中,以便在 build 配置中添加集成测试组件:

echo-integration/BUILD.bazel

load(
    "@fuchsia_sdk//fuchsia:defs.bzl",
    "fuchsia_cc_test",
    "fuchsia_component_manifest",
    "fuchsia_test_component",
    "fuchsia_test_package",
)

fuchsia_cc_test(
    name = "echo_integration_test",
    size = "small",
    srcs = ["echo_integration_test.cc"],
    deps = [
        "//fuchsia-codelab/echo-fidl:examples.routing.echo.fidl_cc",
        "@com_google_googletest//:gtest_main",
        "@fuchsia_sdk//pkg/async-default",
        "@fuchsia_sdk//pkg/async-loop",
        "@fuchsia_sdk//pkg/async-loop-cpp",
        "@fuchsia_sdk//pkg/async-loop-default",
        "@fuchsia_sdk//pkg/fdio",
        "@fuchsia_sdk//pkg/component_incoming_cpp",
        "@fuchsia_sdk//pkg/syslog",
    ],
)

fuchsia_component_manifest(
    name = "test_manifest",
    src = "meta/echo_integration_test.cml",
    component_name = "echo_integration_test_component",
    includes = [
        "@fuchsia_sdk//pkg/sys/testing:elf_test_runner",
        "@fuchsia_sdk//pkg/syslog:client",
    ],
)

fuchsia_test_component(
    name = "echo_integration_test_component",
    manifest = ":test_manifest",
    visibility = ["//visibility:public"],
    deps = [":echo_integration_test"],
)

fuchsia_test_package(
    name = "test_pkg",
    package_name = "echo_integration_test",
    visibility = ["//visibility:public"],
    components = [
        "//fuchsia-codelab/echo-server:echo_server_component",
    ],
    test_components = [
        ":echo_integration_test_component",
    ],
)

实现集成测试

集成测试会连接到 Echo echo-server 发送字符串请求, 并验证预期响应。

添加以下代码以实现集成测试:

echo-integration/echo_integration_test.cc

#include <fidl/examples.routing.echo/cpp/fidl.h>
#include <gtest/gtest.h>
#include <lib/component/incoming/cpp/protocol.h>

#include <string>

TEST(EchoIntegrationTest, TestEcho) {
  zx::result client_end = component::Connect<examples_routing_echo::Echo>();
  ASSERT_TRUE(client_end.is_ok());
  fidl::SyncClient client{std::move(*client_end)};

  std::string request("Hello, world!");
  fidl::Result result = client->EchoString({request});
  ASSERT_TRUE(result.is_ok());

  auto response = result->response();
  ASSERT_TRUE(response.has_value());
  ASSERT_EQ(request, response.value());
}

更新 build 配置

构建集成测试软件包并将其发布到 fuchsiasamples.com 代码库:

bazel run //fuchsia-codelab/echo-integration:test_pkg.publish -- \
    --repo_name fuchsiasamples.com

运行集成测试

fuchsia_test_package() 规则会生成包含测试组件的软件包 及其依赖项集成测试组件具有以下网址:

fuchsia-pkg://fuchsiasamples.com/echo_integration_test#meta/echo_integration_test.cm

使用 ffx test 命令执行集成测试。验证测试是否通过:

ffx test run \
    fuchsia-pkg://fuchsiasamples.com/echo_integration_test#meta/echo_integration_test.cm