本指南介绍了如何构建 Fuchsia 以添加
Fuchsia 的源代码 //examples
目录,并在以下位置运行测试:
Fuchsia 目标。
前提条件
在运行此测试组件之前,您必须:
探索示例
Hello, world!
示例包括使用
各种受支持的语言测试组件有两个关键元素:
可执行的测试套件
Fuchsia 测试套件作为组件构建,并在 test runner(用于受支持的测试运行程序) 测试框架,例如 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 配置后,请使用以下代码构建 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