測試公用程式庫

這個程式庫可實作輔助程式,以便為 ffx 子工具編寫 e2e 測試。

本指南涵蓋的內容:

  • 可減少設定樣板的測試韌體
  • ffx 的密封執行環境 (透過 ffx_isolate)
  • 低階模擬器產生的公用程式 (適用於 Flash 和 OTA 測試)

預計涵蓋以下項目:

  • ffx 指令輸出的比對器 (b/293217132)
  • ffx_e2e_emu 合併使用 ffx emu 整合 (b/295230484)

使用方法

子工具 e2e 測試通常位於 src/developer/ffx/tests。這與 ffx self-test 類似,但使用 rustc_test 執行測試,而非子工具。

BUILD.gn 中:

import("//build/host.gni")
import("//build/rust/rustc_binary.gni")

if (is_host) {
  rustc_test("ffx_<something>_test") {
    testonly = true
    edition = "2021"
    source_root = "src/tests.rs"

    sources = [ "src/tests.rs" ]

    deps = [
      "//src/developer/ffx/testing:ffx_testing",
      "//src/lib/fuchsia",
      "//src/lib/testing/fixture",
    ]

    # If using ffx_testing::Emu: Only one emulator can be spawned at a time.
    # These lower level emulators use TAP networking, which is required for IPv6
    # support.
    # args = [ "--test-threads=1" ]
  }
}

src/tests.rs 中:

use ffx_testing::{base_fixture, TestContext};
use fixture::fixture;

// base_fixture sets up the test environment this test
#[fixture(base_fixture)]
#[fuchsia::test]
async fn my_test(ctx: TestContext) {
  let _daemon = ctx.isolate().start_daemon().await.unwrap();

  let output = ctx.isolate().ffx(&["daemon", "echo"]).await.expect("daemon echo");
  assert!(output.status.success());
}

低階模擬器

低階模擬器可以使用 Emu::start 啟動。可由序列控制,並由 ffx 調查如需使用模擬器的測試,請參閱 ffx_target_test

/!\ 這個模擬器會顯示空白磁碟 /!\

這是設計所致。如要使用 fuchsia 映像檔進行 e2e 測試,請使用 //src/developer/ffx/lib/e2e_emu 下的 ffx_e2e_emu 程式庫。

use ffx_testing::Emu;

let emu = Emu::start(&ctx);

// NOTE: If using serial, make sure to drain or close the serial stream when no longer in use.
// Failure to do so will result in QEMU halting.
{
  let serial = emu.serial().await;

  // ...
}

// Dropping or letting the emu go out of scope cleans up the emulator.