整合測試著重於驗證元件與系統中其他元件互動時的行為。因此,整合測試通常會與主要元件分開建構,並可能將測試中的元件和其他依附元件宣告為子項。視測試性質而定,依附元件可能會以模擬或虛設的方式提供,以便確保測試案例保持密封。
測試元件
以下是簡單整合測試元件的元件資訊清單範例:
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" ],
},
],
}
這個測試元件宣告包含下列關鍵元素:
- 所需語言特定測試執行元件資料分割的
include
。這可讓測試管理員正確執行測試套件。 - 將測試中的元件和相依元件列為
children
。 - 在測試領域中,將必要功能路由至元件之間。
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",
],
)
這項整合測試建構設定包含下列重要元素:
- 描述整合測試元件及其元件資訊清單的
fuchsia_test_component()
目標。 - 代表元件依附元件的其他
fuchsia_component()
目標 。 - 封裝測試元件的單一密封
fuchsia_test_package()
會整合所有依附元件
練習:Echo 伺服器整合測試
在本練習中,您將新增整合測試元件,透過 Test Runner Framework 測試 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
例項來自與
整合測試。這種做法可避免依附其他套件元件的依附元件,進而提升測試套件的密封性。
在 BUILD.bazel
檔案中新增下列規則,以納入整合功能
測試元件:
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-server
公開的 Echo
通訊協定,傳送字串要求,並驗證預期的回應。
加入下列程式碼來實作整合測試:
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());
}
更新建構設定
建構並發布整合測試套件至 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