Skip to main content

pytest fixture logging configured from packaged YAML

Project description

pytest fixture logging configured from packaged YAML

PyPI status License Python versions supported
Test suite status pytest-logging-strict coverage percentage Quality check status
GitHub stars msftcangoblowme on Mastodon

* Python 3.9 through 3.13, PyPy

New in 0.1.x

initial release;

Why?

Every single test, of interest, has boilerplate to setup the logging configuration. Which is coming from a variable in a constants module.

In each and every package. That logging configuration is hardcoded.

logging-strict package manages logging configurations as strictly validated YAML files. pytest-logging-strict adds pytest integration which includes an easy to use fixture.

The alternatives is pull a str or a dict from a constants module. Or dealing with built-in pytest fixture, caplog.

With pytest integration:

  • querying

    Once per pytest session. Query options are provided in pyproject.toml. cli provided options override.

    Pulls the logging config YAML from logging-strict, but can pull from any installed package. Can submit your logging config YAML file to logging-strict.

    Share your logging configuration. Have it accessible and available for all your packages, and as a bonus, everyone elses’ packages.

  • extracting

    Once per pytest session. Overrides logging-strict to force extracting into session scoped temp folder. After each session automagically removed.

  • pytest fixture

    Use logging_strict fixture. Provides both the logger and list of all available loggers.

    So know which loggers are enabled besides only the main package logger

Installation

python -m pip install pytest-logging-strict

Configuration

In conftest.py

pytest_plugins = ["logging_strict"]

In pyproject.toml

Customize the query. If not, the default is taken from logging-strict package.

[tool.pytest.ini_options]
logging_strict_yaml_package_name = 'logging_strict'
logging_strict_package_data_folder_start = 'configs'
logging_strict_category = 'worker'
logging_strict_genre = 'mp'
logging_strict_flavor = 'asz'
logging_strict_version_no = '1'

and/or cli

pytest --showlocals -vv --logging-strict-yaml-package-name = 'logging_strict' \
--logging-strict-package-data-folder-start = 'configs' \
--logging-strict-category = 'worker' \
--logging-strict-genre = 'mp' \
--logging-strict-flavor = 'asz' \
--logging-strict-version-no = '1' tests

The cli overrides pyproject.toml settings.

Usage

Minimalistic example

pytest marker sends param package name to the fixture. Creates the main logger instance. While still having access to all possible loggers defined in the logger config YAML file. e.g. root and asyncio.

import pytest

@pytest.mark.logging_package_name("my_package_name")
def test_fcn(logging_strict):
    t_two = logging_strict()
    if t_two is not None:
        logger, lst_loggers = t_two
        logger.info("Hello World!")

The pytest marker communicates ur package name to logging_strict fixture. Which then initiates the main logger instance.

Full example

import logging
from logging_strict.tech_niques import captureLogs
import pytest

@pytest.mark.logging_package_name("my_package_name")
def test_fcn(logging_strict):
    t_two = logging_strict()
    if t_two is None:
        logger_name_actual == "root"
        fcn = logger.error
    else:
        assert isinstance(t_two, tuple)
        logger, lst_loggers = t_two
        logger_name_actual = logger.name
        logger_level_name_actual = logging.getLevelName(logger.level)

        msg = "Hello World!"

        # log message was logged and can confirm
        with captureLogs(
            logger_name_actual,
            level=logger_level_name_actual,
        ) as cm:
            fcn(msg)
        out = cm.output
        is_found = False
        for msg_full in out:
            if msg_full.endswith(msg):
                is_found = True
        assert is_found

Batteries included

textual console apps

pytest --showlocals -vv --logging-strict-yaml-package-name = 'logging_strict' \
--logging-strict-package-data-folder-start = 'configs' \
--logging-strict-category = 'app' \
--logging-strict-genre = 'textual' \
--logging-strict-flavor = 'asz' \
--logging-strict-version-no = '1' tests

multiprocess worker – default

pytest --showlocals -vv --logging-strict-yaml-package-name = 'logging_strict' \
--logging-strict-package-data-folder-start = 'configs' \
--logging-strict-category = 'worker' \
--logging-strict-genre = 'mp' \
--logging-strict-flavor = 'asz' \
--logging-strict-version-no = '1' tests

Please submit your logging configuration for review and curation to make available to everyone.

In the meantime or if not in the mood to share

pytest --showlocals -vv --logging-strict-yaml-package-name = 'zope.interface' \
--logging-strict-package-data-folder-start = 'data' \
--logging-strict-category = 'worker' \
--logging-strict-genre = 'mp' \
--logging-strict-flavor = 'mine' \
--logging-strict-version-no = '1' tests

The package data file would be stored as:

data/mp_1_mine.worker.logging.config.yaml

The flavor, e.g. mine, should be alphanumeric no whitespace nor underscores. e.g. justonebigblob

Milestones

  • Simplify querying

    logging-strict#4 will add support for a config TOML file. Which will contain logging config YAML records.

    Then the file naming convention will be dropped.

    The config TOML file is placed at the package base folder. And is the reference point to advertise which logging config YAML files are in the package.

  • classifier

    pypi.org allows searching by classifiers. So will be easier for everyone to identify which packages offer logging config YAML files

License

aGPLv3+ [full text]

Collaborators

Note there is no code of conduct. Will adapt to survive any mean tweets or dodgy behavior.

Can collaborate by:

ACTUALLY DO SOMETHING … ANYTHING

  • use pytest-logging-strict in your own packages’ tests

  • peer review and criticism. Make me cry, beg for leniency, and have no other recourse than to appeal to whats left of your humanity

  • request features

  • submit issues

  • submit PRs

  • follow on mastodon. Dropping messages to say hello or share offensive memes

  • translate the docs into other languages

  • leave a github star on repos you like

  • write distribute and market articles to raise awareness

ASK FOR HELP

  • ask for eyeballs to review your repo

  • request for support

FOSS FUNDING

  • apply force and coersion to take your monero or litecoin

  • fund travel to come out to speak at tech conferences (currently residing in West Japan)

  • Mr. Money McBags printer goes Brrrrr. Get assistance towards identifying package maintainers in need of funding

ASK FOR ABUSE

  • Throw shade, negativity, and FUD at everything and anything. Do it! Will publically shame you into put your money where your mouth is.

  • pointless rambling and noise that leads no where. Will play spot the pattern and respond with unpleasent truths, or worse, offensive memes

  • Threaten to be useful or hold higher standing. e.g. recruiters or NPOs/NGOs

  • suggest a code of conduct. Ewwwww! That’s just down right mean

  • suggest a license written by a drunkard

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_logging_strict-0.1.0.post0.tar.gz (77.5 kB view details)

Uploaded Source

Built Distribution

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

pytest_logging_strict-0.1.0.post0-py3-none-any.whl (41.5 kB view details)

Uploaded Python 3

File details

Details for the file pytest_logging_strict-0.1.0.post0.tar.gz.

File metadata

File hashes

Hashes for pytest_logging_strict-0.1.0.post0.tar.gz
Algorithm Hash digest
SHA256 c049eed63d698bfbd32f17e1d6f0c9ef64b9659fa73140be58b54149165f15bc
MD5 2c38c71a6d27194cd04d84156fcac42b
BLAKE2b-256 bad2e247ec5746cc370b3af9b4ecc3c2544290fa5bc555a50bdec8440de630d2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_logging_strict-0.1.0.post0.tar.gz:

Publisher: release.yml on msftcangoblowm/pytest-logging-strict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_logging_strict-0.1.0.post0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_logging_strict-0.1.0.post0-py3-none-any.whl
Algorithm Hash digest
SHA256 e4669bbf01ecc88a2b820641853e5622f003a2b69deff0489e42ad6ef995eb0b
MD5 6458b8fb0de96f187476447e56882d3a
BLAKE2b-256 3293f2c17d62a26833118ae66a6fe8ac316b06939d89b876a1834e054afe4671

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_logging_strict-0.1.0.post0-py3-none-any.whl:

Publisher: release.yml on msftcangoblowm/pytest-logging-strict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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