Tavern: API Testing Framework

3.3.3 · active · verified Thu Apr 16

Tavern is a Python library, pytest plugin, and command-line tool designed for automated testing of APIs, including RESTful, MQTT, and gRPC services. It utilizes a simple, concise, and flexible YAML-based syntax for defining tests, making it highly customizable for complex scenarios. Currently at version 3.3.3, Tavern maintains an active development status and integrates seamlessly with the pytest ecosystem for comprehensive test management and reporting.

Common errors

Warnings

Install

Imports

Quickstart

To quickly get started, define your API tests in a `.tavern.yaml` file and execute them using `pytest`. Tavern automatically discovers and runs tests defined in files matching the `test_*.tavern.yaml` pattern when pytest is invoked.

import pytest
import os

# Create a dummy test_example.tavern.yaml file
with open('test_example.tavern.yaml', 'w') as f:
    f.write("""
---
test_name: Get some fake data from the JSON placeholder API
stages:
  - name: Make sure we have the right ID
    request:
      url: https://jsonplaceholder.typicode.com/posts/1
      method: GET
    response:
      status_code: 200
      json:
        id: 1
""")

# Run pytest (this assumes pytest is installed and finds the .tavern.yaml file)
# In a real scenario, you'd run 'pytest' from the command line.
# For programmatic execution, you would use tavern.core.run directly.
print("Created test_example.tavern.yaml. Run 'pytest -v test_example.tavern.yaml' in your terminal.")
# For demonstration, we'll simulate running it (actual pytest.main() might exit the interpreter)
# pytest.main(['-v', 'test_example.tavern.yaml'])

view raw JSON →