本指南說明如何建構 Fuchsia,並納入
Fuchsia 的來源 //examples
目錄,在
您的 Fuchsia 目標。
必要條件
如要執行這個測試元件,您必須:
查看範例
Hello, world!
範例包含以
支援多種語言。測試元件有兩個主要元素:
可執行的測試套件
Fuchsia 測試套件以元件形式建構而成,並在 test Runner (支援時) 測試架構,例如 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 = "2021"
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:tests
在此範例中,core
是基本功能集的產品,其中包含
常見的網路功能,x64
則是指 x64 架構。
如果是搭載最低建構設定的 Fuchsia 裝置,指令如下:
fx set core.x64 --with //examples/hello_world:tests
請參閱設定建構作業,瞭解相關資訊
完成建構設定後,請使用下列指令建構 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