執行測試元件

本指南說明如何建構 Fuchsia,以納入 Fuchsia 來源 //examples 目錄的測試套件,並針對 Fuchsia 目標執行測試。

必要條件

執行這個測試元件前,您必須:

探索範例

Hello, world! 範例包含以各種支援語言編寫的單元測試元件。測試元件有兩個主要元素:

  • 以支援的語言編寫的執行測試套件
  • 用於定義測試元件建構目標的 BUILD.gn 檔案,並加入 Fuchsia 測試套件中。

可執行測試套件

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 檔案會宣告 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 映像檔中加入範例測試

如要在建構設定中加入範例套件,請在設定產品和主機板環境時使用 --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

您現在有一個建構內含可隨選擷取及啟動的範例測試。

執行測試套件

  1. 開啟終端機並執行 fx serve

    fx serve
    
  2. 開啟另一個終端機並執行 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