整合測試 著重於驗證元件與其他元件互動時的行為 整個系統元件因此,我們通常會建構整合測試 以獨立於主要元件之外的方式宣告受測試的元件 和其他依附元件做為子項根據測試性質 依附元件元件可能會做為模擬或虛設常式提供,藉此宣傳 這些病例仍保持隱密
測試元件
以下是簡易整合測試元件的元件資訊清單範例:
荒漠油廠
meta/integration_tests.cml
:
{
include: [
"syslog/client.shard.cml",
"//src/sys/test_runners/rust/default.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" ],
},
],
}
C++
meta/integration_tests.cml
:
{
include: [
"syslog/client.shard.cml",
"//src/sys/test_runners/gtest/default.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 建構系統提供 fuchsia_test_package()
GN 目標給
而不同的測試元件,例如整合測試這項規則可讓您
請單獨宣告內含測試的元件,而非
,並描述測試的目標裝置環境
應該可以執行
以下範例說明如何將上述整合測試納入
BUILD.gn
檔案:
import("//build/components.gni")
...
// Component under test
fuchsia_component("foo_client") {
deps = [ ... ]
manifest = "meta/foo_client.cml"
}
// Test dependencies
fuchsia_component("mock_service") {
deps = [ ... ]
manifest = "meta/mock_service.cml"
testonly = true
}
// Component containing integration tests
fuchsia_component("integration_tests") {
deps = [ ":bin_test" ]
manifest = "meta/integration_tests.cml"
testonly = true
}
fuchsia_test_package("hello-world-tests") {
test_components = [ ":integration_tests" ]
deps = [
":foo_client",
":mock_service",
]
}
練習:Echo 伺服器整合測試
在本練習中,您將新增整合測試元件以練習 FIDL
echo_server
元件與測試執行器的通訊協定介面
在 FEMU 環境中建立架構並執行測試。
新增整合測試元件
首先,請替名為「Scaffold」的新整合測試元件建立專案 Scaffold
//vendor/fuchsia-codelab
目錄中的 echo-integration
:
mkdir -p vendor/fuchsia-codelab/echo-integration
在新的專案目錄中建立以下檔案和目錄結構:
荒漠油廠
echo-integration
|- BUILD.gn
|- meta
| |- echo_integration.cml
|
|- src
|- lib.rs
BUILD.gn
:測試二進位檔、元件和套件的 GN 建構目標。meta/echo_integration.cml
:宣告受測試元件的資訊清單 與其功能src/lib.rs
:Rust 整合測試的原始碼。
C++
echo-integration
|- BUILD.gn
|- meta
| |- echo_integration.cml
|
|- echo_integration_test.cc
BUILD.gn
:測試二進位檔、元件和套件的 GN 建構目標。meta/echo_integration.cml
:宣告受測試元件的資訊清單 與其功能echo_integration_test.cc
:C++ 整合測試的原始碼。
更新測試元件資訊清單
測試元件的資訊清單會套用基準
依附元件,例如 test_runners
更新 echo_integration.cml
檔案,將 echo-server
元件宣告為子項,並轉送 Echo
也為測試元件提供通訊協定能力
荒漠油廠
echo-integration/meta/echo_integration.cml
:
{
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",
},
// 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++
echo-integration/meta/echo_integration.cml
:
{
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",
},
// 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
例項來自與
整合測試。這個做法會宣傳名為「密封」的測試套件,
避免依賴其他套件的元件
實作整合測試
整合測試會連至 Echo
通訊協定
echo-server
與用戶端元件傳送字串要求的方式相同,
並驗證預期的回應
加入下列程式碼即可實作整合測試:
荒漠油廠
echo-integration/src/lib.rs
:
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++
echo-integration/echo_integration_test.cc
:
#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
檔案,以建構並套件整合測試
和 echo 伺服器依附元件:
荒漠油廠
echo-integration/BUILD.gn
:
import("//build/components.gni")
import("//build/rust/rustc_test.gni")
rustc_test("bin") {
name = "echo-integration-test"
edition = "2021"
deps = [
"//vendor/fuchsia-codelab/echo-fidl:echo_rust",
"//src/lib/fuchsia",
"//src/lib/fuchsia-component",
"//third_party/rust_crates:anyhow",
]
sources = [ "src/lib.rs" ]
}
fuchsia_component("component") {
testonly = true
component_name = "echo_integration"
manifest = "meta/echo_integration.cml"
deps = [ ":bin" ]
}
fuchsia_test_package("tests") {
package_name = "echo-integration-tests"
test_components = [ ":component" ]
deps =
[ "//vendor/fuchsia-codelab/echo-server:component" ]
}
C++
echo-integration/BUILD.gn
:
import("//build/components.gni")
executable("bin") {
output_name = "echo-integration-test"
sources = [ "echo_integration_test.cc" ]
deps = [
"//vendor/fuchsia-codelab/echo-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("component") {
testonly = true
component_name = "echo_integration"
manifest = "meta/echo_integration.cml"
deps = [ ":bin" ]
}
fuchsia_test_package("tests") {
package_name = "echo-integration-tests"
test_components = [ ":component" ]
deps =
[ "//vendor/fuchsia-codelab/echo-server:component" ]
}
更新建構設定
將整合測試套件新增至建構設定:
fx set workstation_eng.x64 \
--with //vendor/fuchsia-codelab/echo-server \
--with //vendor/fuchsia-codelab/echo-client \
--with //vendor/fuchsia-codelab/echo-realm \
--with //vendor/fuchsia-codelab/echo-integration:tests
執行 fx build
,並確認建構作業是否順利完成:
fx build
執行整合測試
fuchsia_test_package()
規則會產生含有測試元件的套件
與其依附元件整合測試元件的網址如下:
fuchsia-pkg://fuchsia.com/echo-integration-tests#meta/echo_integration.cm
使用 ffx test
指令執行整合測試。請確認
通過:
ffx test run \
fuchsia-pkg://fuchsia.com/echo-integration-tests#meta/echo_integration.cm