A plugin that allows users to add attributes to their tests. These attributes can then be referenced by fixtures or the test itself.
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pytest_attributes-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1d77e6fe80e606805a9257ca56ca943d535da9ea8effd5eeedb5565953417a8 |
|
MD5 | 2c2454e659d4825866f14073a94a2afa |
|
BLAKE2b-256 | 3499f300bc7ed9afffa6dd943095f6f34b0f4ff57225e49cdec3282a719c180c |