本指南将介绍如何构建 Fuchsia,以包含来自
Fuchsia 源代码 //examples 目录的测试软件包,并在
您的 Fuchsia 目标上运行测试。
前提条件
如需运行此测试组件,您必须:
探索示例
Hello, world! 示例包含使用各种受支持的语言编写的单元测试组件。测试组件有两个关键元素:
可执行测试套件
Fuchsia 测试套件以组件的形式构建,并在受支持的 测试框架(例如 GoogleTest)的 测试运行器中执行。 测试套件二进制文件包含针对这些特定于语言的框架编写的测试用例。
C++
#include <gtest/gtest.h>
TEST(HelloWorldTest, True) { EXPECT_TRUE(true); }
Rust
#[cfg(test)]
mod tests {
#[fuchsia::test]
fn it_works() {
assert_eq!(true, true);
}
}
如需详细了解如何测试 Fuchsia 组件,请参阅 使用组件进行测试
BUILD.gn
BUILD.gn 文件为 fuchsia_unittest_package() 声明构建目标。此模板专门用于打包包含测试的组件。
C++
executable("unittests") {
testonly = true
output_name = "hello_world_cpp_test_bin"
sources = [ "hello_world_unittest.cc" ]
deps = [ "//src/lib/fxl/test:gtest_main" ]
}
fuchsia_unittest_package("hello-world-cpp-unittests") {
deps = [ ":unittests" ]
}
Rust
rustc_binary("bin") {
name = "hello_world_rust"
# Generates the "bin_test" build target
with_unit_tests = true
edition = "2024"
deps = []
test_deps = [ "//src/lib/fuchsia" ]
sources = [ "src/main.rs" ]
}
fuchsia_unittest_package("hello-world-rust-tests") {
deps = [ ":bin_test" ]
}
如需详细了解 Fuchsia 如何使用 GN 定义测试软件包, 请参阅:构建组件。
在 Fuchsia 映像中包含示例测试
如需在 build 配置中包含示例软件包,请在设置产品和板级环境时使用 --with 标志:
fx set product.board --with //examples/hello_world:tests对于具有最低构建配置的 Fuchsia 模拟器,该命令为:
fx set core.x64 --with //examples/hello_world在此示例中,core 是具有最少功能集(包括常见网络功能)的产品,而 x64 是指 x64 架构。
对于具有最低构建配置的 Fuchsia 设备,该命令为:
fx set core.x64 --with //examples/hello_world如需了解更多选项,请参阅配置构建,了解 更多选项。
设置 build 配置后,请使用以下命令构建 Fuchsia:
fx build您现在拥有一个构建,其中包含可以 按需提取和启动的示例测试。
运行测试套件
打开终端并运行
fx serve:fx serve打开另一个终端并运行
hello-world单元测试:C++
fx test hello-world-cpp-unittestsRust
fx test hello-world-rust-tests
此命令会输出以下内容:
C++
$ fx test hello-world-cpp-unittests
...
PASS: 0 FAIL: 0 00:00 🤔 fx shell run-test-suite '--max-severity-logs WARN' fuchsia-pkg://fuchsia.com/hello-world-cpp-unittests?hash=c34ed8b2ea21fd5158f1de77a5581a4b5123161ef851eea430768a00efc1cbbf#meta/hello-world-cpp-unittests.cm
>> Runtime has exceeded 2 seconds (adjust this value with the -s|--slow flag)
Running test 'fuchsia-pkg://fuchsia.com/hello-world-cpp-unittests?hash=c34ed8b2ea21fd5158f1de77a5581a4b5123161ef851eea430768a00efc1cbbf#meta/hello-world-cpp-unittests.cm'
[RUNNING] HelloWorldTest.True
[PASSED] HelloWorldTest.True
1 out of 1 tests passed...
fuchsia-pkg://fuchsia.com/hello-world-cpp-unittests?hash=c34ed8b2ea21fd5158f1de77a5581a4b5123161ef851eea430768a00efc1cbbf#meta/hello-world-cpp-unittests.cm completed with result: PASSED
PASS: 1 FAIL: 0 00:15 ✅ fx shell run-test-suite '--max-severity-logs WARN' fuchsia-pkg://fuchsia.com/hello-world-cpp-unittests?hash=c34ed8b2ea21fd5158f1de77a5581a4b5123161ef851eea430768a00efc1cbbf#meta/hello-world-cpp-unittests.cm
🎉 Ran 1 tests with 0 failures (use the -v flag to see each test) 🎉
Rust
$ fx test hello-world-rust-tests
...
PASS: 0 FAIL: 0 00:00 🤔 fx shell run-test-suite '--max-severity-logs WARN' fuchsia-pkg://fuchsia.com/hello-world-rust-tests?hash=45cc85adaa09af18c575c45be942d72e173719c53e69d879eeb9602fa38e4884#meta/hello-world-rust-tests.cm
>> Runtime has exceeded 2 seconds (adjust this value with the -s|--slow flag)
Running test 'fuchsia-pkg://fuchsia.com/hello-world-rust-tests?hash=45cc85adaa09af18c575c45be942d72e173719c53e69d879eeb9602fa38e4884#meta/hello-world-rust-tests.cm'
[RUNNING] hello_tests::greeting_test
[RUNNING] hello_tests::my_test
[RUNNING] tests::it_works
[PASSED] tests::it_works
[PASSED] hello_tests::my_test
[PASSED] hello_tests::greeting_test
3 out of 3 tests passed...
fuchsia-pkg://fuchsia.com/hello-world-rust-tests?hash=45cc85adaa09af18c575c45be942d72e173719c53e69d879eeb9602fa38e4884#meta/hello-world-rust-tests.cm completed with result: PASSED
PASS: 1 FAIL: 0 00:15 ✅ fx shell run-test-suite '--max-severity-logs WARN' fuchsia-pkg://fuchsia.com/hello-world-rust-tests?hash=45cc85adaa09af18c575c45be942d72e173719c53e69d879eeb9602fa38e4884#meta/hello-world-rust-tests.cm