【问题标题】:cargo build failed with #[rstest] inside货物构建失败,里面有#[rstest]
【发布时间】:2023-01-21 01:23:31
【问题描述】:

我想使用参数化测试并发现测试可以做到这一点。

添加:use rstest::rstest;main.rs 文件中并将#[rstest] 也放在main.rs 中在cargo test 上运行良好

但是如果我想用cargo build构建程序,我会收到这个错误

| use rstest::rstest;
 |     ^^^^^^ use of undeclared crate or module `rstest`

所以问题是:我如何组织我的代码以使用#[rstest]并且还能够构建/运行程序?

【问题讨论】:

    标签: unit-testing testing rust rust-cargo


    【解决方案1】:

    取决于你是否想使用 rstest 与非测试版本一起运行代码,你要么必须在你的 Cargo.toml 中添加 rstest

    [dependencies]
    rstest = "*"
    

    或者您必须使用 rstest 从非测试版本中删除代码:

    #[cfg(test)]
    use rstest::rstest;
    

    【讨论】:

      猜你喜欢
      • 2021-11-16
      • 2023-04-08
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 2015-03-12
      • 2018-11-17
      • 2014-12-14
      • 2015-08-23
      相关资源
      最近更新 更多