DockerTest + postgreSQL Testing
Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
Search for a command to run...
Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
No comments yet. Be the first to comment.
Move beyond traditional RESTful thinking. Learn how to design APIs specifically for MCP (Model Context Protocol) servers. This guide covers the shift in mindset, a practical OpenAPI 3.1 example, and a Spring Boot implementation to make your services ...

Extending Kestra to Every Corner of Your Data Stack. Introduction: The Power of Plugins Imagine you're a master chef. You don't just have one knife - you have specialized tools for every task: a paring knife for delicate work, a chef's knife for chop...
Mastering Complex Orchestration Scenarios. Introduction: The Orchestrator's Toolkit Imagine you're conducting a symphony. You don't just wave your baton - you cue sections, adjust tempo, handle surprises, and ensure harmony. That's what advanced work...
From Data Extraction to Loading - A Practical Guide Introduction: Why ETL Still Matters in the Modern Data Stack Remember when data engineering was "extract, transform, load"? Some say ETL is dead, replaced by ELT, reverse ETL, and data mesh. But her...
Building Blocks of Declarative Orchestration. Introduction: The Power of Simplicity Imagine trying to build a house without understanding bricks, beams, and blueprints. That's what using an orchestration tool without understanding its core concepts f...

When developing applications that rely on a database or other external dependencies, it's often useful to have automated tests that can verify that everything is working as expected. However, setting up and tearing down these dependencies can be a hassle, especially when running tests locally. This is where Dockertest comes in. Dockertest is a Go library that provides a simple way to spin up Docker containers for use in integration testing.
In this article, we'll go through an example of using Dockertest in a Go application to test a PostgreSQL database.
Before we get started, make sure that you have the following software installed on your system:
Go (version 1.13 or higher)
Docker
Docker Compose
To install Dockertest, run the following command:
go get github.com/ory/dockertest/v3
First, let's create a test file that will use Dockertest to spin up a PostgreSQL database for our integration tests. Create a new file called database_test.go and add the following code:
package main
import (
"context"
"database/sql"
"fmt"
"log"
"testing"
"time"
_ "github.com/lib/pq"
"github.com/ory/dockertest/v3"
"github.com/stretchr/testify/assert"
)
var db *sql.DB
func TestMain(m *testing.M) {
pool, err := dockertest.NewPool("")
if err != nil {
log.Fatalf("Could not connect to Docker: %s", err)
}
resource, err := pool.Run("postgres", "latest", []string{
"POSTGRES_PASSWORD=secret",
"POSTGRES_USER=user",
"POSTGRES_DB=testdb",
})
if err != nil {
log.Fatalf("Could not start resource: %s", err)
}
hostPort := resource.GetHostPort("5432/tcp")
if err := pool.Retry(func() error {
var err error
db, err = sql.Open("postgres", fmt.Sprintf("host=localhost port=%s user=user password=secret dbname=testdb sslmode=disable", hostPort))
if err != nil {
return err
}
return db.Ping()
}); err != nil {
log.Fatalf("Could not connect to Docker: %s", err)
}
code := m.Run()
if err := pool.Purge(resource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}
os.Exit(code)
}
func TestDatabase(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
rows, err := db.QueryContext(ctx, "SELECT 1")
assert.NoError(t, err)
assert.True(t, rows.Next())
}
This code sets up a PostgreSQL container using Dockertest and ensures that the container is ready for use before running the tests. The tests themselves simply check that we can execute a simple SQL query against the database.
To run the tests, execute the following command:
go test -v ./...
This will output information about the tests as they run, and should ultimately show that the tests passed.
After the tests have run, the Docker container will still be running. To clean up the container, you can run the following command:
docker stop $(docker ps -aq)
This will stop all running containers on your system.
In conclusion, dockertest is a powerful Go library that makes it easy to write unit and integration tests for database-driven applications. By using this library, you can spin up real Docker containers for your databases in your test environment, and run tests against them, giving you a more accurate and reliable test suite.
We have covered the basics of dockertest in this article, including how to set up and use the library, and how to write test cases for PostgreSQL and MySQL databases. We have also seen how dockertest can be used with other Go testing frameworks like testing and Ginkgo.
Overall, dockertest is a great tool for any Go developer who needs to write database-driven applications and wants to have a robust testing framework. By using dockertest, you can be confident that your tests are running against real databases, and that your application will work correctly in production.
I hope this helps, you!!
More such articles:
https://www.youtube.com/@maheshwarligade
\==========================**=========================
If this article adds any value to you then please clap and comment.
Let’s connect on Stackoverflow, LinkedIn, & Twitter.