Add a simple blog post to talk about async-std testing

This commit is contained in:
R Tyler Croy 2020-06-21 14:33:25 -07:00
parent 32a88ce92f
commit f75bb2a54a
No known key found for this signature in database
GPG Key ID: E5C92681BEF6CEA2
2 changed files with 57 additions and 2 deletions

View File

@ -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);
}
}
```

View File

@ -1,6 +1,6 @@
---
layout: tag_page
title: "Tag: firefox"
tag: firefox
title: "Tag: async-std"
tag: async-std
robots: noindex
---