A pytest plugin that allows multiple failures per test.
Project description
pytest-check
A pytest plugin that allows multiple failures per test.
This pytest plugin was a rewrite and a rename of pytest-expect.
Installation
Note: Requires pytest 6.0 or above.
From PyPI:
$ pip install pytest-check
Or from github.
$ pip install git+https://github.com/okken/pytest-check
Usage
Example using import:
import pytest_check as check
def test_example():
a = 1
b = 2
c = [2, 4, 6]
check.greater(a, b)
check.less_equal(b, a)
check.is_in(a, c, "Is 1 in the list")
check.is_not_in(b, c, "make sure 2 isn't in list")
Test results:
=================================== FAILURES ===================================
_________________________________ test_example _________________________________
FAILURE:
assert 1 > 2
test_check.py, line 14, in test_example() -> check.greater(a, b)
FAILURE:
assert 2 <= 1
test_check.py, line 15, in test_example() -> check.less_equal(b, a)
FAILURE: Is 1 in the list
assert 1 in [2, 4, 6]
test_check.py, line 16, in test_example() -> check.is_in(a, c, "Is 1 in the list")
FAILURE: make sure 2 isn't in list
assert 2 not in [2, 4, 6]
test_check.py, line 17, in test_example() -> check.is_not_in(b, c, "make sure 2 isn't in list")
------------------------------------------------------------
Failed Checks: 4
=========================== 1 failed in 0.11 seconds ===========================
Example using fixture:
def test_example(check):
a = 1
b = 2
c = [2, 4, 6]
check.greater(a, b)
check.less_equal(b, a)
check.is_in(a, c, "Is 1 in the list")
check.is_not_in(b, c, "make sure 2 isn't in list")
validation functions
- check.equal - a == b
- check.not_equal - a != b
- check.is_ - a is b
- check.is_not - a is not b
- check.is_true - bool(x) is True
- check.is_false - bool(x) is False
- check.is_none - x is None
- check.is_not_none - x is not None
- check.is_in - a in b
- check.is_not_in - a not in b
- check.is_instance - isinstance(a, b)
- check.is_not_instance - not isinstance(a, b)
- check.almost_equal - a == pytest.approx(b, rel, abs) see at: pytest.approx
- check.not_almost_equal - a != pytest.approx(b, rel, abs) see at: pytest.approx
- check.greater - a > b
- check.greater_equal - a >= b
- check.less - a < b
- check.less_equal - a <= b
- check.raises - func raises given exception similar to pytest.raises
Defining your own check functions
The @check_func
decorator allows you to wrap any test helper that has an assert
statement in it to be a non-blocking assert function.
from pytest_check import check_func
@check_func
def is_four(a):
assert a == 4
def test_all_four():
is_four(1)
is_four(2)
is_four(3)
is_four(4)
The above will result in:
...
________________________________ test_all_four _________________________________
FAILURE: assert 1 == 4
test_fail.py, line 8, in test_all_four() -> is_four(1)
FAILURE: assert 2 == 4
test_fail.py, line 9, in test_all_four() -> is_four(2)
FAILURE: assert 3 == 4
test_fail.py, line 10, in test_all_four() -> is_four(3)
------------------------------------------------------------
Failed Checks: 3
=========================== 1 failed in 0.12 seconds ===========================
Using check as a context manager
You can use the check()
context manager to wrap any assert that you want to continue after in a test.
from pytest_check import check
def test_context_manager():
with check:
x = 3
assert 1 < x < 4
Within any with check:
, however, you still won't get past the assert statement,
so you will need to use multiple with check:
blocks for multiple asserts:
def test_multiple_failures():
with check: assert 1 == 0
with check: assert 1 > 2
with check: assert 1 < 5 < 4
Using raises as a context manager
raises
can also be used as a context manager:
from pytest_check import raises
def test_context_manager():
with raises(AssertionError):
x = 3
assert 1 < x < 4
Just like with check
as a context manager, execution won't proceed past the first line that throws an error, even if it is successfully captured and logged by Pytest Check.
Break your assertions over multiple uses of raises
if you encounter this problem.
maxfail behavior
Setting -x
or --maxfail=1
will cause this plugin to abort testing after the first failed check.
Setting -maxfail=2
or greater will turn off any handling of maxfail within this plugin and the behavior is controlled by pytest.
In other words, the maxfail
count is counting tests, not checks.
The exception is the case of 1
, where we want to stop on the very first failed check.
Contributing
Contributions are very welcome. Tests can be run with tox. Test coverage is now 100%. Please make sure to keep it at 100%. If you have an awesome pull request and need help with getting coverage back up, let me know.
License
Distributed under the terms of the MIT license, "pytest-check" is free and open source software
Issues
If you encounter any problems, please file an issue along with a detailed description.
Changelog
- 1.0.6
- Update pyproject.toml to use newer flit
- Attempt to fix plugin name from pytest_check to pytest-check.
- 1.0.5
- Add
raises
- Add
- 1.0.4
- Require Python >= 3.6
- Remove old Manifest.in file.
- 1.0.3
- Fix #64, modifications to maxfail behavior.
- 1.0.2
- Add
excinfo
to call object inpytest_runtest_makereport()
. - Intended to help with some report systems.
- Add
- 1.0.1
- Remove Beta Classifier
- Status is now "Development Status :: 5 - Production/Stable"
- 1.0.0
- Jump to 1.0 version, API is fairly stable.
- 0.4.1
- Fix #43/#44 tests with failing checks and failing asserts now report all issues.
- 0.4.0
- added
is_()
andis_not()
- Requires pytest 6.0 or above. (Removed some cruft to support pytest 5.x)
- added
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
File details
Details for the file pytest-check-1.0.6.tar.gz
.
File metadata
- Download URL: pytest-check-1.0.6.tar.gz
- Upload date:
- Size: 12.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.27.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 84e22f670a69e8622ee4c04332ac18aff4d4e8624d7b4cb3c916fe8b2516f4dc |
|
MD5 | 59a03dc30dd8c4397d02d4d285e3eb2b |
|
BLAKE2b-256 | b60a6b99cd062307bac929ef9bc22355e8a5664dc65dcf8cd95f81b28c167cf8 |
File details
Details for the file pytest_check-1.0.6-py3-none-any.whl
.
File metadata
- Download URL: pytest_check-1.0.6-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.27.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 061b60393365c80d73e4d060eba742a675ce88ff88f759be2539d27d5de04e91 |
|
MD5 | 13d4dc963f26b8ae68ff13898a4cb3e4 |
|
BLAKE2b-256 | 7d4a4c1b5d100a111a1ecfef99983b42f5e653b678baa8374f135fed5fbd7c51 |