29 个版本

使用旧版 Rust 2015

0.2.22 2019年10月10日
0.2.18 2019年8月25日
0.2.17 2019年7月3日
0.2.13 2019年3月4日
0.1.1 2016年11月16日

#156构建工具

每月 36 次下载

MIT/Apache

91KB
2K SLoC

ctest

构建状态 文档

Rust 中 FFI 绑定的自动化测试。此仓库旨在验证可在 crates.io 上找到的 *-sys crate,以确保 Rust 中的 API 与 C 中定义的 API 匹配。

示例

遗憾的是,目前的用法有点混乱,但使用此库,首先,在你的仓库中创建一个新的 Cargo 项目

$ cargo new --bin systest

然后,编辑 systest/Cargo.toml 以添加这些依赖项

[package]
# ...
build = "build.rs"

[dependencies]
mylib-sys = { path = "../mylib-sys" }
libc = "0.2"

[build-dependencies]
ctest = "0.2"

接下来,向 systest/build.rs 添加一个构建脚本

extern crate ctest;

fn main() {
    let mut cfg = ctest::TestGenerator::new();

    // Include the header files where the C APIs are defined
    cfg.header("foo.h")
       .header("bar.h");

    // Include the directory where the header files are defined
    cfg.include("path/to/include");

    // Generate the tests, passing the path to the `*-sys` library as well as
    // the module to generate.
    cfg.generate("../mylib-sys/lib.rs", "all.rs");
}

然后,向 src/main.rs 添加以下内容

#![allow(bad_style)]

extern crate mylib_sys;
extern crate libc;

use libc::*;
use mylib_sys::*;

include!(concat!(env!("OUT_DIR"), "/all.rs"));

一切准备就绪!要在 systest 目录下运行测试,请执行 cargo run,然后一切都应该启动!

工作原理

此库将解析 *-sys crate 以了解所有外部的 fn 定义。然后,它将生成一个测试套件以确保所有函数签名、常量值、结构体布局/对齐、类型大小/对齐等都与它们的 C 等效项匹配。

生成的测试有两种形式。一种是包含main函数的Rust文件(因此上面有include!),另一种是作为构建脚本一部分编译的C文件。C文件包含所有头文件,并返回有关C端的信息(这些信息在Rust中进行验证)。

可以对C文件的生成方式应用大量配置,您可以浏览文档

使用 ctest 的项目

许可证

本项目许可协议为以下之一

由您选择。

贡献

除非您明确声明,否则您有意提交给 ctest 包含的任何贡献,根据 Apache-2.0 许可证定义,应作为上述双重许可,不附加任何额外条款或条件。

依赖项

~5MB
~97K SLoC