Solvro Talks - Integration testing with Testcontainers

Kształt kółka
Kształt półkola
Ksztalt koła dekoracyjny
Solvro Talks - Integration testing with Testcontainers

Solvro Talks - Integration testing with Testcontainers

What are Testcontainers?

Testcontainers is a library available in various programming languages that
uses Docker to run lightweight containers in order to simulate
external system dependencies. For example, you can run containers with databases,
message brokers, web services, and other components that
our application interacts with.

How to get started with Testcontainers?

To start working with Testcontainers, we need:
1. Docker installed - Docker is essential for running containers.
2. Testcontainers library - you need to add the appropriate library to your project (e.g., in
the case of Java, add a dependency to Maven or Gradle).
3. Container configuration - define which containers will be run during
tests and what configurations they should have.

Sample Test Structure

We need to fetch dependencies from the Maven repository:

<dependency>
   <groupId>org.testcontainers</groupId>
   <artifactId>mysql</artifactId>
   <scope>test</scope>
</dependency>
<dependencyManagement>
   <dependencies>
      <dependency>
         <groupId>org.testcontainers</groupId>
         <artifactId>testcontainers-bom</artifactId>
         <version>1.19.8</version>
        <type>pom</type>
        <scope>import</scope>
     </dependency>
   </dependencies>
</dependencyManagement>

To avoid specifying the version for each dependency, you can use BOM or Bill of Materials.
A sample test using Testcontainers might look like this:

public class MyIntegrationTest {


   @Rule
   public MySQLContainer mysql = new MySQLContainer("mysql:6.0");


   @Test
   public void testSomething() {
      String jdbcUrl = mysql.getJdbcUrl();
      //apply any logic related to database connection
   }
}

The MySQL database container is started before the test and shut down after its
completion. This ensures that the database is always in its initial state.

Debugging

Debugging integration tests with Testcontainers is usually simpler because
we can easily replicate the test environment. In case of problems, we can:
● Check container logs - Testcontainers allows access to logs
of running containers, which helps in diagnosing issues.
● Run tests locally - We can run tests on local development
machines, which facilitates quick iteration and debugging.

Advantages and Disadvantages of Testcontainers

Advantages:
● Ease of use - Simple configuration
● Support for multiple programming languages - Available in various languages,
such as Java, Python, .NET.
● Test isolation - Each test runs in an isolated environment
● Minimal requirements - Only requires Docker installed.
● Creating custom containers - Allows defining custom containers
tailored to specific needs.
● Open-source project - enabling the community to participate in development.

Disadvantages:
● Compatibility - Older operating systems may not be compatible with
Docker, and consequently, with Testcontainers.

Summary

Testcontainers is a powerful tool supporting integration testing through
the use of Docker containers. It enables the creation of isolated, easily
replicable test environments, which significantly improves the quality and reliability of tests.

Awatar Dawid Rymarczyk

Dawid Rymarczyk

Prezes 2023/2024
Paradox Call To Action Image
Paradox Shape Image

Join our team!

Contact us

JoinJoin