TestRail API Python Wrapper

1.13.6 · active · verified Sat Apr 11

testrail-api is a Python wrapper for the TestRail API, enabling programmatic interaction with TestRail instances for managing test cases, runs, results, and more. It is actively maintained with frequent minor releases, typically addressing new TestRail API features, bug fixes, and Python version compatibility. The current version is 1.13.6.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the TestRail API client using environment variables for credentials and fetch a list of projects. Ensure you replace the placeholder values or set your environment variables correctly.

import os
from testrail_api import TestRailAPI

# Ensure these environment variables are set:
# TR_BASE_URL (e.g., 'https://yourinstance.testrail.io/')
# TR_USER (e.g., 'user@example.com')
# TR_PASSWORD_OR_API_KEY (API key is recommended)

TR_BASE_URL = os.environ.get('TR_BASE_URL', '')
TR_USER = os.environ.get('TR_USER', '')
TR_PASSWORD_OR_API_KEY = os.environ.get('TR_PASSWORD_OR_API_KEY', '')

if not all([TR_BASE_URL, TR_USER, TR_PASSWORD_OR_API_KEY]):
    print("Please set TR_BASE_URL, TR_USER, and TR_PASSWORD_OR_API_KEY environment variables.")
    exit(1)

try:
    client = TestRailAPI(
        base_url=TR_BASE_URL,
        user=TR_USER,
        password=TR_PASSWORD_OR_API_KEY,
    )

    # Example: Get all projects
    projects = client.projects.get_projects()
    print(f"Successfully connected. Found {len(projects)} projects.")
    if projects:
        print(f"First project: {projects[0]['name']}")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →