Skip to main content
PyPI version Python versions

Zafira plugin for pytest

Pytest plugin for integration with Zafira.


Installation

Install using command:

$ pip install pytest-zafira

Enable the fixture explicitly in your tests or conftest.py:

pytest_plugins = ['pytest_zafira.zafira_plugin', 'pytest_zafira.screenshot_plugin']

Configuration

pytest-zafira plugin searches configuration file in the current working directory and it called ‘zafira_properties.ini’. File has to look like:

[config]
service-url = url for zafira-server + '/zafira-ws'
zafira_enabled = True (turn on/off fixture)
zafira_app_url = url of deployed zafira
access_token = access_token from Zafira dashboard
job_name = any string you like
suite_name = any string you like
artifact_expires_in_default_time = (int value in sec.) an expiration time for amazon bucket link
artifact_log_name = test_logs
aws_access_key = secret
aws_secret_key = secret
aws_screen_shot_bucket = secret
s3_save_screenshots = True (save screenshots to AWS S3 bucket)

More about access_token find here Integration of Zafira.

After that step you have to configure logging. An example of logging configuration file (yaml):

version: 1
formatters:
  default_formatter:
    format: '%(asctime)s:%(levelname)s - %(message)s'
    datefmt: '%H:%M:%S'
handlers:
  console:
    class: logging.StreamHandler
    level: 0
    formatter: default_formatter
    stream: ext://sys.stdout
  zafira_log_appender:
    class: pytest_zafira.RabbitHandler
loggers:
  zafira:
    level: INFO
    handlers: [console, zafira_log_appender]
    propagate: no
root:
  level: WARN
  handlers: [console, zafira_log_appender]

Then add META_INFO logging level for logger:

import logging.config
import os

import yaml


class LoggingConfiguration:
    def __init__(self):
        self.ROOT_LOG_LEVEL = self.fetch_env(expected_env='ROOT_LOG_LEVEL', default='')

        self.ZAFIRA_LOG_LEVEL = self.fetch_env(expected_env='ZAFIRA_LOG_LEVEL', default='')

        # and this level is responsible for screenshots
        self.add_meta_info_level()
        # apply configuration from logging.cfg
        self.apply_configuration()
        # set log level according to env variables, do nothing if they empty or overwrite default from logging.cfg
        self.init_loggers()

    def init_loggers(self):
        zafira_logger = logging.getLogger('zafira')
        if self.ZAFIRA_LOG_LEVEL:
            zafira_logger.setLevel(self.ZAFIRA_LOG_LEVEL)

    @staticmethod
    def add_meta_info_level():
        """
        Logging level for screenshots
        """
        meta_info_level = logging.DEBUG + 1
        add_logging_level('META_INFO', meta_info_level)

    def apply_configuration(self):
        config = yaml.load(open('path/to/your/logging/config/file.yml', 'r'))
        if self.ROOT_LOG_LEVEL:
            config['root']['level'] = os.environ['ROOT_LOG_LEVEL']
        logging.config.dictConfig(config)

    @staticmethod
    def fetch_env(expected_env, default):
        if expected_env in os.environ:
            return os.environ[expected_env]
        else:
            return default


def add_logging_level(level_name, level_number):
    """
    Comprehensively adds a new logging level to the `logging` module and the
    currently configured logging class.

    `levelName` becomes an attribute of the `logging` module with the value
    `levelNum`. `methodName` becomes a convenience method for both `logging`
    itself and the class returned by `logging.getLoggerClass()` (usually just
    `logging.Logger`). If `methodName` is not specified, `levelName.lower()` is
    used.

    To avoid accidental clobberings of existing attributes, this method will
    raise an `AttributeError` if the level name is already an attribute of the
    `logging` module or if the method name is already present
    """
    method_name = level_name.lower()

    if hasattr(logging, level_name):
        raise AttributeError('{} already defined in logging module'.format(level_name))
    if hasattr(logging, method_name):
        raise AttributeError('{} already defined in logging module'.format(method_name))
    if hasattr(logging.getLoggerClass(), method_name):
        raise AttributeError('{} already defined in logger class'.format(method_name))

    def log_for_level(self, message, *args, **kwargs):
        if self.isEnabledFor(level_number):
            self._log(level_number, message, args, **kwargs)

    def log_to_root(message, *args, **kwargs):
        logging.log(level_number, message, *args, **kwargs)

    logging.addLevelName(level_number, level_name)
    setattr(logging, level_name, level_number)
    setattr(logging.getLoggerClass(), method_name, log_for_level)
    setattr(logging, method_name, log_to_root)

and activate this config when tests will start.

Usage

To use just run the pytest`s tests.

License

Distributed under the terms of the Apache Software License 2.0 license, “pytest-zafira” is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.

Metadata

Release files for pytest-zafira 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-zafira 1.0.3
File Size Uploaded
pytest-zafira-1.0.3.tar.gz 22.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-zafira 1.0.3
File Interpreter ABI Platform
pytest_zafira-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 48.2 kB

Release files / pytest-zafira-1.0.3.tar.gz

Download URL pytest-zafira-1.0.3.tar.gz
Size 22.0 kB
Tags Source
SHA-256 checksum
How to use checksums
969cfaedb423635b97b1a1a0a0abab15605277664a302cf2778c09feae247e15
BLAKE2b-256 checksum
How to use checksums
0c7160fdba7bdfa59e09c38c44a9806c29397572d6d163ac1c0962b536b60eda
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.8

Release files / pytest_zafira-1.0.3-py3-none-any.whl

Download URL pytest_zafira-1.0.3-py3-none-any.whl
Size 26.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e478ed2957778a30be1e8ba6d5fe009afdef2eb93c938567f0a5438655b4cbfe
BLAKE2b-256 checksum
How to use checksums
05fce527bc606aa2d6848e76cd905b0705dccb4d398a07059442ca100adeeec8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.8

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page