8 个版本
0.0.1 | 2024 年 5 月 22 日 |
---|---|
0.0.1-rc7 | 2024 年 5 月 6 日 |
0.0.1-rc6 | 2024 年 5 月 5 日 |
0.0.1-rc5 | 2024 年 5 月 3 日 |
0.0.1-rc4 | 2024 年 4 月 10 日 |
10 在 #extism 中
每月 22 次下载
13KB
200 行
xtp-test
适用于 xtp / Extism 插件的 Rust 测试框架。
示例
use extism_pdk::*;
use xtp_test;
// You _must_ export a single `test` function for the runner to execute.
use extism_pdk::*;
use xtp_test;
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Count {
count: usize,
total: usize,
vowels: String,
}
#[plugin_fn]
pub fn test() -> FnResult<()> {
// call a function from some Extism plugin (you'll link these up in the CLI command to run the test),
// passing in some data and getting back a string (`callString` is a helper for string output)
let Json(res): Json<Count> = xtp_test::call("count_vowels", "some input")?;
// assert the count of the vowels is correct, giving the test case a name (which will be shown in the CLI output)
// using the macro version here will also capture filename and line number
xtp_test::assert_eq!("count_vowels of 'some input'", res.count, 4);
// create a group of tests, which will be run together and reset after the group is complete
xtp_test::group("count_vowels maintains state", || {
let mut accum_total = 0;
let expected_final_total = 12;
for i in 0..3 {
let Json(res): Json<Count> = xtp_test::call("count_vowels", "this is a test")?;
accum_total += res.count;
xtp_test::assert_eq("total count increased", accum_total, 4 * (i + 1));
}
xtp_test::assert_eq(
"expected total at and of test",
accum_total,
expected_final_total,
);
Ok(())
})?;
Ok(())
}
API 文档
请参阅 docs.rs
文档页面以获取详细信息。
使用方法
1. 使用 XTP 测试 crate 创建 Rust 项目
cargo new --lib rust-xtp-test
cd rust-xtp-test
# ensure you have `crate-type = ["cdylib"]` in your `[lib]` section of Cargo.toml
cargo add xtp-test extism-pdk
2. 用 Rust 编写测试
use extism_pdk::*;
use xtp_test;
// You _must_ export a single `test` function for the runner to execute.
#[plugin_fn]
pub fn test() -> FnResult<()> {
// call a function from the Extism plugin being tested
let example = xtp_test::call("example", example_input)?;
// assert various things about the behavior and performance of the function call
xtp_test::assert_ne("example not null", &example, "");
// ...
Ok(())
}
3. 将测试编译为 .wasm
确保您已通过 rustup
安装了 wasm32-unknown-unknown
和/或 wasm32-wasi
目标,并运行
cargo build --target wasm32-unknown-unknown --release
4. 运行测试以针对插件:一旦您将测试代码作为一个 .wasm
模块,您可以使用 xtp
CLI 运行测试针对您的插件
安装 xtp
curl https://static.dylibso.com/cli/install.sh | sudo sh
运行测试套件
xtp plugin test ./plugin-*.wasm --with test.wasm --mock-host host.wasm
# ^^^^^^^^^^^^^^^ ^^^^^^^^^ ^^^^^^^^^
# your plugin(s) test to run optional mock host functions
注意:可选的模拟主机函数必须实现为与被测试插件导入的主机函数签名匹配的 Extism 插件。
需要帮助?
请通过 Discord 上的 #xtp
频道 联系。
依赖关系
~5MB
~73K SLoC