Skip to content

Waiting for containers to start or be ready

There are scenarios where your tests need the external services they rely on to reach a specific state that is particularly useful for testing. This is generally approximated as 'Can we talk to this container over the network?' or 'Let's wait until the container is running an reaches certain state'.

Testcontainers for Rust comes with the concept of wait strategy, which allows your tests to actually wait for the most useful conditions to be met, before continuing with their execution.

The strategy is defined by the WaitFor enum with the following variants:

  • StdOutMessage - wait for a specific message to appear on the container's stdout
  • StdErrMessage - wait for a specific message to appear on the container's stderr
  • Healthcheck - wait for the container to be healthy
  • Http - wait for an HTTP(S) response with predefined conditions (see HttpWaitStrategy for more details)
  • Duration - wait for a specific duration. Usually less preferable and better to combine with other strategies.

Image implementation is responsible for returning the appropriate WaitFor strategies. For GenericImage you can use the with_wait_for method to specify the wait strategy.

Startup timeout and Poll interval

Ordinarily Testcontainers will wait for up to 60 seconds for containers to start. If the default 60s timeout is not sufficient, it can be updated with the ImageExt::with_startup_timeout(duration) method.