Testing has long been a reactive process in most organizations, triggered by code commits or schedules. While Kubernetes transformed how we deploy our applications and automated many of these activities, testing still needs to catch up.
In our previous article on Continuous Testing for the Cloud-Native Era, we discussed the mismatch between modern deployment practices and legacy testing methods, which creates several challenges, including environment inconsistencies, poor visibility into test coverage, and costly debugging sessions.
Such technical challenges impact business outcomes with delayed releases, reduced developer productivity, and increased infrastructure costs. As a result, a simple deployment that should have taken just a few minutes can take days or months to complete.
In this article, we will look at two complementary tools that can address these challenges: Testcontainers, which provide developers with greater control over containerized dependencies in their tests, and Testkube, which provides a cloud-native and vendor-agnostic platform that treats tests as versioned resources.
Testcontainers is an open source library that allows developers to spin up temporary, lightweight versions of services like databases, message queues, and browsers for testing purposes. At its core, Testcontainers leverages Docker to spin up real service containers programmatically during test execution.
It enables developers to write tests that rely on external services without mocks or complex setup scripts. It supports multiple programming languages, including Java, Go, Python, Node.js, and .NET.
For instance, developers can spin up a PostgreSQL container and use it for testing without actually creating one.
import pytest
from testcontainers.postgres import PostgresContainer
def test_postgres_connection():
# This automatically pulls and starts a PostgreSQL container
with PostgresContainer("postgres:14.1") as postgres:
# Get connection details from the running container
connection_url = postgres.get_connection_url()
# Connect to the database and run your tests
# No need for mocks or shared test databases!
db = connect_to_database(connection_url)
result = db.execute("SELECT 1")
assert result == 1
With each execution, Testcontainer guarantees a fresh, isolated container that mimics the actual service, ensuring reliable and consistent test results.
Due to their nature and seamless integration with various tools, Testcontainers fit in well with existing developer workflows:
Testcontainers became a popular choice among developers for the following reasons:
Testcontainers provide developers with a simpler way to deal with dependencies. However, they cannot test Kubernetes-specific features like ConfigMaps, Ingress objects, or other Kubernetes resources. Further, enterprise Kubernetes deployments demand capabilities beyond just developer-centric tools. That’s where Testkube comes into play: to provide a comprehensive platform for testing cloud native applications.
Let's understand how Testkube specifically complements the developer-focused Testcontainers approach:
Testkube provides a standalone open source agent that is deployed in your Kubernetes infrastructure for vendor-agnostic test execution and a commercial control plane that can be used to manage multiple agents with additional features for parallelization, orchestration, RBAC, centralized troubleshooting, and reporting, etc.
Enterprise setups need unified visibility that individual tools can't provide. Testkube offers a centralized dashboard for monitoring test execution across teams and services, along with standardized reporting formats.
Combining Testcontainers with Testkube allows you to create a continuous testing pipeline that spans from development environments to production clusters. It addresses the complete spectrum of testing needs from providing developers with the simplicity they require to giving platform teams the enterprise-scale orchestration they demand.
Developers can start by using Testcontainers for local testing, ensuring dependencies are properly containerized and tests are environment-agnostic. These same tests can then be packaged and deployed to Kubernetes via Testkube, orchestrating their execution within the cluster. Thus, tests written once can be executed across different environments with minimal modification, ensuring consistency throughout the delivery pipeline.
For instance, below is a TestWorkflow showing an Ollama HuggingFace test with Gradle and Testcontainers Workflow.
kind: TestWorkflow
apiVersion: testworkflows.testkube.io/v1
metadata:
name: gradle-with-testcontainers
namespace: testkube
spec:
content:
git:
uri: 'https://github.com/testcontainers/testcontainers-java'
revision: main
container:
workingDir: /data/repo/examples/ollama-hugging-face
resources:
requests:
cpu: 512m
memory: 512Mi
services:
dind:
logs: always
timeout: 5m
image: 'docker:27.4.0-dind'
env:
- name: DOCKER_TLS_CERTDIR
securityContext:
privileged: true
readinessProbe:
tcpSocket:
port: 2375
steps:
- name: Run tests
run:
image: 'gradle:8.5.0-jdk11'
env:
- name: TESTKUBE_GRADLE
value: 'true'
- name: DOCKER_HOST
value: 'tcp://{{ services.dind.0.ip }}:2375'
command:
- gradle
- '--no-daemon'
- test
artifacts:
paths:
- reports/**/*
- test-results/**/*
Read our Testcontainer document to learn more about using Testkube with Testcontainer.
Testcontainers and Testkube form a natural partnership, with each tool addressing distinct but complementary needs:
Testcontainers excels at:
Testkube specializes in:
Together, they create a comprehensive testing strategy that leverages the strengths of both approaches, eliminating gaps in your testing coverage.
Using Testcontainers and Testkube together can help you excel in different areas, like:
Enterprise organizations face numerous challenges implementing Kubernetes at scale. These include the scalability of testing infrastructure, security risks in containerized environments, compliance with regulatory frameworks, and efficient resource utilization.
That’s where the combined synergy of Testcontainers and Testkube comes in, addressing these challenges through a comprehensive testing approach.
The combined approach uses Kubernetes capabilities to distribute tests across the cluster based on priority and requirements. Testkube enables the concurrent execution of TestWorkflows and provides configurable options through its built-in parallelization capabilities to run multiple tests simultaneously. This architecture supports efficient scaling by running test executors across multiple nodes, spinning up additional pods during peak periods, and scaling down during lean periods.
Testcontainers execute within defined security contexts, such as non–root users and read–only file systems, while Testkube provides support for Role-Based Access Control (RBAC) to help you enforce how members of an Organization have access to Environments and their Resources. This multi-layered approach ensures isolation and secure execution of tests in your CI pipeline.
In addition, Testkube runs any testing tool from inside your Kubernetes clusters; you do not need to make your applications accessible to external networks for testing purposes. It also integrates with tools like OWASP Zap, which allows you to scan, test, and identify vulnerabilities across the application, including the front-end, back-end, and APIs.
Testkube maintains detailed records of all test executions, providing audit evidence for compliance requirements. By treating tests as Kubernetes resources, changes are version-controlled and traceable, supporting change management requirements. Testcontainers, on the other hand, provide isolated, disposable environments for running tests. They allow for controlled testing, allow developers to better track the dependencies and configurations used in their tests, and help in audits and compliance reviews.
Testcontainers are ephemeral by nature and are provisioned and reclaimed dynamically. They ensure that containers are destroyed regardless of whether tests are executed successfully or fail, keeping costs in check. With Testkube, tests are Kubernetes resources, which makes it easy for organizations to track resource consumption by team or project, enabling accurate cost allocation and management.
By addressing these enterprise concerns, the combined approach of using Testcontainers with Testkube enables organizations to implement robust Kubernetes testing at scale without compromising on security, compliance, or cost efficiency.
Using Testkube and Testcontainers creates a powerful and comprehensive testing strategy for Kubernetes environments. It bridges the gap between development simplicity and enterprise requirements. Testcontainers provide developers with consistent and containerized testing environments, while Testkube extends these capabilities with Kubernetes native orchestration, centralized management, and platform-level controls.
By adopting this approach, teams can accelerate release cycles, reduce failures, and build greater confidence in their Kubernetes deployments.
Ready to transform your Kubernetes testing strategy? Try Testkube today and experience how it complements your existing Testcontainers workflow to deliver enterprise-grade testing capabilities. You can also reach out to the Testkube team, plus a vibrant community of fellow Kubernetes testers is on Slack.
Testkube is a test execution and orchestration framework for Kubernetes that works with any CI/CD system and testing tool you need, empowering teams to deliver on the promise of agile, efficient, and comprehensive testing programs by leveraging all the capabilities of K8s to eliminate CI/CD bottlenecks, perfecting your testing workflow. Get started with Testkube's free trial today.
Related topics: