Expand description
Tests. Under encore test every test gets infrastructure of its own —
databases, buckets and topics no other test sees — with nothing to set up:
#[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 works out which test a call belongs to from where it is made: on the
test’s own thread (the test harness names it after the test), or on a thread
of the Tokio runtime the test runs in — a spawned task, spawn_blocking. The
test’s infrastructure is released when the test ends.
§#[encore::test]
Any test works: #[test], #[tokio::test]. #[encore::test]
is #[tokio::test] — it takes the same flavor and worker_threads — that
tells Encore about the test before it runs, rather than leaving it to be
worked out at the first call. Its runtime’s threads are part of the test from
the start, and its infrastructure is released as soon as it returns.
#[encore::test(flavor = "multi_thread")]
async fn handles_concurrent_requests() -> Result<(), encore::Error> {
// …
}§Fixtures
A test starts from the app’s freshly provisioned resources: an empty,
migrated database, an empty bucket. A Fixture is a state to start from instead, built
once by a setup of yours and then copied for each test that loads it. See
Fixture and fixture!.
§Other threads
A thread the test starts itself is neither, so Encore cannot tell which test
it belongs to, and using a resource on it is an error. (The exception is a
multi-thread runtime built on such a thread, which Encore takes for a test’s:
it gets infrastructure of its own.) Link the thread to the test with a
TestScope:
let scope = encore::testing::current()?;
std::thread::spawn(move || {
let _guard = scope.enter();
// … part of the test from here on
});Structs§
- Fixture
- A saved state for tests to start from, in place of the app’s freshly provisioned one: the rows a setup inserted, say.
- Scope
Guard - Keeps the thread part of a test. Returned by
TestScope::enter. - Test
Scope - A handle to one test, made by
current. Cheap to clone, and can be sent to other threads, to make what they do part of the test.
Functions§
- current
- The test the calling thread belongs to.
- published
- What the current test has published to
topic, in the order it was published: by the test itself, and by the code it called. - set_
config - Gives
configa value for the current test, in place of the app’s. No other test sees it, and it is gone when the test ends. The value is checked against the config’s schema, and read as its type, here. - set_
secret - Gives
secreta value for the current test, in place of the app’s. No other test sees it, and it is gone when the test ends. - without_
fixture - Makes the current test start without a fixture, where a module’s would
otherwise apply (see
fixture!). LikeFixture::load, it must come before the test first uses a resource.
Type Aliases§
- SetupFn
- A fixture’s setup: a function returning the future that puts the app’s resources in the state the fixture saves.