Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_attributes-1.0.0.tar.gz (4.4 kB view hashes)

Uploaded Source

Built Distribution

pytest_attributes-1.0.0-py3-none-any.whl (5.5 kB view hashes)

Uploaded Python 3

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