测试实用程序库

此库实现了辅助程序,用于为 ffx 子工具编写 e2e 测试。

其中包括:

  • 可减少初始设置样板的测试夹具
  • ffx 的封闭执行环境(通过 ffx_isolate
  • 低级别模拟器生成实用程序(用于刷写和 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

/!\ 此模拟器启动了一个空磁盘 /!\

这是设计所致。如需使用紫红映像进行 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.