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.
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:
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.
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:
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.
```python
import pytest
import requests
def test_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 response
assert len(data) == 13, f"Expected 13 objects, but received {len(data)}"
```
Below is are the steps to set up a Test Workflow in Testkube:
On the next screen, you have to define the source of your test.
The wizard's last page shows you the final yaml spec generated based on the values you provided.
Below is the yaml spec for the Pytest workflow:
```yaml
kind: TestWorkflow
apiVersion: testworkflows.testkube.io/v1
metadata:
name: pytest
namespace: testkube-agent
spec:
content:
git:
uri: https://github.com/kubeshop/testkube-examples.git
revision: main
paths:
- Pytest-Test-Workflow
container:
workingDir: /data/repo/Pytest-Test-Workflow
image: atulinfracloud/pytest-executor:latest
steps:
- name: Run test
shell: pytest test-api-endpoint.py
```
Click on “Create” to create and save the 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.
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.
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: