You have successfully subscribed to the Testkube newsletter.
You have successfully subscribed to the Testkube newsletter.
Oops! Something went wrong while submitting the form.
Table of Contents
Pytest and Testkube Integration Summary
Summary
1
Pytest Integration Challenge: While Pytest is a popular, flexible Python testing framework with minimal boilerplate code, integrating it into Kubernetes environments traditionally poses scaling and parallel execution challenges.
2
Testkube Solution:Testkube is a Kubernetes-native test orchestration framework that stores tests as Custom Resource Definitions (CRDs), enabling seamless scaling and integration with CI/CD pipelines.
3
Simple Workflow Creation: Creating test workflows is straightforward - you define a YAML spec that includes your Git repository, Docker image, and shell commands (like pytest test-api-endpoint.py) without complex scripting.
4
End-to-End Management: A single YAML file manages everything related to your tests including code, environments, resources, and artifacts, making the testing process more efficient and robust.
5
Universal Testing Platform:Testkube works with any testing tool (not just Pytest) and any CI/CD system, positioning itself as the only native Kubernetes test orchestration framework for comprehensive testing workflows.
Testing modern distributed applications within Kubernetes environments can be daunting due to the complexity and the need for scalable solutions. Traditional testing tools often fall short when it comes to efficiency and agility.
However, with the advent of Kubernetes native solutions like Testkube, it's easier than ever to integrate powerful testing frameworks such as Pytest into your testing workflows. In this comprehensive guide, we'll explore how to leverage Testkube with Pytest to streamline your testing processes in Kubernetes.
Pytest Overview
Python remains a top choice for programming among developers due to its simplicity and robust ecosystem. Pytest, a popular framework within this ecosystem, excels in testing Python-based applications, but not only. It is preferred for its minimalistic design, flexibility, and rich feature set, which makes it ideal to test any type of application. It includes:
Minimal Design: Pytest reduces boilerplate code, facilitating quick and easy test case creation.
Flexibility: Its modular nature and extensive plugin ecosystem allow for significant customization.
Rich Feature Set: Features like auto-discovery, assertive properties, and module fixtures streamline test management.
Diverse Testing: Pytest supports both unit and integration tests, ensuring you are covered in most testing scenarios.
While Pytest is robust for testing your applications, integrating it into Kubernetes can pose challenges such as scaling and parallel execution. This is where Testkube comes into play.
Why Use Testkube To Run Pytests in Kubernetes?
Testkube is specifically designed to orchestrate and scale Pytest executions within Kubernetes, taking full advantage of Kubernetes' scalability, flexibility, and efficiency. Here’s why it stands out:
Kubernetes Native: By storing tests as Kubernetes Custom Resource Definitions (CRDs), Testkube ensures compatibility and scalability.
Integration with CI/CD Tools: Testkube seamlessly integrates with existing CI/CD pipelines, enhancing end-to-end testing capabilities.
Simplified Workflow Creation: Without the need for complex scripting, Testkube facilitates the creation of detailed test workflows, allowing for better control and customization of test executions.
Creating a Pytest Test Workflow
We’ve created a custom Pytest image for this example, but you can also create your own. For all the files and examples shown on this blogpost, refer to this Pytest folder.
To demonstrate the power of Testkube with Pytest, let’s create a simple test workflow. We first create a Pytest test that checks an API endpoint for the correct number of objects returned.
import pytest
import requests
deftest_validate_object_count():# Send a GET request to the API endpoint response = requests.get("https://api.restful-api.dev/objects")
# Assert that the response status code is 200 (OK)assert response.status_code == 200# Parse the JSON response data = response.json()
# Validate the number of objects in the responseassertlen(data) == 13, f"Expected 13 objects, but received {len(data)}"
Below is are the steps to set up a Test Workflow in Testkube:
Prepare Your Kubernetes Cluster: Ensure your cluster has the Testkube agent installed and configured.
Navigate to Test Workflows: In the Testkube dashboard, click on "Add A New Test Workflow" and select "Create From Scratch".
Workflow Configuration: Follow the wizard to set up your workflow. Provide the test's image, the shell command (pytest test-api-endpoint.py), and the Git repository details.
On the next screen, you have to define the source of your test.
Choose Git from the drop-down.
Provide the path to the Git repository that has the test.
Provide a path if the test file isn’t in the root folder.
Check the “Use path as working directory” checkbox.
The wizard's last page shows you the final yaml spec generated based on the values you provided.
Click on “Create” to create and save the Test Workflow.
Executing a Test Workflow
Click on “Run Now” to run the workflow. Clicking on the respective execution will show you the logs, artifacts, and the underlying workflow file.
Creating a Test Workflow in Testkube is straightforward and simple. We saw that just from one yaml file, we can manage everything related to our test - code, environments, resources, and artifacts. This makes your testing process and workflows more efficient and robust.
Conclusion
Pytest is one of Python's most popular testing frameworks, and Testkube is the only native Kubernetes test orchestration framework. Leveraging both these tools together streamlines your testing process for Kubernetes applications.
As we saw in this post, developers can benefit from Testkube’s Kubernetes capabilities and Pytest’s flexibility in creating efficient Test Workflows. You can also bring in any other testing tool and create a Test Workflow, not just Pytest.
If you already have a testing tool and want to experience Test Workflows, visit our website to learn more about Testkube's capabilities and how it can transform your testing workflow. You can also join our Slack channel for any assistance.
FAQs
Pytest with Testkube FAQ
Pytest is a Python testing framework known for its minimal setup, flexible plugin ecosystem, and rich features like fixtures, auto-discovery, and detailed assertions. It supports unit, integration, and functional testing, making it a go-to framework for Python and non-Python applications.
Yes. Pytest can test Kubernetes applications, but running it directly inside Kubernetes often requires additional setup for scaling, orchestration, and CI/CD integration. Tools like Testkube simplify this by natively running Pytest inside clusters.
Common challenges include managing test containers, handling distributed execution, scaling tests across clusters, and integrating results into CI/CD pipelines. These issues often lead to fragile workflows if managed manually.
Testkube runs Pytest tests as Kubernetes Jobs using Custom Resource Definitions (CRDs). This ensures tests scale with cluster resources, are easy to version-control, and integrate seamlessly with CI/CD systems.
A Test Workflow is a declarative YAML-based definition that describes how tests should be executed in Kubernetes. It bundles configuration, test commands, repositories, and resources in one spec for repeatable and automated execution.
You create a Test Workflow in the Testkube dashboard or via YAML, reference your Git repo containing Pytest tests, define the container image (with Pytest installed), and specify the shell command (e.g., pytest test-api-endpoint.py).
Traditional setups often require custom Kubernetes scripts, manual scaling, and separate infrastructure for test management. Testkube removes this overhead by making tests Kubernetes-native, scalable, and CI/CD-friendly.
Yes. Testkube is tool-agnostic. It supports any testing tool that can run inside a container, including Cypress, Postman, JMeter, Playwright, and custom executors.
Testkube distributes test executions across Kubernetes pods. This allows parallel test runs, optimized resource allocation, and more efficient execution at scale without bottlenecks.
A Pytest Test Workflow YAML defines metadata, Git repository paths, container images, working directories, and execution steps. It ensures tests are versioned, reproducible, and Kubernetes-native.
Testkube provides detailed logs, artifacts, and workflow definitions for each execution. Developers can drill into failed runs, analyze container logs, and rerun specific steps for faster debugging.
Yes. Testkube connects with CI/CD tools like GitHub Actions, GitLab CI, and Jenkins. Tests can be triggered by commits, pull requests, or Kubernetes events, ensuring continuous testing across the lifecycle.
Key benefits include scalability, Kubernetes-native execution, simplified orchestration, CI/CD integration, cost efficiency (reuse existing clusters), and reduced test maintenance overhead.
Not necessarily. You can use prebuilt Testkube Pytest executor images or create your own Docker image if you need additional dependencies or plugins.
Install the Testkube agent in your Kubernetes cluster, create a Pytest Test Workflow via dashboard or YAML, link your Git repo, and run tests directly from the cluster. Full guides and examples are available on the Testkube GitHub repo and docs.
Testkube is a test execution and orchestration framework for Kubernetes that works with any CI/CD system and testing tool you need. It empowers 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.