Testing Kubernetes Applications with Pytest and Testkube

Jun 3, 2024
read
Bruno Lopes
Product Leader
Testkube
Read more from
Bruno Lopes

Table of Contents

Start your free trial.

Start your free trial.

Start your free trial.

Explore Testkube hands-on.
30 days
no commitment
$0
no credit card needed

Subscribe to our monthly newsletter to stay up to date with all-things Testkube.

Please disable pixel blocker extension
You have successfully subscribed to the Testkube newsletter.
You have successfully subscribed to the Testkube newsletter.
Oops! Something went wrong while submitting the form.
Jun 3, 2024
read
Bruno Lopes
Product Leader
Testkube
Read more from
Bruno Lopes
Bruno Lopes
Product Leader
Testkube
This guide shows how to leverage Testkube with Pytest to streamline your testing processes in Kubernetes.

Table of Contents

Executive Summary

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 it isn’t limited to them. 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 test auto-discovery, detailed assertion introspection, and modular 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 Pytest Tests 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 in this blog post, 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.

Below are the steps to set up a Test Workflow in Testkube:

  1. Prepare Your Kubernetes Cluster: Ensure your cluster has the Testkube agent installed and configured.
  2. Navigate to Test Workflows: In the Testkube dashboard, click on "Add A New Test Workflow" and select "Create From Scratch".
  3. 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.
Testkube Create a Test Workflow wizard with the Pytest image and shell command

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.
Testkube Test Workflow wizard Source step with the Git repository, branch, and test path

The wizard's last page shows you the final YAML spec generated based on the values you provided.

Testkube Test Workflow wizard Summary step showing the generated Pytest workflow YAML

Below is the YAML spec for the Pytest workflow:

kind: TestWorkflow
apiVersion: testworkflows.testkube.io/v1
metadata:
  name: pytest
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. 

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. 

Testkube execution view of a passed Pytest Test Workflow with log output

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.

Frequently Asked Questions

Can Pytest run inside a Kubernetes cluster?

Yes. With Testkube, Pytest runs as a Test Workflow inside your cluster. Testkube pulls your tests from Git, runs them in a container, and stores the logs and artifacts for each execution.

What do I need to create a Pytest Test Workflow in Testkube?

You need a cluster with the Testkube agent installed, a container image with Pytest and your test dependencies, the shell command that runs your tests (for example, pytest test-api-endpoint.py), and the Git repository and path where the tests live. The wizard turns these values into a YAML spec.

Do I need a custom Docker image to run Pytest with Testkube?

No. This guide uses a custom Pytest image, but any image that includes Python, Pytest, and the packages your tests import will work. Set it in the container image field of the workflow.

Can I trigger Pytest workflows from my CI/CD pipeline?

Yes. Testkube integrates with existing CI/CD pipelines, so you can start a Pytest Test Workflow from your pipeline while the tests themselves run inside Kubernetes.

Where can I see Pytest results in Testkube?

Open the Test Workflow in the Testkube dashboard and select an execution. You can view the logs, the artifacts, and the underlying workflow file for that run.

{{banner-get-started}}

Bruno Lopes
Product Leader
Testkube
Read more from
Bruno Lopes

About Testkube

Testkube is the open testing platform for AI-driven engineering teams. It runs tests directly in your Kubernetes clusters, works with any CI/CD system, and supports every testing tool your team uses. By removing CI/CD bottlenecks, Testkube helps teams ship faster with confidence.
Get Started with a trial to see Testkube in action.