ReportPortal Python Client

5.7.4 · active · verified Sun Apr 12

The reportportal-client is a Python client library for integrating test automation results with ReportPortal v5. It provides functionalities to manage launches, test items, and logs, including attachments. The current version is 5.7.4, and the library generally sees frequent minor updates, often several per month.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the basic usage of the ReportPortal Python client, including starting and finishing a launch, creating test items (steps), logging messages, and attaching files. It also shows how to integrate the client with Python's standard logging module using `RPLogger` and `RPLogHandler`. Environment variables are used for ReportPortal configuration (RP_ENDPOINT, RP_PROJECT, RP_API_KEY) for secure execution. The `client.terminate()` call in the finally block is essential for asynchronous operations.

import os
import subprocess
from mimetypes import guess_type
import logging

from reportportal_client import RPClient, RPLogger, RPLogHandler
from reportportal_client.helpers import timestamp

# Configure logging for the client itself (optional, but good practice)
logging.basicConfig(level=logging.DEBUG)

# ReportPortal configuration from environment variables
endpoint = os.environ.get('RP_ENDPOINT', 'http://localhost:8080/api/v1')
project = os.environ.get('RP_PROJECT', 'default_personal')
api_key = os.environ.get('RP_API_KEY', 'YOUR_API_KEY') # Get from your ReportPortal User Profile
launch_name = 'Example Launch'
launch_doc = 'Basic example of using reportportal-client.'

# Set up ReportPortal logger
logging.setLoggerClass(RPLogger)
rp_logger = logging.getLogger(__name__)
rp_logger.setLevel(logging.INFO)
rp_logger.addHandler(RPLogHandler(endpoint=endpoint, project=project, api_key=api_key, launch_uuid=None))

try:
    # Initialize RPClient
    client = RPClient(endpoint=endpoint, project=project, api_key=api_key)
    # Start log upload thread (for async mode)
    client.start()

    # Start a new launch
    launch = client.start_launch(
        name=launch_name,
        start_time=timestamp(),
        description=launch_doc,
        attributes=[{'key': 'client', 'value': 'python'}, {'value', 'example'}]
    )
    launch_uuid = launch.uuid
    rp_logger.info(f'Launch started with UUID: {launch_uuid}')

    # Start a test item (e.g., a test case or suite)
    test_item = client.start_test_item(
        name='Test Case 1',
        description='First Test Case',
        start_time=timestamp(),
        attributes=[{'key': 'suite', 'value': 'smoke'}],
        item_type='STEP',
        launch_uuid=launch_uuid
    )
    test_item_uuid = test_item.uuid
    rp_logger.info(f'Test Item started with UUID: {test_item_uuid}')

    # Log messages
    client.log(time=timestamp(), message='Hello from ReportPortal Client!', level='INFO', launch_uuid=launch_uuid, item_uuid=test_item_uuid)
    rp_logger.warning('This is a warning log using the standard Python logger.', attachment={
        'name': 'warning.txt',
        'data': b'This is an attached text file content.',
        'mime': 'text/plain'
    })

    # Simulate a subprocess call and attach output
    try:
        output = subprocess.check_output(['ls', '-l'], stderr=subprocess.STDOUT)
        client.log(time=timestamp(), message='ls -l output', level='DEBUG', launch_uuid=launch_uuid, item_uuid=test_item_uuid, attachment={
            'name': 'ls_output.txt',
            'data': output,
            'mime': 'text/plain'
        })
    except subprocess.CalledProcessError as e:
        client.log(time=timestamp(), message=f'Command failed: {e.output.decode()}', level='ERROR', launch_uuid=launch_uuid, item_uuid=test_item_uuid)

    # Finish the test item
    client.finish_test_item(item_uuid=test_item_uuid, end_time=timestamp(), status='PASSED', launch_uuid=launch_uuid)
    rp_logger.info(f'Test Item {test_item_uuid} finished.')

    # Finish the launch
    client.finish_launch(launch_uuid=launch_uuid, end_time=timestamp(), status='PASSED')
    rp_logger.info(f'Launch {launch_uuid} finished.')

except Exception as e:
    rp_logger.error(f'An error occurred: {e}')
finally:
    # It's crucial to call terminate() to ensure all pending requests are sent to ReportPortal
    client.terminate()
    rp_logger.info('Client terminated, all pending logs sent.')

view raw JSON →