Hi all,
I am trying to run a docker container using testcontainers-rs at the beginning of the tests using static
variable so I can have single container shared by all tests.
// std::sync::OnceLock from rust 1.70
static CONTAINER: OnceLock<Container<IMAGE>> = OnceLock::new();
fn container() -> &'static Container<IMAGE> {
CONTAINER.get_or_init(...)
}
// more code ...
There are more code but those are irrelevant at the moment. This code works fine, I have one container for the all the tests and containers is not even started when none of the tests is touching it so it's also lazy as far as I can tell which is great. However, I have a small problem with restriction of the statics variables;
Static items do not call drop at the end of the program.
testcontainers-rs actually implements Drop
which removes the containers when it goes out of scope, but due to the above restrictions containers stay around after the tests. I would like to be able to add some sort of afterAll
or shutdownHook
behavior to tests but I can't seem to find anything.
Thanks in advance!
NOTE: Please consider this as a learning because I know that there are already some libraries like sqlx with sqlx:test macro which creates database
(as in database inside given database instance
) and allow you to use dedicated database
for each tests. However I am using Diesel (again for the sake of learning) which doesn't have such support.