本指南說明如何建構 Fuchsia,以便納入 Fuchsia 來源 //examples 目錄中的測試套件,並在 Fuchsia 目標上執行測試。
必要條件
執行這項測試元件前,請務必完成下列事項:
探索範例
Hello, world! 範例包含以各種支援語言編寫的單元測試元件。測試元件有兩項主要元素:
可執行的測試套件
Fuchsia 測試套件會建構為元件,並在支援的測試架構 (例如 GoogleTest) 的測試執行工具中執行。測試套件二進位檔包含針對這些語言專屬架構編寫的測試案例。
C++
#include <gtest/gtest.h>
TEST(HelloWorldTest, True) { EXPECT_TRUE(true); }
荒漠油廠
#[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" ]
}
荒漠油廠
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 映像檔中加入範例測試
如要在建構設定中加入範例套件,請在設定產品和主機板環境時使用 --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如需更多選項,請參閱「設定建構作業」。
設定建構設定後,請使用下列指令建構 Fuchsia:
fx build現在您已擁有包含範例測試的建構版本,可視需要擷取及啟動。
執行測試套件
開啟終端機並執行
fx serve:fx serve開啟另一個終端機,然後執行
hello-world單元測試:C++
fx test hello-world-cpp-unittests荒漠油廠
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) 🎉
荒漠油廠
$ 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