Expand description
Testing support.
Under encore test, every test receives isolated infrastructure—its own
databases, buckets, and topics—with no manual setup required:
#[tokio::test]
async fn creates_a_user() -> Result<(), encore::Error> {
create_user("ada").await?; // uses the `static DB`, isolated to this test
let row = DB.query_one("SELECT count(*) FROM users", &[]).await?;
assert_eq!(row.get::<_, i64>(0), 1);
Ok(())
}Encore associates each call with the right test automatically, whether it
runs on the test’s own thread (named after the test by the test harness) or
on a Tokio runtime thread created for the test (including spawned tasks and
spawn_blocking). A test’s infrastructure is released when the test ends.
§Running without encore test
cargo test, cargo-nextest, and IDE test runners also work. Encore
locates the enclosing app and starts the local daemon via encore on PATH
if it is not already running. Secrets and configs use their test values just
as under encore test. The app’s .env files are not loaded automatically;
set any needed environment variables in your test. Set ENCORE_TEST=0 to
disable this behavior, or ENCORE_TEST=1 to enable it for an unrecognized
test runner.
§#[encore::test]
Standard #[test] and #[tokio::test] attributes work out of the box.
#[encore::test] behaves like #[tokio::test] (with
the same flavor and worker_threads options) while registering the test
with Encore before it starts rather than on first resource use. Its runtime
threads belong to the test from the start, and its infrastructure is
released immediately when the test returns.
#[encore::test(flavor = "multi_thread")]
async fn handles_concurrent_requests() -> Result<(), encore::Error> {
// …
}§Fixtures
By default, each test starts with freshly provisioned resources (empty,
migrated databases and empty buckets). A Fixture saves a populated
starting state built once by your setup function and copies it for each test
that loads it. See Fixture and fixture!.
§Other threads
Threads spawned directly with std::thread::spawn are not automatically
associated with a test, so using an Encore resource on them returns an
error. (The exception is a multi-threaded Tokio runtime created on such a
thread, which Encore treats as its own test with its own infrastructure.)
Propagate the test context to manually spawned threads with TestScope:
let scope = encore::testing::current()?;
std::thread::spawn(move || {
let _guard = scope.enter();
// … part of the test from here on
});Modules§
- system
- End-to-end system tests that run against the full application.
Structs§
- Fixture
- A reusable starting state for tests, replacing freshly provisioned empty resources (for example, pre-seeded database rows).
- Scope
Guard - Keeps the thread part of a test. Returned by
TestScope::enter. - Test
Scope - A handle to a running test, returned by
current.
Functions§
- current
- Returns a
TestScopehandle for the test running on the calling thread. - deliver
- Delivers all unhandled messages published to
subscription’s topic during the current test, invoking its handler one message at a time in publish order. Returns the number of messages handled. - published
- Returns all messages published to
topicduring the current test, in publish order (both directly by the test and by code it called). - set_
config - Overrides
configwithvaluefor the current test only. - set_
secret - Overrides
secretwithvaluefor the current test only. - without_
fixture - Opts the current test out of any module-level fixture (see
fixture!).