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.
§Without encore test
cargo test and cargo-nextest work too, and so does an editor’s button that
runs one test: Encore finds the app the test is in, and starts its daemon
with the encore on PATH if it is not running. Its secrets and configs
have the values configured for tests, as under encore test. The app’s
.env files are not read: give a test the variables it needs yourself.
ENCORE_TEST=0 turns this off, and ENCORE_TEST=1 turns it on for a test
runner Encore does not recognize.
§#[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
});Modules§
- system
- System tests: tests of the whole app, which run as no service.
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.
- deliver
- Runs
subscription’s handler on each message the current test — or the code it called — published to its topic that the subscription has not handled yet, one at a time, in the order they were published, as part of the test. Returns how many it handled. - 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.