Skip to main content

A Zafira plugin for pytest

Project description

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.

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

pytest-zafira-1.0.3.tar.gz (22.0 kB view details)

Uploaded Source

Built Distribution

pytest_zafira-1.0.3-py3-none-any.whl (26.2 kB view details)

Uploaded Python 3

File details

Details for the file pytest-zafira-1.0.3.tar.gz.

File metadata

  • Download URL: pytest-zafira-1.0.3.tar.gz
  • Upload date:
  • Size: 22.0 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for pytest-zafira-1.0.3.tar.gz
Algorithm Hash digest
SHA256 969cfaedb423635b97b1a1a0a0abab15605277664a302cf2778c09feae247e15
MD5 53ab20ddbca26667944285290a8a89e7
BLAKE2b-256 0c7160fdba7bdfa59e09c38c44a9806c29397572d6d163ac1c0962b536b60eda

See more details on using hashes here.

File details

Details for the file pytest_zafira-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: pytest_zafira-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 26.2 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for pytest_zafira-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e478ed2957778a30be1e8ba6d5fe009afdef2eb93c938567f0a5438655b4cbfe
MD5 18cf458e6a18df236231984af9178d52
BLAKE2b-256 05fce527bc606aa2d6848e76cd905b0705dccb4d398a07059442ca100adeeec8

See more details on using hashes here.

Supported by

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