pub struct Fixture { /* private fields */ }Expand description
A reusable starting state for tests, replacing freshly provisioned empty resources (for example, pre-seeded database rows).
use encore::testing::Fixture;
static SEEDED: Fixture = Fixture::new("seeded", || Box::pin(seed()));
async fn seed() -> Result<(), encore::Error> {
// Ordinary app code: its resources are the fixture's while it is built.
DB.execute("INSERT INTO users (name) VALUES ('ada'), ('grace')", &[]).await?;
Ok(())
}
#[tokio::test]
async fn lists_users() -> Result<(), encore::Error> {
SEEDED.load().await?;
assert_eq!(list_users().await?.len(), 2);
Ok(())
}The setup function runs once per test run when a test first loads the fixture, regardless of how many tests, threads, or test binaries use it. Each resource modified during setup is snapshotted, and every test that loads the fixture receives its own isolated copy. Untouched resources start clean (or inherit from the parent fixture when extending another fixture).
Postgres databases, ClickHouse databases, object buckets, and Redis databases are snapshotted. Other resource types always start from their initial state.
Snapshots are cached across test runs as long as the app’s source code has not changed, except for fixtures that modify Redis (which are rebuilt each run).
For less boilerplate—and to automatically apply a fixture to every
#[encore::test] in a module—use the fixture! macro.
Implementations§
Source§impl Fixture
impl Fixture
Sourcepub const fn new(name: &'static str, setup: SetupFn) -> Self
pub const fn new(name: &'static str, setup: SetupFn) -> Self
Declares a new fixture. const, so it can initialize a static.
name uniquely identifies the fixture across test binaries; two
declarations with the same name share one snapshot built by whichever
loads first.
Auto Trait Implementations§
impl !Freeze for Fixture
impl !RefUnwindSafe for Fixture
impl !UnwindSafe for Fixture
impl Forget for Fixture
impl Move for Fixture
impl Send for Fixture
impl Sync for Fixture
impl Unpin for Fixture
impl UnsafeUnpin for Fixturewhere
&'static str: UnsafeUnpin,
&'static Fixture: UnsafeUnpin,
Align1<u8>: UnsafeUnpin,
*const u8 is !null: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
PhantomData<Mutex<Waitlist>>: UnsafeUnpin,
*const Waiter is !null: UnsafeUnpin,
Align8<usize>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request