A Pytest plugin providing essential utilities like soft assertions.
Project description
Pytest Essentials
A Pytest plugin providing essential utilities to enhance your testing workflow, starting with a robust Soft Assert mechanism.
Features
- Soft Assertions: Collect multiple assertion failures within a single test without stopping on the first failure.
- Automatically checks all collected assertions at the end of each test.
- Integrates with Allure reports to attach assertion failure details.
- Configurable failure behavior per test or globally:
broken: Mark test as "broken" (default, uses a customSoftAssertBrokenTestError).failed: Mark test as "failed" (usespytest.fail()).passed: Log assertion errors but allow the test to pass.
Installation
You can install pytest-essentials using pip:
pip install pytest-essentials
If you want to use the Allure reporting integration for soft assertion details, also install allure-pytest:
pip install pytest-essentials[allure]
# or
pip install pytest-essentials allure-pytest
The plugin will be automatically discovered by Pytest once installed.
Usage
Soft Assertions
Import the SoftAssert class from pytest_essentials. The plugin automatically provides a fixture that will call assert_all() on any SoftAssert instances used during a test.
from pytest_essentials import SoftAssert
import pytest
class TestMyFeature:
def test_example_with_soft_asserts(self):
sa = SoftAssert()
sa.assert_equal(1, 1, "Message for first check")
sa.assert_true(False, "This condition should have been true")
sa.assert_in("key", {"other_key": "value"}, "Checking for key in dict")
sa.assert_equal("actual", "expected", "Comparing two strings")
# By default, if any of the above assertions fail, the test will be marked 'broken'.
# All failures will be reported.
@pytest.mark.soft_assert_level("failed")
def test_soft_asserts_marked_failed(self):
sa = SoftAssert()
sa.assert_equal(1, 2, "This will cause a failure")
sa.assert_true(True, "This one is okay")
# This test will be marked as 'failed' if assert_equal(1,2) fails.
@pytest.mark.soft_assert_level("passed")
def test_soft_asserts_marked_passed(self):
sa = SoftAssert()
sa.assert_is_none("some_value", "This value should be None")
# This test will pass, but the assertion error for assert_is_none
# will be logged (e.g., to console and Allure if configured).
Configuration
You can set the default failure level for soft assertions globally:
1. Command-line option:
pytest --soft-assert-level=failed
2. pytest.ini file:
[pytest]
soft_assert_level = failed ; broken, failed, or passed
The priority for determining the failure level is:
@pytest.mark.soft_assert_level("level")marker on the test function.--soft-assert-levelcommand-line option.soft_assert_levelinpytest.ini.- Default built-in level (
broken).
Contributing
Contributions are welcome! Please open an issue or submit a pull request. (Further details to be added)
License
This project is licensed under the MIT License. (Assumed, can be changed in pyproject.toml)
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_essentials-0.1.1.tar.gz.
File metadata
- Download URL: pytest_essentials-0.1.1.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
146854308236edcee754dde68931a3ad903af442f458f5342d225df94519b747
|
|
| MD5 |
73c1855305058d69b2dfee8e7eaba5f8
|
|
| BLAKE2b-256 |
003393e57aff65abad0b9b12d0989c6c12a49d5485cee5dfab11a614668d6a45
|
File details
Details for the file pytest_essentials-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pytest_essentials-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e587fac14100150df0eac4a208ce0c143eec0de4cada35033e82a116a56b3e1b
|
|
| MD5 |
5d68476ffb29fe817e312f287673f890
|
|
| BLAKE2b-256 |
680ad4a1ed822f752f52d9dc2690b7385869593f0637df4ec89027abdf300cfe
|