Add a simple blog post to talk about async-std testing
This commit is contained in:
parent
32a88ce92f
commit
f75bb2a54a
|
@ -0,0 +1,55 @@
|
|||
---
|
||||
layout: post
|
||||
title: "Writing Rust unit tests with async-std"
|
||||
tags:
|
||||
- async-std
|
||||
- async
|
||||
- rust
|
||||
---
|
||||
|
||||
I have been writing a [lot of](https://github.com/reiseburo/hotdog) [Rust lately](https://github.com/rtyler/otto)
|
||||
and as a consequence I have had to get a _lot_ better at writing unit tests. As
|
||||
if testing along weren't tricky enough, almost everything I am writing takes
|
||||
advantage of `async`/`await` and is running on top of the
|
||||
[async-std](https://async.rs/) runtime.
|
||||
|
||||
Testing `async` functions with `async-std` is actually so easy it's no wonder
|
||||
nothing showed up in my first search engine queries! Rather than using
|
||||
`#[test]` use the `#[async_std::test]` notation and convert the test function
|
||||
to an async function, for example:
|
||||
|
||||
```rust
|
||||
async fn simple_method() -> boolean {
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_simple_case() {
|
||||
let result = simple_method().await;
|
||||
assert!(result);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
That was easy™
|
||||
|
||||
|
||||
Another option which also worked out just fine was to utilize [smol](https://github.com/stjepang/smol) as in `dev-dependencies` to run something like:
|
||||
|
||||
```rust
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_simple_case() {
|
||||
let result = smol::run(simple_method());
|
||||
assert!(result);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
layout: tag_page
|
||||
title: "Tag: firefox"
|
||||
tag: firefox
|
||||
title: "Tag: async-std"
|
||||
tag: async-std
|
||||
robots: noindex
|
||||
---
|
Loading…
Reference in New Issue