Rust测试框架dinghy-test的使用:高效跨平台测试工具与自动化测试解决方案

Rust测试框架dinghy-test的使用:高效跨平台测试工具与自动化测试解决方案

什么是Dinghy?

Dinghy是一个cargo扩展,用于在交叉编译场景中实现cargo工作流程。它特别适用于基于"小型"处理器的设备,如Android和iOS手机,或小型单板计算机如Raspberry Pi等无法或不便进行本地编译的情况。

示例演示

Demo

如何使用?

一旦dinghy配置了工具链和设备,你就可以在任何cargo项目中运行测试和基准测试,大多数情况下无需修改项目。

只需在cargo和其子命令之间添加dinghy -d some_device

cargo dinghy -d my_android test
cargo dinghy -d my_raspberry bench

默认情况下,不使用-d参数时,Dinghy会像普通cargo一样进行本地构建。

完整示例代码

以下是一个使用dinghy-test的完整示例:

// 首先确保已安装dinghy-test
// cargo install dinghy-test

// 示例测试代码
#[cfg(test)]
mod tests {
    #[test]
    fn test_addition() {
        assert_eq!(2 + 2, 4);
    }

    #[test]
    fn test_subtraction() {
        assert_eq!(5 - 3, 2);
    }
}

// 使用dinghy运行测试的配置示例
// 在.cargo/config.toml中添加:
/*
[target.'cfg(all(any(target_arch="aarch64",target_arch="x86_64"),target_vendor="apple",any(target_os="ios",target_os="tvos",target_os="apple-watchos")))']
runner = "cargo dinghy runner --"
*/

// 然后可以运行:
// cargo dinghy -d my_ios_device test

完整示例demo

以下是一个更完整的dinghy-test使用示例,包含项目配置和测试代码:

// Cargo.toml 配置
/*
[package]
name = "dinghy-demo"
version = "0.1.0"
edition = "2021"

[dependencies]

[dev-dependencies]
dinghy-test = "0.4"
*/

// src/lib.rs
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

pub fn subtract(a: i32, b: i32) -> i32 {
    a - b
}

// tests/integration_test.rs
use dinghy_demo::{add, subtract};

#[test]
fn test_add() {
    assert_eq!(add(2, 3), 5);
}

#[test]
fn test_subtract() {
    assert_eq!(subtract(5, 2), 3);
}

// .cargo/config.toml 配置
/*
[target.aarch64-linux-android]
runner = "cargo dinghy runner --"

[target.armv7-linux-androideabi]
runner = "cargo dinghy runner --"

[target.'cfg(all(any(target_arch="aarch64",target_arch="x86_64"),target_vendor="apple",any(target_os="ios",target_os="tvos",target_os="apple-watchos")))']
runner = "cargo dinghy runner --"
*/

// 运行测试的命令
// cargo dinghy -d my_android_device test
// cargo dinghy -d my_ios_simulator test

入门指南

根据目标和开发环境的不同,设置Dinghy的难易程度会有所变化:

  • Android相对容易,特别是如果你已经是移动开发者
  • iOS设置步骤较多,但Apple提供了所需的一切
  • 其他远程SSH可访问的设备从dinghy的角度来看是最简单的

高级功能

  • 一些项目需要资源文件来运行测试或基准测试
  • 在大型项目中,可能需要根据测试平台过滤项目成员
  • 传递环境变量到可执行文件有时很有用
  • Dinghy提供了覆盖系统来"添加"内容到工具链的sysroot
  • dinghy-build crate提供了一些在交叉编译上下文中有用的build.rs功能

仅使用Dinghy作为运行器

如果你的项目已经能够为目标平台构建而无需dinghy,只想使用dinghy在设备上运行代码,可以直接使用dinghy的捆绑运行器。只需在.cargo/config中注册dinghy作为运行器:

[target.'cfg(all(any(target_arch="aarch64",target_arch="x86_64"),target_vendor="apple",any(target_os="ios",target_os="tvos",target_os="apple-watchos")))']
runner = "cargo dinghy runner --"

然后可以直接运行测试,例如:cargo test --target aarch64-apple-ios-sim

许可证

Dinghy采用以下许可证之一:

  • Apache License, Version 2.0
  • MIT license

贡献

除非你明确声明,否则任何有意提交的贡献都将按上述双重许可,不附加任何额外条款或条件。


1 回复

Rust测试框架dinghy-test的使用:高效跨平台测试工具与自动化测试解决方案

简介

dinghy-test是一个专为Rust设计的跨平台测试框架,特别适合需要在多种平台(包括移动设备和嵌入式系统)上运行测试的场景。它扩展了Rust的标准测试功能,提供了更强大的跨平台测试能力。

主要特性

  • 支持在iOS、Android、Linux、macOS和Windows等平台运行测试
  • 自动化设备部署和测试执行
  • 与Cargo集成,保持熟悉的测试工作流程
  • 支持复杂的测试环境设置

安装方法

在Cargo.toml中添加dinghy作为开发依赖:

[dev-dependencies]
dinghy = "0.4"

基本使用方法

1. 编写测试

dinghy-test兼容标准的Rust测试写法,可以直接使用#[test]属性:

#[test]
fn basic_test() {
    assert_eq!(2 + 2, 4);
}

2. 平台特定测试

可以使用#[cfg]属性来指定特定平台的测试:

#[test]
#[cfg(target_os = "android")]
fn android_specific_test() {
    // Android平台特有的测试逻辑
}

3. 运行测试

使用dinghy命令行工具运行测试:

cargo dinghy test

指定平台运行:

cargo dinghy -d android test

高级功能示例

设备管理

列出可用设备:

cargo dinghy devices

复杂测试环境

#[test]
#[dinghy::test]
fn complex_test() {
    // 这里可以使用dinghy提供的额外功能
    let platform = std::env::var("DINGHY_PLATFORM").unwrap();
    println!("Running on platform: {}", platform);
    
    // 测试逻辑...
}

测试前后钩子

#[dinghy::test_setup]
fn setup() {
    println!("Setting up test environment");
}

#[dinghy::test_teardown]
fn teardown() {
    println!("Cleaning up test environment");
}

实际应用示例

以下是一个跨平台文件系统操作的测试示例:

#[test]
#[dinghy::test]
fn test_file_operations() {
    use std::fs::File;
    use std::io::Write;
    use std::path::Path;
    
    let test_file = Path::new("dinghy_test_file.txt");
    
    // 写入测试
    let mut file = File::create(&test_file).unwrap();
    file.write_all(b"dinghy test content").unwrap();
    
    // 读取验证
    let content = std::fs::read_to_string(&test_file).unwrap();
    assert_eq!(content, "dinghy test content");
    
    // 清理
    std::fs::remove_file(&test_file).unwrap();
}

配置dinghy

可以在项目根目录下创建dinghy.toml文件进行配置:

[android]
env = { ANDROID_NDK_HOME = "/path/to/ndk" }

[ios]
simulator = "iPhone 11"

完整示例demo

下面是一个完整的dinghy-test测试项目示例,展示了跨平台测试的完整流程:

  1. 首先创建项目并添加依赖:
cargo new dinghy_demo --lib
cd dinghy_demo
  1. 编辑Cargo.toml文件:
[package]
name = "dinghy_demo"
version = "0.1.0"
edition = "2021"

[dev-dependencies]
dinghy = "0.4"
  1. 创建测试文件src/lib.rs:
// 标准测试
#[test]
fn basic_addition() {
    assert_eq!(1 + 1, 2);
}

// Android平台特定测试
#[test]
#[cfg(target_os = "android")]
fn android_test() {
    println!("This test only runs on Android");
    assert!(true);
}

// iOS平台特定测试
#[test]
#[cfg(target_os = "ios")]
fn ios_test() {
    println!("This test only runs on iOS");
    assert!(true);
}

// 使用dinghy特性的复杂测试
#[test]
#[dinghy::test]
fn cross_platform_test() {
    let platform = std::env::var("DINGHY_PLATFORM").unwrap_or("unknown".to_string());
    println!("Running test on platform: {}", platform);
    
    // 平台特定的断言
    match platform.as_str() {
        "android" => assert_eq!(platform, "android"),
        "ios" => assert_eq!(platform, "ios"),
        _ => assert!(true), // 其他平台
    }
}

// 测试环境设置
#[dinghy::test_setup]
fn test_setup() {
    println!("Setting up test environment for dinghy");
}

// 测试环境清理
#[dinghy::test_teardown]
fn test_teardown() {
    println!("Tearing down test environment");
}
  1. 创建dinghy配置文件dinghy.toml:
[android]
env = { ANDROID_HOME = "/path/to/android/sdk" }

[ios]
simulator = "iPhone 13"
  1. 运行测试:
# 运行所有测试
cargo dinghy test

# 在Android设备上运行测试
cargo dinghy -d android test

# 在iOS模拟器上运行测试
cargo dinghy -d ios test

注意事项

  1. 对于移动设备测试,需要提前设置好开发环境(如Android SDK或Xcode)
  2. 某些平台可能需要额外的权限配置
  3. 复杂的测试可能需要更长的执行时间

dinghy-test为Rust开发者提供了强大的跨平台测试能力,特别适合需要验证代码在多种环境下行为的项目。

回到顶部