本指南介绍了如何构建 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 = "2021"
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
对于具有最低 build 配置的 Fuchsia 模拟器,命令为:
fx set core.x64 --with //examples/hello_world:tests
在此示例中,core
是具有最低功能集的产品(包括常见的网络功能),而 x64
是指 x64 架构。
对于具有最低 build 配置的 Fuchsia 设备,命令为:
fx set core.x64 --with //examples/hello_world:tests
如需了解更多选项,请参阅配置 build。
设置 build 配置后,请使用以下命令构建 Fuchsia:
fx build
您现在已经有了一个 build,其中包含可按需提取和启动的示例测试。
运行测试套件
打开终端并运行
fx serve
:fx serve
打开另一个终端并运行
hello-world
单元测试:C++
fx test hello-world-cpp-unittests
Rust
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