Expand description
End-to-end system tests that run against the full application.
While unit tests run inside a single service in isolation (service-to-service
calls are not served and Pub/Sub subscriptions do not run), tests in a
system test suite—a crate whose directory contains encore.systest.json—run
against the whole app in a dedicated test session. Services start on demand
just like encore run, using test secrets, test configs, and isolated test
data.
use encore::testing::system;
static BILLING: Database = Database::named("billing");
#[encore::test]
async fn placing_an_order_charges_the_card() -> Result<(), Box<dyn std::error::Error>> {
let order = orders::place(&PlaceParams { sku: "tee".into(), qty: 2 }).await?;
// The order's event has been handled by billing's subscription.
system::settled().await?;
let row = BILLING
.query_one("SELECT amount FROM charges WHERE order_id = $1", &[&order.id])
.await?;
assert_eq!(row.get::<_, i64>(0), 4000);
Ok(())
}A system test can call any service and access any resource declared by the
app (either via ::named(...) or through a service’s own declaration).
Each test gets isolated data across every Postgres database, ClickHouse database, Redis database, and object bucket (with migrations applied). All services handling requests for that test operate on the test’s isolated data, so tests can run in parallel without interfering with one another.
System tests can also load a Fixture. When the fixture’s
setup runs against the app and settles, the resulting state across all
databases, buckets, and Redis instances is snapshotted and copied for each
test that loads it.
Background work that is not triggered by a test request (such as a worker
loop or polling job) runs against the session’s shared data. To test such
background work, set "data": "shared" in encore.systest.json; tests in
that suite share data with background workers and with each other.
Published Pub/Sub messages are delivered automatically to subscriptions in
their declaring services. Call settled to wait for delivery rather than
calling deliver.
Each test has an ID that propagates automatically across all outgoing requests and messages so Encore can attribute work and route data correctly. Making a service call or publishing a message from an unassociated thread panics.
To test frontends in a real browser, call browser to launch a managed
headless Chromium instance connected to the test session with automatic
recording and telemetry.
Run system tests with encore test, or with cargo test inside the suite’s
directory.
Structs§
- Browser
- The current test’s browser: see
browser. Cheap to clone, and every clone is the same browser. - Browser
Options - Options for launching a test browser with
browser_with. - Deliveries
- The deliveries of a
Telemetry, fromTelemetry::deliveries. - Event
- An event of a span.
- Link
- A link to another span.
- Log
- A line a service logged, or something Encore captured of a request.
- Lost
Context - An inter-service call that dropped its incoming request context (and
therefore its test ID). See
Telemetry::lost_context. - Publishes
- The publishes of a
Telemetry, fromTelemetry::publishes. - Queries
- The queries of a
Telemetry, fromTelemetry::queries. - Requests
- The requests of a
Telemetry, fromTelemetry::requests. - Settle
Options - The options of
settled_with. - Span
- One span.
- Telemetry
- Spans and logs recorded by the test session for a single system test.
- Telemetry
Options - The options of
telemetry_with.
Enums§
- Kind
- A span’s kind.
- Record
- Controls when
browserrecordings are saved. - Status
- A span’s status.
- What
- Classification of a recorded
Spanfor test assertions.
Functions§
- browser
- Returns a managed headless Chromium
Browserfor the current test, launching it on first call and closing it when the test ends. - browser_
with - Like
browser, with customBrowserOptions. Options only take effect on the first call in a test; subsequent calls return the same browser. - headers
- Returns HTTP headers that associate a custom client’s request with the current test (carrying the test ID in Request Context and the active trace context).
- settled
- Waits up to 30 seconds for all asynchronous work triggered by the current
test to finish (no in-flight service or gateway requests, and no queued,
active, or retrying Pub/Sub deliveries). See
settled_withto customize the timeout. - settled_
with - Like
settled, with customSettleOptions. If the app does not settle in time, the returned error lists what work is still in progress. - telemetry
- Waits for the app to settle and returns the
Telemetryrecorded for the current test, including all service spans, inter-service requests, Pub/Sub deliveries, queries, and log lines. - telemetry_
with - Like
telemetry, with customTelemetryOptions(for example, to inspect telemetry immediately without waiting for the app to settle). - url
- Returns the URL of the named gateway or frontend in the current test session, for use with custom clients (such as a WebSocket client). Frontends start on demand on the first connection.