# Pytest soft asserts.
![PyPI](https://img.shields.io/pypi/v/nrt-pytest-soft-asserts?color=blueviolet&style=plastic)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/nrt-pytest-soft-asserts?color=greens&style=plastic)
![PyPI - License](https://img.shields.io/pypi/l/nrt-pytest-soft-asserts?color=blue&style=plastic)
![PyPI - Downloads](https://img.shields.io/pypi/dd/nrt-pytest-soft-asserts?style=plastic)
![PyPI - Downloads](https://img.shields.io/pypi/dm/nrt-pytest-soft-asserts?color=yellow&style=plastic)
[![Coverage Status](https://coveralls.io/repos/github/etuzon/python-nrt-pytest-soft-asserts/badge.svg)](https://coveralls.io/github/etuzon/pytohn-nrt-pytest-soft-asserts)
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/etuzon/python-nrt-pytest-soft-asserts?style=plastic)
![GitHub last commit](https://img.shields.io/github/last-commit/etuzon/python-nrt-pytest-soft-asserts?style=plastic)
[![DeepSource](https://app.deepsource.com/gh/etuzon/python-nrt-pytest-soft-asserts.svg/?label=active+issues&token=d3XBT3-sw5yOtGTGWIJMpmT_)](https://app.deepsource.com/gh/etuzon/python-nrt-pytest-soft-asserts/?ref=repository-badge)
### Supported asserts:
| Assert | Description | Example | Return |
|-------------------------------------------------------------|-----------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------|-----------------------------------------------------|
| assert_true(condition, message=None) | Verify that condition is True. | soft_asserts.assert_true(a == b) | True if assertion passes, False if assertion fails. |
| assert_false(condition, message=None) | Verify that condition is False. | soft_asserts.assert_false(a == b) | True if assertion passes, False if assertion fails. |
| assert_equal(first, second, message=None) | Verify that first is equal to second. | soft_asserts.assert_equal(a, b) | True if assertion passes, False if assertion fails. |
| assert_not_equal(first, second, message=None) | Verify that first is not equal to second. | soft_asserts.assert_not_equal(a, b) | True if assertion passes, False if assertion fails. |
| assert_is(first, second, message=None) | Verify that first and second are the same object. | soft_asserts.assert_is(a, b) | True if assertion passes, False if assertion fails. |
| assert_is_not(first, second, message=None) | Verify that first and second are not the same object. | soft_asserts.assert_is_not(a, b) | True if assertion passes, False if assertion fails. |
| assert_is_none(obj, message=None) | Verify that obj is None. | soft_asserts.assert_is_none(a) | True if assertion passes, False if assertion fails. |
| assert_is_not_none(obj, message=None) | Verify that obj is not None. | soft_asserts.assert_is_not_none(a) | True if assertion passes, False if assertion fails. |
| assert_in(obj, container, message=None) | Verify that obj is in container. | soft_asserts.assert_in(a, [a, b, c]) | True if assertion passes, False if assertion fails. |
| assert_not_in(obj, container, message=None) | Verify that obj is not in container. | soft_asserts.assert_not_in(a, [b, c]) | True if assertion passes, False if assertion fails. |
| assert_is_instance(obj, cls, message=None) | Verify that obj is instance of cls. | soft_asserts.assert_is_instance(a, A) | True if assertion passes, False if assertion fails. |
| assert_is_not_instance(obj, cls, message=None) | Verify that obj is not instance of cls. | soft_asserts.assert_is_not_instance(a, B) | True if assertion passes, False if assertion fails. |
| assert_almost_equal(first, second, delta, message=None) | Verify that first is almost equal to second,<br/>and the different is equal or less to delta. | soft_asserts.assert_almost_equal(1.001, 1.002, 0.1) | True if assertion passes, False if assertion fails. |
| assert_not_almost_equal(first, second, delta, message=None) | Verify that first is not almost equal to second,<br/>and the different is more than delta. | soft_asserts.assert_not_almost_equal(1.001, 1.002, 0.00001) | True if assertion passes, False if assertion fails. |
| assert_raises(exception, method: Callable, *args, **kwargs) | Verify that method execution raise exception. | soft_asserts.assert_raises(TypeError, sum, 'a', 2) | True if assertion passes, False if assertion fails. |
| assert_raises_with(exception, message=None) | Verify that execution in 'with' block raise exception. | with soft_asserts.assert_raised_with(ValueError):<br/> raise ValueError(ERROR_MESSAGE_1) | |
In the end of each test, the soft asserts will be verified and the test will be marked as failed if any of the asserts failed.<br/>
To verify the soft asserts in the middle of the test, call `soft_asserts.assert_all()`.<br/>
<br/>
assert_all() will raise _AssertionError_ if any of the asserts failed.<br/>
#### Steps
Each testing section can be divided to steps.<br/>
The meaning of this is that if one of the asserts in a step failed,<br/>
then the step will be entered to list of failure steps and next test can be skipped<br/>
if it is depended on the failed step.<br/>
Example:
To make test be skipped if step failed, a custom marker should be created.
This is an example of such custom marker, but user can create its own custom marker.
In conftest.py file:
```python
import pytest
@pytest.fixture(autouse=True)
def run_before_test(request):
markers = request.node.own_markers
for marker in markers:
if marker.name == 'soft_asserts':
marker_params = marker.kwargs
soft_asserts = marker_params['soft_asserts']
skip_steps = marker_params['skip_steps']
for step in skip_steps:
if soft_asserts.is_step_in_failure_steps(step):
pytest.skip(f'Skipped because [{step}] failed.')
```
```python
import pytest
from nrt_pytest_soft_asserts.soft_asserts import SoftAsserts
soft_asserts = SoftAsserts()
STEP_1 = 'step_1'
STEP_2 = 'step_2'
def test_assert_with_steps():
soft_asserts.set_step(STEP_1)
# result is False
result = soft_asserts.assert_true(False)
# print False
print(result)
soft_asserts.set_step(STEP_2)
soft_asserts.assert_true(False)
# From this code section steps will not be attached to failure asserts
soft_asserts.unset_step()
soft_asserts.assert_true(False)
soft_asserts.assert_all()
@pytest.mark.soft_asserts(soft_asserts=soft_asserts, skip_steps=[STEP_1])
def test_skip_if_step_1_fail():
soft_asserts.assert_true(True)
@pytest.mark.soft_asserts(soft_asserts=soft_asserts, skip_steps=[STEP_2])
def test_skip_if_step_2_fail():
soft_asserts.assert_true(True)
```
#### Print error on each failed assert
Each assertion failure can be printed.<br/>
This can be done by adding logger or by adding a print method.<br/>
- In case a logger will be added to soft asserts, then logger.error(message) will be used.
- In case a print method will be added to soft asserts, then print_method(message) will be used.
- logger and print method cannot be added together.
#### Error format
`message [file_path: line_number] code_line`
#### logger example:
```python
import logging
from nrt_pytest_soft_asserts.soft_asserts import SoftAsserts
logger = logging.getLogger('test')
soft_asserts = SoftAsserts()
# logger will be used to print message after each assert fail.
soft_asserts.set_logger(logger)
def test_assert_true_fail():
i = 1
j = 2
# logger.error() will print messages to console for each assert that fails
soft_asserts.assert_true(i + j == 5)
# f'{i} is different from {j}' will be printed by logger.error() after assert will fail
soft_asserts.assert_equal(i, j, f'{i} is different from {j}')
soft_asserts.assert_all()
```
#### print method example:
```python
from nrt_pytest_soft_asserts.soft_asserts import SoftAsserts
def print_method(message):
print(message)
soft_asserts = SoftAsserts()
# print_method will be used to print message after each assert fail.
soft_asserts.set_print_method(print_method)
def test_assert_true_fail():
i = 1
j = 2
# print_method will print messages to console for each assert that fails
soft_asserts.assert_true(i + j == 5)
# f'{i} is different from {j}' will be printed by print_method after assert will fail
soft_asserts.assert_equal(i, j, f'{i} is different from {j}')
soft_asserts.assert_all()
```
Wiki: https://github.com/etuzon/python-nrt-pytest-soft-asserts/wiki
Raw data
{
"_id": null,
"home_page": "https://github.com/etuzon/python-nrt-pytest-soft-asserts",
"name": "nrt-pytest-soft-asserts",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "python, python3, python-3, nrt, pytest, soft, assert, asserts, assertion, assertions, soft-assert, soft-asserts, soft-assertion, soft-assertions, nrt-pytest-soft-asserts",
"author": "Eyal Tuzon",
"author_email": "Eyal Tuzon <eyal.tuzon.dev@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/ed/83/38743a0ebde28cf277e0103105092cc853dcad6c5d57be2cc95a39863ec1/nrt_pytest_soft_asserts-1.1.3.tar.gz",
"platform": null,
"description": "# Pytest soft asserts.\r\n\r\n![PyPI](https://img.shields.io/pypi/v/nrt-pytest-soft-asserts?color=blueviolet&style=plastic)\r\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/nrt-pytest-soft-asserts?color=greens&style=plastic)\r\n![PyPI - License](https://img.shields.io/pypi/l/nrt-pytest-soft-asserts?color=blue&style=plastic)\r\n![PyPI - Downloads](https://img.shields.io/pypi/dd/nrt-pytest-soft-asserts?style=plastic)\r\n![PyPI - Downloads](https://img.shields.io/pypi/dm/nrt-pytest-soft-asserts?color=yellow&style=plastic)\r\n[![Coverage Status](https://coveralls.io/repos/github/etuzon/python-nrt-pytest-soft-asserts/badge.svg)](https://coveralls.io/github/etuzon/pytohn-nrt-pytest-soft-asserts)\r\n![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/etuzon/python-nrt-pytest-soft-asserts?style=plastic)\r\n![GitHub last commit](https://img.shields.io/github/last-commit/etuzon/python-nrt-pytest-soft-asserts?style=plastic)\r\n[![DeepSource](https://app.deepsource.com/gh/etuzon/python-nrt-pytest-soft-asserts.svg/?label=active+issues&token=d3XBT3-sw5yOtGTGWIJMpmT_)](https://app.deepsource.com/gh/etuzon/python-nrt-pytest-soft-asserts/?ref=repository-badge)\r\n\r\n### Supported asserts:\r\n\r\n| Assert | Description | Example | Return |\r\n|-------------------------------------------------------------|-----------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------|-----------------------------------------------------|\r\n| assert_true(condition, message=None) | Verify that condition is True. | soft_asserts.assert_true(a == b) | True if assertion passes, False if assertion fails. |\r\n| assert_false(condition, message=None) | Verify that condition is False. | soft_asserts.assert_false(a == b) | True if assertion passes, False if assertion fails. |\r\n| assert_equal(first, second, message=None) | Verify that first is equal to second. | soft_asserts.assert_equal(a, b) | True if assertion passes, False if assertion fails. |\r\n| assert_not_equal(first, second, message=None) | Verify that first is not equal to second. | soft_asserts.assert_not_equal(a, b) | True if assertion passes, False if assertion fails. |\r\n| assert_is(first, second, message=None) | Verify that first and second are the same object. | soft_asserts.assert_is(a, b) | True if assertion passes, False if assertion fails. |\r\n| assert_is_not(first, second, message=None) | Verify that first and second are not the same object. | soft_asserts.assert_is_not(a, b) | True if assertion passes, False if assertion fails. |\r\n| assert_is_none(obj, message=None) | Verify that obj is None. | soft_asserts.assert_is_none(a) | True if assertion passes, False if assertion fails. |\r\n| assert_is_not_none(obj, message=None) | Verify that obj is not None. | soft_asserts.assert_is_not_none(a) | True if assertion passes, False if assertion fails. |\r\n| assert_in(obj, container, message=None) | Verify that obj is in container. | soft_asserts.assert_in(a, [a, b, c]) | True if assertion passes, False if assertion fails. |\r\n| assert_not_in(obj, container, message=None) | Verify that obj is not in container. | soft_asserts.assert_not_in(a, [b, c]) | True if assertion passes, False if assertion fails. |\r\n| assert_is_instance(obj, cls, message=None) | Verify that obj is instance of cls. | soft_asserts.assert_is_instance(a, A) | True if assertion passes, False if assertion fails. |\r\n| assert_is_not_instance(obj, cls, message=None) | Verify that obj is not instance of cls. | soft_asserts.assert_is_not_instance(a, B) | True if assertion passes, False if assertion fails. |\r\n| assert_almost_equal(first, second, delta, message=None) | Verify that first is almost equal to second,<br/>and the different is equal or less to delta. | soft_asserts.assert_almost_equal(1.001, 1.002, 0.1) | True if assertion passes, False if assertion fails. |\r\n| assert_not_almost_equal(first, second, delta, message=None) | Verify that first is not almost equal to second,<br/>and the different is more than delta. | soft_asserts.assert_not_almost_equal(1.001, 1.002, 0.00001) | True if assertion passes, False if assertion fails. |\r\n| assert_raises(exception, method: Callable, *args, **kwargs) | Verify that method execution raise exception. | soft_asserts.assert_raises(TypeError, sum, 'a', 2) | True if assertion passes, False if assertion fails. |\r\n| assert_raises_with(exception, message=None) | Verify that execution in 'with' block raise exception. | with soft_asserts.assert_raised_with(ValueError):<br/> raise ValueError(ERROR_MESSAGE_1) | |\r\n \r\n\r\nIn the end of each test, the soft asserts will be verified and the test will be marked as failed if any of the asserts failed.<br/>\r\nTo verify the soft asserts in the middle of the test, call `soft_asserts.assert_all()`.<br/>\r\n<br/>\r\nassert_all() will raise _AssertionError_ if any of the asserts failed.<br/>\r\n\r\n#### Steps\r\n\r\nEach testing section can be divided to steps.<br/>\r\nThe meaning of this is that if one of the asserts in a step failed,<br/>\r\nthen the step will be entered to list of failure steps and next test can be skipped<br/>\r\nif it is depended on the failed step.<br/> \r\n\r\nExample:\r\n\r\nTo make test be skipped if step failed, a custom marker should be created.\r\n\r\nThis is an example of such custom marker, but user can create its own custom marker.\r\n\r\nIn conftest.py file:\r\n\r\n```python\r\nimport pytest\r\n\r\n\r\n@pytest.fixture(autouse=True)\r\ndef run_before_test(request):\r\n markers = request.node.own_markers\r\n\r\n for marker in markers:\r\n if marker.name == 'soft_asserts':\r\n marker_params = marker.kwargs\r\n soft_asserts = marker_params['soft_asserts']\r\n skip_steps = marker_params['skip_steps']\r\n\r\n for step in skip_steps:\r\n if soft_asserts.is_step_in_failure_steps(step):\r\n pytest.skip(f'Skipped because [{step}] failed.')\r\n```\r\n\r\n```python\r\nimport pytest\r\nfrom nrt_pytest_soft_asserts.soft_asserts import SoftAsserts\r\n\r\nsoft_asserts = SoftAsserts()\r\n\r\nSTEP_1 = 'step_1'\r\nSTEP_2 = 'step_2'\r\n\r\ndef test_assert_with_steps():\r\n soft_asserts.set_step(STEP_1)\r\n # result is False\r\n result = soft_asserts.assert_true(False)\r\n # print False\r\n print(result) \r\n soft_asserts.set_step(STEP_2) \r\n soft_asserts.assert_true(False)\r\n \r\n # From this code section steps will not be attached to failure asserts\r\n soft_asserts.unset_step()\r\n soft_asserts.assert_true(False)\r\n \r\n soft_asserts.assert_all()\r\n\r\n\r\n@pytest.mark.soft_asserts(soft_asserts=soft_asserts, skip_steps=[STEP_1])\r\ndef test_skip_if_step_1_fail():\r\n soft_asserts.assert_true(True)\r\n\r\n@pytest.mark.soft_asserts(soft_asserts=soft_asserts, skip_steps=[STEP_2])\r\ndef test_skip_if_step_2_fail():\r\n soft_asserts.assert_true(True)\r\n```\r\n\r\n#### Print error on each failed assert\r\n\r\nEach assertion failure can be printed.<br/>\r\nThis can be done by adding logger or by adding a print method.<br/>\r\n\r\n - In case a logger will be added to soft asserts, then logger.error(message) will be used.\r\n - In case a print method will be added to soft asserts, then print_method(message) will be used.\r\n - logger and print method cannot be added together.\r\n\r\n#### Error format\r\n\r\n`message [file_path: line_number] code_line`\r\n\r\n#### logger example:\r\n\r\n```python\r\nimport logging\r\nfrom nrt_pytest_soft_asserts.soft_asserts import SoftAsserts\r\n\r\n\r\nlogger = logging.getLogger('test')\r\n\r\nsoft_asserts = SoftAsserts()\r\n\r\n# logger will be used to print message after each assert fail.\r\nsoft_asserts.set_logger(logger)\r\n\r\n\r\ndef test_assert_true_fail():\r\n i = 1\r\n j = 2\r\n # logger.error() will print messages to console for each assert that fails\r\n soft_asserts.assert_true(i + j == 5)\r\n # f'{i} is different from {j}' will be printed by logger.error() after assert will fail\r\n soft_asserts.assert_equal(i, j, f'{i} is different from {j}')\r\n soft_asserts.assert_all()\r\n```\r\n\r\n#### print method example:\r\n\r\n```python\r\nfrom nrt_pytest_soft_asserts.soft_asserts import SoftAsserts\r\n\r\ndef print_method(message):\r\n print(message)\r\n\r\nsoft_asserts = SoftAsserts()\r\n\r\n# print_method will be used to print message after each assert fail.\r\nsoft_asserts.set_print_method(print_method)\r\n\r\n\r\ndef test_assert_true_fail():\r\n i = 1\r\n j = 2\r\n # print_method will print messages to console for each assert that fails\r\n soft_asserts.assert_true(i + j == 5)\r\n # f'{i} is different from {j}' will be printed by print_method after assert will fail\r\n soft_asserts.assert_equal(i, j, f'{i} is different from {j}')\r\n soft_asserts.assert_all()\r\n```\r\n\r\nWiki: https://github.com/etuzon/python-nrt-pytest-soft-asserts/wiki\r\n",
"bugtrack_url": null,
"license": null,
"summary": "Soft asserts for pytest",
"version": "1.1.3",
"project_urls": {
"Bug Tracker": "https://github.com/etuzon/python-nrt-pytest-soft-asserts/issues",
"Homepage": "https://github.com/etuzon/python-nrt-pytest-soft-asserts",
"documentation": "https://github.com/etuzon/python-nrt-pytest-soft-asserts/wiki"
},
"split_keywords": [
"python",
" python3",
" python-3",
" nrt",
" pytest",
" soft",
" assert",
" asserts",
" assertion",
" assertions",
" soft-assert",
" soft-asserts",
" soft-assertion",
" soft-assertions",
" nrt-pytest-soft-asserts"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6e898895cf2192147f200ae5bb0288e3431deb7133d8d90551b504fe75c60dbc",
"md5": "6dd4eaea1e15a881cdb43697a74639cf",
"sha256": "0430660ccb5b9e96c5d126e36d9f57857b12307a2aaab6c47ddf8085db68117a"
},
"downloads": -1,
"filename": "nrt_pytest_soft_asserts-1.1.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6dd4eaea1e15a881cdb43697a74639cf",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 6879,
"upload_time": "2024-12-16T08:27:28",
"upload_time_iso_8601": "2024-12-16T08:27:28.507272Z",
"url": "https://files.pythonhosted.org/packages/6e/89/8895cf2192147f200ae5bb0288e3431deb7133d8d90551b504fe75c60dbc/nrt_pytest_soft_asserts-1.1.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ed8338743a0ebde28cf277e0103105092cc853dcad6c5d57be2cc95a39863ec1",
"md5": "eb5387d4104dfe370a41b61610dc2165",
"sha256": "0d9f1c12769ab01a8579a7fd89ab1b3b5e4eb255aae3d421d6868b9f4a4f5044"
},
"downloads": -1,
"filename": "nrt_pytest_soft_asserts-1.1.3.tar.gz",
"has_sig": false,
"md5_digest": "eb5387d4104dfe370a41b61610dc2165",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 7566,
"upload_time": "2024-12-16T08:27:32",
"upload_time_iso_8601": "2024-12-16T08:27:32.286195Z",
"url": "https://files.pythonhosted.org/packages/ed/83/38743a0ebde28cf277e0103105092cc853dcad6c5d57be2cc95a39863ec1/nrt_pytest_soft_asserts-1.1.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-16 08:27:32",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "etuzon",
"github_project": "python-nrt-pytest-soft-asserts",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"requirements": [
{
"name": "pytest",
"specs": [
[
">=",
"7.0.1"
]
]
},
{
"name": "zipp",
"specs": [
[
">=",
"3.19.1"
]
]
}
],
"lcname": "nrt-pytest-soft-asserts"
}