Common Testing
Common testing shortcuts and utilities reused across projects. Freely share this code or copy into your own projects.
If there is a function that you've found useful in more than one project, consider adding it to this repo.
Feature Highlights
Result and Option Assertions
Asserts that a Result or Option is in a particular state. There are also into
functions for converting the Result or Option into the inner value after asserting it is in the expected state.
- assert::ok
- assert::ok_into
- assert::err
- assert::err_into
- assert::some
- assert::some_into
- assert::none
use assert;
COW Assertions
Asserts that a COW is in a particular state.
- assert::borrowed
- assert::owned
use assert;
AsRef Assertions
Assert that there is a common AsRef implementation between two types, and that the AsRef values are equal. If there is more than one possible AsRef implementation, specify the type. If there is only one possible AsRef implementation, the type is inferred.
use assert;
Testing Globals, OS calls, or File Systems
Rust will normally run tests in parallel, which can cause issues when tests are not isolated. This library provides a way to run tests sequentially, which is useful for testing code that uses global variables, OS calls, or file systems.
If you want to have tests run sequentially, use the setup::sequential
function. This will return a lock that will prevent other tests that also use setup::sequential
from running at the same time.
- setup::sequential
use setup;
Strict Equality Assertions
This library provides strict equality assertions that will fail if the types are not the same.
The marcos for testing equality (assert!, assert_eq!, assert_ne!) are not strict enough regarding types. For example, assert_eq!(1, 1.0)
will pass. Also, the marcos will sometimes not detect issues until runtime. By providing non-macro functions that require PartialEq, the compiler catches more issues at compile time.
We also standardize the equality assertions to always print comparisons of the values in the error message, and require the values to be references to prevent assertions from taking accidental ownership.
- assert::equal
- assert::not_equal
- assert::default
use assert;
File Setup and Assertions
Too many tests are written with unique boilerplate for working with fixtures, files or large data, leading to erratic expectations or side-effects that becomes difficult to maintain or to reason about when debugging asynchonous code. This library provides reusable file handling that encourage best practices that reduce side-effects or variability between tests.
-
assert::equal_file_contents - Compare against fixtures or large data.
-
assert::cursor_completely_consumed - Catch certain really common errors.
-
setup::get_file_contents - Read fixtures or large data into the test.
-
setup::create_dir_all - Guarantee a directory path exists for the test.
-
setup::remove_file - Remove file if exists, useful to reset a test.
-
setup::write_file_contents - Create temporary files for the test.
use assert;
Binary Data Assertions
Assertions for working with binary data, making assumptions about the data types for more useful failure messages.
- assert::equal_bytes
- assert::equal_hex_bytes
use assert;