diff --git a/_posts/2020-06-21-async-std-tests.md b/_posts/2020-06-21-async-std-tests.md new file mode 100644 index 0000000..98ae94d --- /dev/null +++ b/_posts/2020-06-21-async-std-tests.md @@ -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); + } +} +``` + diff --git a/tag/firefox.md b/tag/async-std.md similarity index 51% rename from tag/firefox.md rename to tag/async-std.md index 25b6c11..1e0de76 100644 --- a/tag/firefox.md +++ b/tag/async-std.md @@ -1,6 +1,6 @@ --- layout: tag_page -title: "Tag: firefox" -tag: firefox +title: "Tag: async-std" +tag: async-std robots: noindex ---