ReportPortal Python Client
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
- breaking Python 3.8 support was officially removed in version 5.7.0. Projects using Python 3.8 or older will need to upgrade their Python version to use recent client versions.
- breaking The `log_manager.py` module, which included classes like `RPLogger` and `RPLogHandler`, was removed in version 5.7.0. These classes are now directly available under the top-level `reportportal_client` package.
- gotcha The configuration parameter `log_batch_payload_size` was renamed to `log_batch_payload_limit` in version 5.6.7. If you use a custom configuration file or environment variables with the old name, it will no longer be recognized.
- breaking The `NOT_FOUND` constant was removed in version 5.6.3 as it caused infinite issues according to release notes.
- gotcha When using the client in asynchronous mode (which is default for `RPClient`), it is crucial to call `client.terminate()` after all test operations are complete. Failing to do so may result in unsent logs or test items, as pending requests may not be flushed to the ReportPortal server.
- gotcha ReportPortal supports both API Key and OAuth 2.0 Password Grant authentication. If both API key and OAuth parameters are provided, OAuth 2.0 authentication will take precedence. Ensure your configuration correctly specifies the desired authentication method.
Install
-
pip install reportportal-client
Imports
- RPClient
from reportportal_client import RPClient
- RPLogHandler
from reportportal_client import RPLogHandler
- RPLogger
from reportportal_client import RPLogger
- timestamp
from reportportal_client.helpers import timestamp
Quickstart
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.')