Skip to main content

How to Deploy a pytest Testing Project Using Cookiecutter

Use Cookiecutter to bootstrap consistent pytest projects across an organization. Same skeleton, different teams, zero copy-paste drift.

Introduction

Cookiecutter is a powerful command-line utility that creates projects from templates, making it an excellent tool for standardizing and deploying pytest-based automation projects. Whether you’re setting up test automation for web applications, APIs, or mobile apps, Cookiecutter can help you bootstrap projects quickly while maintaining consistency across your organization.

In this article, we’ll explore how to create and deploy a comprehensive pytest automation project template using Cookiecutter, covering everything from basic setup to advanced deployment strategies.

Why use Cookiecutter for test automation?

The main issue you try to solve when deploying a cross-organization testing project is how to force the sources to be identical, but allow flexibility where it’s needed.

For that, Cookiecutter is an amazing tool that ensures:

  • Consistency - all team members start with the same project structure, coding standards, and configuration files.
  • Speed - reduces project setup time from hours to minutes by automating the creation of boilerplate code and configuration.
  • Best practices - embeds testing best practices, CI/CD configurations, and project organization patterns directly into the template.
  • Customization - allows for flexible project generation based on different requirements (web testing, API testing, mobile testing, etc.).

How to use templating for the deployment

Let’s use a simple automation project as an example. Edit the cookiecutter.json file with data about the test project:

{
  "project_name": "my-test-suite",
  "project_slug": "{{ cookiecutter.project_name|lower|replace(' ', '_') }}",
  "author_name": "Mor Dabastany",
  "python_version": "3.11",
  "test_target": ["web", "api", "mobile"],
  "use_pre_commit": "yes"
}

In pytest.ini we configure:

[pytest]
addopts = -v --strict-markers --tb=short
testpaths = tests
markers =
    smoke: smoke tests
    regression: regression tests
    api: API-only tests

In requirements.txt we put the packages we’re about to use:

pytest>=8.0.0
pytest-html
pytest-xdist
requests
pyyaml

Let’s deploy

Now, when we’d like to create a new project, we can use the project templating. We can also pack multiple conftest.py, fixtures.py, and other sources into the template, then deploy a custom project from the source template:

pip install cookiecutter
cookiecutter gh:your-org/pytest-template

Cookiecutter prompts for each variable defined in cookiecutter.json, then writes a fresh project tailored to the answers. Same skeleton, different teams, zero copy-paste drift.