Skip to main content

Assign Attributes to your Test Functions and Access Them from Anywhere

Overview

A powerful tool that allows you to add custom attributes and variables to your tests.

With this plugin, you can easily attach additional data to each test, which can then be referenced by fixtures or even the test itself. This enables you to create more flexible and dynamic test suites tailored to your specific needs.

This is an enhanced alternative to pytest mark decorators that allows retrieval of attribute information within fixtures and functions

Features

  • Custom Attributes for Tests - Add custom attributes to individual tests using the @attributes marker, allowing you to store important metadata or configuration data directly with the test.

  • Access Attributes in Fixtures - Easily access attribute values within fixtures, enabling you to create dynamic fixtures that adapt to the specific requirements of each test.

  • Reference Attributes in Tests - Directly access attribute values within the test functions themselves, providing a convenient way to parameterize tests, perform conditional logic, or customize test behavior based on the attached attributes.

Installation

pip install pytest-attributes

Usage

First, import attributes from pytest_attributes:

from pytest_attributes import attributes

Now you can add attributes to each of your tests using the @attributes marker. Like so:

@attributes(
    step = 1,
    action = "Test the functionality of feature X",
    expected = "Feature X works successfully"
    )
def test_functionality():
    assert True

In the above example, we created our attributes marker, and then added whatever parameters we wanted inside and set those values. Now those values are associated with this specific test called "test_functionality".

We can now reference these attributes by using the provided keyterm 'attr':

@attributes(
    step = 1,
    action = "Test the functionality of feature X",
    expected = "Feature X works successfully"
    )
def test_functionality(attr):
    print(attr.step)
    print(attr.action)
    print(attr.expected)
    assert True

The above example prints the following:

test.py 1
Test the functionality of feature X
Feature X works successfully
.

More importantly, however, we can use attr to get these attributes from within fixtures! This opens up the door to many possibilities, such as determining what to do with each test before running it, attaching attributes to report files, and even sending them alongside the test results to any desired endpoints.

The process of doing this is extremely simple. Simply provide attr as an argument to the desired fixture and you can access its attributes. Like so:

@pytest.fixture(autouse=True)
def my_fixture(attr):
    print(attr.action)

Example Code

my_test.py

import pytest
from pytest_attributes import attributes

@attributes(
    step = 1,
    action = "Test the functionality of feature X",
    expected = "Feature X works successfully"
    )
def test_functionality(attr):
    print(attr.step)
    print(attr.action)
    print(attr.expected)
    assert True

conftest.py

import pytest

@pytest.fixture(autouse=True)
def my_fixture(attr):
    print(attr.action)

Contributing

Contributions are very welcome. Tests can be run with tox_, please ensure the coverage at least stays the same before you submit a pull request.

License

Distributed under the terms of the BSD-3_ license, "pytest-attributes" is free and open source software

Issues

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

.. _file an issue: https://github.com/MichaelE55/pytest-attributes/issues

Release files for pytest-attributes 1.0.0

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-attributes 1.0.0
File Size Uploaded
pytest_attributes-1.0.0.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-attributes 1.0.0
File Interpreter ABI Platform
pytest_attributes-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.9 kB

Release files / pytest_attributes-1.0.0.tar.gz

Download URL pytest_attributes-1.0.0.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
764a09b27507e9045188e8871a97e2a7ed7c1b35b59b715599ab9c4f29e7d7f8
BLAKE2b-256 checksum
How to use checksums
ae7926b2364994bff9be768039e10b5ca90f9b8e8d5309a30746311098a1088b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.8.10

Release files / pytest_attributes-1.0.0-py3-none-any.whl

Download URL pytest_attributes-1.0.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c1d77e6fe80e606805a9257ca56ca943d535da9ea8effd5eeedb5565953417a8
BLAKE2b-256 checksum
How to use checksums
3499f300bc7ed9afffa6dd943095f6f34b0f4ff57225e49cdec3282a719c180c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.8.10

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.1.2

2 release files

0.1.1

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