testcontainers / testcontainers-rs

A library for integration-testing against docker containers from within Rust.
https://rust.testcontainers.org
Apache License 2.0
770 stars 141 forks source link

How to start containers only once for all tests? #195

Closed pronvis closed 4 years ago

pronvis commented 4 years ago

Currently I fail to move container into variable because of Container<'a, ..., ...> lifetime. This lifetime depends on let docker = clients::Cli::default(); lifetime, which I can't move into generic option of another variable.

Also, I fail to move container initialization into lazy static macro.

Any ideas how to avoid those issues and create docker containers only once for all tests?

thomaseizinger commented 4 years ago

Thanks for the report!

One of the design goals of testcontainers was test isolation, which is why we are using Rust's lifetimes to enforce this. Most of this library's design is really oriented around that, the backend is currently embarrassingly simple as we just call out to the docker CLI. Hence, if you don't have the requirement of test isolation, it may be easier to roll your own solution of starting a docker container.

Solving this from within Rust is actually tricky because Rust's testing framework doesn't expose any hooks at the moment that run before or after all tests.

You might be better off to write yourself a shell-script that just starts a docker container, calls cargo test and stops them again afterwards if you want to share a container among all your tests :)