Integration Testing in Rust

栏目: IT技术 · 发布时间: 5年前

内容简介:PublishedWe all know testing is important. And, I hope, it might even be familiar. You’re coding away and catch yourself thinking “I should probably test this”. And before you know it you’ve mashed on your keyboard and there appearsThese are unit tests whi

Published onJoshleeb's blog

We all know testing is important. And, I hope, it might even be familiar. You’re coding away and catch yourself thinking “I should probably test this”. And before you know it you’ve mashed on your keyboard and there appears

#[cfg(test)]
mod tests {
    ...
}

These are unit tests which are super easy to setup. No added dependencies, no added tooling

But what about integration tests?

Rust supports those too, which you probably already know. They’re only slightly more involved to setup so they’re still fairly straightforward.

But there are some caveats that I personally ran into.

For one, The Rust Book explains that

each file in the tests directory is a separate crate.

Which on its own doesn’t sound too bad. But it has the implication that having code shared between your integration tests isn’t intuitive. I think it’s because the code structure goes back to the Rust 2015 edition of defining modules, using mod.rs .

And two, which is arguably the bigger issue, is that it’s not obvious how to run only all integration tests. Or only all unit tests for that matter, but we’ll get to that too.

A Better Setup

I went looking through docs and crates and eventually opened up the source for diesel-rs/diesel , which isn’t the first time it’s been a great reference.

Now it looks like diesel has a separate crate, internal to the workspace, called diesel_tests which appears to contain the integration tests. But I don’t want to setup a new crate and workspace just to extend my test suite. Instead let’s take a look at the Cargo.toml for that crate and borrow what we can borrow.

For my new crate, mycrate , I’ve setup the Cargo.toml as

[package]
name = "mycrate"
...
autotests = false

[[test]]
name = "integration"
path = "tests/lib.rs"

Specifically, we’ve added two sections .

Autotests

The autotests = false disables automatic test discovery. This sounds bad but isn’t needed because we’re manually telling Cargo where to find our tests with…

Test Target

This may look familiar if you’ve seen [[bin]] or [[example]] where we explicitly specify an additional target for Cargo to work with. This is no different. We are simply telling Cargo there’s a test target called “integration” which has an entry point at tests/lib.rs .

Single Integration Test Crate

This config solves the first caveat. We no longer have to deal with a crate per integration test file, and so the way we handle code shared between the tests is the same way we would handle sharing code in the implementation.

And everything is still in the tests directory, so no changes there either.

Running Tests

We’ve also made segmenting our tests a bit clearer.

# Run all tests
cargo test

# Run only unit tests
cargo test --lib

# Run only integration tests
cargo test --test integration

# Run only integration tests, single threaded
# (you’ll probably want this one)
cargo test --test integration -- --test-threads=1

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Ajax模式与最佳实践

Ajax模式与最佳实践

Christian Gross / 李锟、张祖良、蔡毅、赵泽欣 / 电子工业出版社 / 2007-3 / 49.80元

Ajax 正在将我们带入到下一代的网络应用中。 本书深入探讨了动态的网络应用,将Ajax和REST集成在一起作为单独的解决方案。一个很大的优势是,与Ajax相似,REST可以和现今存在的技术一起使用。现在上百万的客户端计算机都是基于Ajax的,上百万的服务器是基于REST的。   无论你是否已经开发过Ajax应用程序,这都是一本理想的书。因为这本书描述了各种各样的模式和最好的实践经验。通过此......一起来看看 《Ajax模式与最佳实践》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具