VOOZH about

URL: https://thenewstack.io/kubernetes-end-to-end-testing-using-testcontainers-and-python/

⇱ Kubernetes End-to-End Testing Using Testcontainers and Python - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-12-12 10:00:21
Kubernetes End-to-End Testing Using Testcontainers and Python
sponsor-andela,sponsored-post-contributed,tutorial,
Kubernetes / Python / Software Testing

Kubernetes End-to-End Testing Using Testcontainers and Python

Learn how to combine testcontainers-k3s and Python to create isolated, reproducible, production-like environments for thorough testing.
Dec 12th, 2024 10:00am by Manish Saini
👁 Featued image for: Kubernetes End-to-End Testing Using Testcontainers and Python
Featured image by Alex Shuper for Unsplash+.
Andela sponsored this post.

Testing Kubernetes applications often involves dealing with multiple dependencies and a complex environment. While end-to-end (E2E) testing provides a realistic way to validate application behavior, replicating production environments can be challenging. With Testcontainers, you can simplify this process by using lightweight, disposable containers to emulate a Kubernetes cluster and its dependencies.

You can use the testcontainers-python library to perform end-to-end testing for a Kubernetes application.

What Is Testcontainers?

Testcontainers is an open source library that supports running lightweight, disposable containers for testing. With the addition of Kubernetes-focused modules like testcontainers-k3s, you can spin up a Kubernetes cluster as part of your test setup.

Using Testcontainers for Kubernetes testing offers:

  • Realistic testing environments: Emulate Kubernetes clusters and services within isolated containers.
  • Automation: Automate the setup and teardown of dependencies like databases, message brokers or Kubernetes.
  • Efficiency: Run tests in a clean, repeatable environment without manually configuring Kubernetes clusters.
  • Dynamic configurations: Customize dependencies on the fly for each test scenario.

Set Up Testcontainers for Python

Prerequisites

    1. Python 3.8 or higher: Install the latest version.
    2. Docker: Ensure Docker is installed and running.
    3. Install Testcontainers: Use pip to install the library.

Run an E2E Test for a Kubernetes Application

In this step-by-step example, I’ll show you how to test a Kubernetes application that interacts with a PostgreSQL database. The test will:

  1. Spin up a Kubernetes cluster using testcontainers-k3s.
  2. Deploy the application and database.
  3. Validate the application’s behavior through HTTP requests.

1. Create a Python Test Class

The following script sets up a Kubernetes cluster using K3sContainer from Testcontainers:

2. Create Kubernetes Manifests

Create Kubernetes manifests for the application and PostgreSQL database.

app.yaml

postgres.yaml

3. Run the Test

Execute the test using your preferred Python test runner, such as Pytest:

pytest test_kubernetes_app.py

This test will:

  1. Start PostgreSQL in a Testcontainers instance.
  2. Spin up a lightweight Kubernetes cluster.
  3. Deploy the application and database.
  4. Verify the application is reachable and functioning as expected.

Best Practices for Kubernetes E2E Testing

  1. Resource management: Ensure your system has enough resources to run containers and the Kubernetes cluster.
  2. Namespace isolation: Use separate namespaces for each test to avoid conflicts.
  3. Mock external APIs: Mock dependencies that are not directly under test for reliability.
  4. Parallelization: Optimize test execution by running independent test cases in parallel.

Conclusion

Due to its distributed nature, end-to-end testing in Kubernetes can be intimidating; however, Testcontainers makes the process easier. By combining the testcontainers-k3s module with Python, you can create isolated, reproducible and production-like environments for thorough testing. This ensures your application behaves as expected in Kubernetes before it goes live.

Adopting this approach lets you catch critical issues earlier in the pipeline and deliver more robust software. Why not give Testcontainers a try and enhance your Kubernetes testing strategy?

Learn more about the possibilities of Kubernetes in Andela’s article How to Run Databases on Kubernetes: An 8-Step Guide.

Andela provides the world’s largest private marketplace for global remote tech talent driven by an AI-powered platform to manage the complete contract hiring lifecycle. Andela helps companies scale teams & deliver projects faster via specialized areas: App Engineering, AI, Cloud, Data & Analytics.
Learn More
The latest from Andela
Hear more from our sponsor
TRENDING STORIES
Manish is a Senior SDET Consultant and a technologist with Andela, a private global marketplace for digital talent. With more than 9 years of experience in Fintech, e-commerce and Cloud, Manish empowers testing teams to achieve faster delivery and exceptional...
Read more from Manish Saini
Andela sponsored this post.
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: Class, Docker.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.