#集成测试 #进程 #tokio #应用程序 #跟踪 #二进制 #事件

tokio-bin-process

在集成测试时,使用 tokio 跟踪断言在独立进程下运行您的应用程序

7 个版本 (4 个破坏性更新)

0.5.1 2024年7月23日
0.5.0 2024年2月27日
0.4.1 2023年9月25日
0.3.0 2023年8月20日
0.1.0 2023年4月12日

#85值格式化

Download history 201/week @ 2024-05-01 155/week @ 2024-05-08 169/week @ 2024-05-15 51/week @ 2024-05-22 101/week @ 2024-05-29 76/week @ 2024-06-05 121/week @ 2024-06-12 33/week @ 2024-06-19 26/week @ 2024-06-26 62/week @ 2024-07-03 78/week @ 2024-07-10 198/week @ 2024-07-17 289/week @ 2024-07-24 152/week @ 2024-07-31 11/week @ 2024-08-07 52/week @ 2024-08-14

650 每月下载

Apache-2.0

39KB
586

允许您的集成测试在单独的进程下运行您的应用程序,并对 tracing 事件进行断言。

为了实现这一点,它定位或构建应用程序的可执行文件,使用 tracing 在 JSON 模式下运行它,然后处理 JSON 日志,以进行断言并以人类可读的形式显示。

它有一些意见,默认情况下,当出现 tracing 警告或错误时,会失败测试。但是,可以按测试的特定方式允许特定的警告或错误。

假设数据库项目 cooldb 的示例使用

use tokio_bin_process::event::Level;
use tokio_bin_process::{BinProcess, bin_path};
use tokio_bin_process::event_matcher::EventMatcher;
use std::time::Duration;
use std::path::PathBuf;

/// you'll want a helper like this as you'll be creating this in every integration test.
async fn cooldb_process() -> BinProcess {
    // start the process
    let mut process = BinProcess::start_binary(
        // Locate the path to the cooldb binary from an integration test or benchmark
        bin_path!("cooldb"),
        "cooldb", // The name that BinProcess should prepend its forwarded logs with
        &[
            // provide any custom CLI args required
            "--foo", "bar",
            // tokio-bin-process relies on reading tracing json's output,
            // so configure the application to produce that
            "--log-format", "json"
        ],
    )
    .await;

    // block asynchrounously until the application gives an event indicating that its ready
    tokio::time::timeout(
        Duration::from_secs(30),
        process.wait_for(
            &EventMatcher::new()
                .with_level(Level::Info)
                .with_target("cooldb")
                .with_message("accepting inbound connections"),
            &[]
        ),
    )
    .await
    .unwrap();
    process
}

#[tokio::test]
async fn test_some_functionality() {
    // start the db
    let cooldb = cooldb_process().await;

    // connect to the db, do something and assert we get the expected result
    perform_test();

    // Shutdown the DB, asserting that no warnings or errors occured,
    // but allow and expect a certain warning.
    // A drop bomb ensures that the test will fail if we forget to call this method.
    cooldb
        .shutdown_and_then_consume_events(&[
            EventMatcher::new()
                .with_level(Level::Warn)
                .with_target("cooldb::internal")
                .with_message("The user did something silly that we want to warn about but is actually expected in this test case")
        ])
        .await;
}

当 Cargo 构建集成测试或基准测试时,它提供了一个测试二进制的路径。我们可以利用这一点,通过使用 BinProcess::start_binary 来提高速度和鲁棒性。

但这并不总是足够灵活,所以作为后备,BinProcess 可以内部调用 Cargo 再次,以确保所需的二进制文件通过 BinProcess::start_binary_name 编译。

依赖项

~7–17MB
~205K SLoC