Skip to main content

Python client for Report Portal v5.

Project description

ReportPortal python client

PyPI Python versions Build Status codecov.io Join Slack chat! stackoverflow Build with Love

Library used only for implementors of custom listeners for ReportPortal

Already implemented listeners:

Installation

The latest stable version is available on PyPI:

pip install reportportal-client

IMPORTANT! The latest client version does not support Report Portal versions below 5.0.0.

Version 3 is the latest one which supports Report Portal versions below 5.0.0 to install it:

pip install reportportal-client~=3.0

Contribution

All the fixes for the client that supports Report Portal versions below 5.0.0 should go into the v3 branch. The master branch will store the code base for the client for Report Portal versions 5 and above.

Usage

Main classes are:

  • reportportal_client.ReportPortalService
  • reportportal_client.ReportPortalServiceAsync(Client version 3.x only)

Basic usage example:

import os
import subprocess
import traceback
from mimetypes import guess_type
from time import time

# Report Portal versions below 5.0.0:
from reportportal_client import ReportPortalServiceAsync

# Report Portal versions >= 5.0.0:
from reportportal_client import ReportPortalService


def timestamp():
    return str(int(time() * 1000))


endpoint = "http://10.6.40.6:8080"
project = "default"
# You can get UUID from user profile page in the Report Portal.
token = "1adf271d-505f-44a8-ad71-0afbdf8c83bd"
launch_name = "Test launch"
launch_doc = "Testing logging with attachment."


def my_error_handler(exc_info):
    """
    This callback function will be called by async service client when error occurs.
    Return True if error is not critical and you want to continue work.
    :param exc_info: result of sys.exc_info() -> (type, value, traceback)
    :return:
    """
    print("Error occurred: {}".format(exc_info[1]))
    traceback.print_exception(*exc_info)


# Report Portal versions below 5.0.0:
service = ReportPortalServiceAsync(endpoint=endpoint, project=project,
                                   token=token, error_handler=my_error_handler)

# Report Portal versions >= 5.0.0:
service = ReportPortalService(endpoint=endpoint, project=project,
                                   token=token)

# Start launch.
launch = service.start_launch(name=launch_name,
                              start_time=timestamp(),
                              description=launch_doc)

# Start test item Report Portal versions below 5.0.0:
test = service.start_test_item(name="Test Case",
                               description="First Test Case",
                               tags=["Image", "Smoke"],
                               start_time=timestamp(),
                               item_type="STEP",
                               parameters={"key1": "val1",
                                           "key2": "val2"})

# Start test item Report Portal versions >= 5.0.0:
item_id = service.start_test_item(name="Test Case",
                                  description="First Test Case",
                                  start_time=timestamp(),
                                  item_type="STEP",
                                  parameters={"key1": "val1",
                                              "key2": "val2"})


# Create text log message with INFO level.
service.log(time=timestamp(),
            message="Hello World!",
            level="INFO")

# Create log message with attached text output and WARN level.
service.log(time=timestamp(),
            message="Too high memory usage!",
            level="WARN",
            attachment={
                "name": "free_memory.txt",
                "data": subprocess.check_output("free -h".split()),
                "mime": "text/plain"
            })

# Create log message with binary file, INFO level and custom mimetype.
image = "/tmp/image.png"
with open(image, "rb") as fh:
    attachment = {
        "name": os.path.basename(image),
        "data": fh.read(),
        "mime": guess_type(image)[0] or "application/octet-stream"
    }
    service.log(timestamp(), "Screen shot of issue.", "INFO", attachment)

# Create log message supplying only contents
service.log(
    timestamp(),
    "running processes",
    "INFO",
    attachment=subprocess.check_output("ps aux".split()))

# Finish test item Report Portal versions below 5.0.0.
service.finish_test_item(end_time=timestamp(), status="PASSED")

# Finish test item Report Portal versions >= 5.0.0.
service.finish_test_item(item_id=item_id, end_time=timestamp(), status="PASSED")

# Finish launch.
service.finish_launch(end_time=timestamp())

# Due to async nature of the service we need to call terminate() method which
# ensures all pending requests to server are processed.
# Failure to call terminate() may result in lost data.
service.terminate()

Send attachment (screenshots)

python-client uses requests library for working with RP and the same semantics to work with attachments (data).

There are two ways to pass data as attachment:

Case 1 - pass file-like object

with open(screenshot_file_path, "rb") as image_file:
    rp_logger.info("Some Text Here",
                   attachment={"name": "test_name_screenshot.png",
                               "data": image_file,
                               "mime": "image/png"})

Case 2 - pass file content itself (like you did)

with open(screenshot_file_path, "rb") as image_file:
        file_data = image_file.read()

rp_logger.info("Some Text Here",
               attachment={"name": "test_name_screenshot.png",
                           "data": file_data,
                           "mime": "image/png"})

Copyright Notice

Licensed under the Apache 2.0 license (see the LICENSE.txt file).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reportportal-client-5.3.0.tar.gz (41.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reportportal_client-5.3.0-py2.py3-none-any.whl (54.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file reportportal-client-5.3.0.tar.gz.

File metadata

  • Download URL: reportportal-client-5.3.0.tar.gz
  • Upload date:
  • Size: 41.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.2

File hashes

Hashes for reportportal-client-5.3.0.tar.gz
Algorithm Hash digest
SHA256 a6f8b06bbcd3fb74ea33823d9e22edbecbce040aa64c7af22293d6129fb2a568
MD5 a919cb5b23a06790a530bc295ab84a9a
BLAKE2b-256 00bc3d445b367a299cbeba361f5ea39a995b216eaa49f6ed9f591891a8d1e1a5

See more details on using hashes here.

File details

Details for the file reportportal_client-5.3.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for reportportal_client-5.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7654f5915b15191b9bc714841d0ec6c7a54732a746da71a36751e8a48d8e6c41
MD5 e56f54221de193596b3a2e13285e2053
BLAKE2b-256 54a1a18bd119483a2cb3365b1c2175da8a95ec77ac1bf0ee891b131bcd37ae39

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page