1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
# pytest-check
A pytest plugin that allows multiple failures per test.
----
Normally, a test function will fail and stop running with the first failed `assert`.
That's totally fine for tons of kinds of software tests.
However, there are times where you'd like to check more than one thing, and you'd really like to know the results of each check, even if one of them fails.
`pytest-check` allows multiple failed "checks" per test function, so you can see the whole picture of what's going wrong.
## Installation
From PyPI:
```
$ pip install pytest-check
```
From conda (conda-forge):
```
$ conda install -c conda-forge pytest-check
```
## Example
Quick example of where you might want multiple checks:
```python
import httpx
from pytest_check import check
def test_httpx_get():
r = httpx.get('https://www.example.org/')
# bail if bad status code
assert r.status_code == 200
# but if we get to here
# then check everything else without stopping
with check:
assert r.is_redirect is False
with check:
assert r.encoding == 'utf-8'
with check:
assert 'Example Domain' in r.text
```
## Import vs fixture
The example above used import: `from pytest_check import check`.
You can also grab `check` as a fixture with no import:
```python
def test_httpx_get(check):
r = httpx.get('https://www.example.org/')
...
with check:
assert r.is_redirect == False
...
```
## Validation functions
`check` also helper functions for common checks.
These methods do NOT need to be inside of a `with check:` block.
| Function | Meaning | Notes |
|------------------------------------------------------|-----------------------------------|------------------------------------------------------------------------------------------------------|
| `equal(a, b, msg="")` | `a == b` | |
| `not_equal(a, b, msg="")` | `a != b` | |
| `is_(a, b, msg="")` | `a is b` | |
| `is_not(a, b, msg="")` | `a is not b` | |
| `is_true(x, msg="")` | `bool(x) is True` | |
| `is_false(x, msg="")` | `bool(x) is False` | |
| `is_none(x, msg="")` | `x is None` | |
| `is_not_none(x, msg="")` | `x is not None` | |
| `is_in(a, b, msg="")` | `a in b` | |
| `is_not_in(a, b, msg="")` | `a not in b` | |
| `is_instance(a, b, msg="")` | `isinstance(a, b)` | |
| `is_not_instance(a, b, msg="")` | `not isinstance(a, b)` | |
| `is_nan(x, msg="")` | `math.isnan(x)` | [math.isnan](https://docs.python.org/3/library/math.html#math.isnan) |
| `is_not_nan(x, msg="")` | `not math.isnan(x) ` | [math.isnan](https://docs.python.org/3/library/math.html#math.isnan) |
| `almost_equal(a, b, rel=None, abs=None, msg="")` | `a == pytest.approx(b, rel, abs)` | [pytest.approx](https://docs.pytest.org/en/latest/reference.html#pytest-approx) |
| `not_almost_equal(a, b, rel=None, abs=None, msg="")` | `a != pytest.approx(b, rel, abs)` | [pytest.approx](https://docs.pytest.org/en/latest/reference.html#pytest-approx) |
| `greater(a, b, msg="")` | `a > b` | |
| `greater_equal(a, b, msg="")` | `a >= b` | |
| `less(a, b, msg="")` | `a < b` | |
| `less_equal(a, b, msg="")` | `a <= b` | |
| `between(b, a, c, msg="", ge=False, le=False)` | `a < b < c` | |
| `between_equal(b, a, c, msg="")` | `a <= b <= c` | same as `between(b, a, c, msg, ge=True, le=True)` |
| `raises(expected_exception, *args, **kwargs)` | *Raises given exception* | similar to [pytest.raises](https://docs.pytest.org/en/latest/reference/reference.html#pytest-raises) |
| `fail(msg)` | *Log a failure* | |
**Note: This is a list of relatively common logic operators. I'm reluctant to add to the list too much, as it's easy to add your own.**
The httpx example can be rewritten with helper functions:
```python
def test_httpx_get_with_helpers():
r = httpx.get('https://www.example.org/')
assert r.status_code == 200
check.is_false(r.is_redirect)
check.equal(r.encoding, 'utf-8')
check.is_in('Example Domain', r.text)
```
Which you use is personal preference.
## Defining your own check functions
### Using `@check.check_func`
The `@check.check_func` decorator allows you to wrap any test helper that has an assert statement in it to be a non-blocking assert function.
```python
from pytest_check import check
@check.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)
```
### Built in check functions return bool
The return value of all the check functions that come pre-written in pytest-check return a bool value.
You can use that to determine if it passes or fails.
So, if you want to perform some action based on success/failure, you can just do something like:
```python
from pytest_check import check
def test_something()
...
if check.equal(a, b):
# they are equal
...
else
# they are not equal
# and a failure was registered by the check method
...
```
### Using `check.fail()`
Using `@check.check_func` is probably the easiest.
However, it does have a bit of overhead in the passing cases
that can affect large loops of checks.
If you need a bit of a speedup, use the following style with the help of `check.fail()`.
```python
from pytest_check import check
def is_four(a):
__tracebackhide__ = True
if a == 4:
return True
else:
check.fail(f"check {a} == 4")
return False
def test_all_four():
is_four(1)
is_four(2)
is_four(3)
is_four(4)
```
## Using raises as a context manager
`raises` is used as context manager, much like `pytest.raises`. The main difference being that a failure to raise the right exception won't stop the execution of the test method.
```python
from pytest_check import check
def test_raises():
with check.raises(AssertionError):
x = 3
assert 1 < x < 4
def test_raises_exception_value():
with check.raises(ValueError) as e:
raise ValueError("This is a ValueError")
check.equal(str(e.value) == "This is a ValueError")
```
If the exception isn't correct, as in it isn't the exception type raised, the error message reported for the test failure will describe the actual exception.
```python
def test_raises_fail():
with check.raises(ValueError):
x = 1 / 0 # division by zero error, NOT ValueError
assert x == 0
```
If you want a custom message instead, you can supply one.
Note, this doesn't check that msg matches the exception string.
It simply is a custom failure message for your test.
```python
def test_raises_and_custom_fail_message():
with check.raises(ValueError, msg="custom"):
x = 1 / 0 # division by zero error, NOT ValueError
assert x == 0
```
## Pseudo-tracebacks
With `check`, tests can have multiple failures per test.
This would possibly make for extensive output if we include the full traceback for
every failure.
To make the output a little more concise, `pytest-check` implements a shorter version, which we call pseudo-tracebacks.
And to further make the output more concise, and speed up the test run, only the first pseudo-traceback is turned on by default.
For example, take this test:
```python
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")
```
This will result in:
```
$ pytest test_check.py
...
================================= FAILURES =================================
_______________________________ test_example _______________________________
FAILURE: check 1 > 2
test_check.py:7 in test_example() -> check.greater(a, b)
FAILURE: check 2 <= 1
FAILURE: check 1 in [2, 4, 6]: Is 1 in the list
FAILURE: check 2 not in [2, 4, 6]: make sure 2 isn't in list
------------------------------------------------------------
Failed Checks: 4
========================= short test summary info ==========================
FAILED test_check.py::test_example - check 1 > 2
============================ 1 failed in 0.01s =============================
```
If you wish to see more pseudo-tracebacks than just the first, you can set `--check-max-tb=5` or something larger:
```
(.venv) $ pytest test_check.py --check-max-tb=5
=========================== test session starts ============================
collected 1 item
test_check.py F [100%]
================================= FAILURES =================================
_______________________________ test_example _______________________________
FAILURE: check 1 > 2
test_check.py:7 in test_example() -> check.greater(a, b)
FAILURE: check 2 <= 1
test_check.py:8 in test_example() -> check.less_equal(b, a)
FAILURE: check 1 in [2, 4, 6]: Is 1 in the list
test_check.py:9 in test_example() -> check.is_in(a, c, "Is 1 in the list")
FAILURE: check 2 not in [2, 4, 6]: make sure 2 isn't in list
test_check.py:10 in test_example() -> check.is_not_in(b, c, "make sure 2 isn't in list")
------------------------------------------------------------
Failed Checks: 4
========================= short test summary info ==========================
FAILED test_check.py::test_example - check 1 > 2
============================ 1 failed in 0.01s =============================
```
## Red output
The failures will also be red, unless you turn that off with pytests `--color=no`.
## No output
You can turn off the failure reports with pytests `--tb=no`.
## Stop on Fail (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.
## any_failures()
Use `any_failures()` to see if there are any failures.
One use case is to make a block of checks conditional on not failing in a previous set of checks:
```python
from pytest_check import check
def test_with_groups_of_checks():
# always check these
check.equal(1, 1)
check.equal(2, 3)
if not check.any_failures():
# only check these if the above passed
check.equal(1, 2)
check.equal(2, 2)
```
## Speedups
If you have lots of check failures, your tests may not run as fast as you want.
There are a few ways to speed things up.
* `--check-max-tb=5` - Only first 5 failures per test will include pseudo-tracebacks (rest without them).
* The example shows `5` but any number can be used.
* pytest-check uses custom traceback code I'm calling a pseudo-traceback.
* This is visually shorter than normal assert tracebacks.
* Internally, it uses introspection, which can be slow.
* Allowing a limited number of pseudo-tracebacks speeds things up quite a bit.
* Default is 1.
* Set a large number, e.g: 1000, if you want pseudo-tracebacks for all failures
* `--check-max-report=10` - limit reported failures per test.
* The example shows `10` but any number can be used.
* The test will still have the total nuber of failures reported.
* Default is no maximum.
* `--check-max-fail=20` - Stop the test after this many check failures.
* This is useful if your code under test is slow-ish and you want to bail early.
* Default is no maximum.
* Any of these can be used on their own, or combined.
* Recommendation:
* Leave the default, equivelant to `--check-max-tb=1`.
* If excessive output is annoying, set `--check-max-report=10` or some tolerable number.
## Local speedups
The flags above are global settings, and apply to every test in the test run.
Locally, you can set these values per test.
From `examples/test_example_speedup_funcs.py`:
```python
def test_max_tb():
check.set_max_tb(2)
for i in range(1, 11):
check.equal(i, 100)
def test_max_report():
check.set_max_report(5)
for i in range(1, 11):
check.equal(i, 100)
def test_max_fail():
check.set_max_fail(5)
for i in range(1, 11):
check.equal(i, 100)
```
## Call on fail
If you want to have a custom action happen for every check failure,
you can use the method `call_on_fail(func)`.
You have to pass it a function that accepts a string.
That function will be called with the message from the check failure.
Example:
```python
from pytest_check import check
def my_func(msg):
...
check.call_on_fail(my_func)
...
```
There are other uses for this, but the original use case idea was for logging to a file.
### Logging to a file
The `examples/logging_to_a_file/` directory has an example of how you could set this up.
In a `conftest.py` file, we:
* Configure a logging logger to write to a file.
* Create a small `log_failure(message)` function that uses that logger
* Call `check.call_on_fail(log_failure)` to register the function.
And that's it, all failures will get logged to a file.
```python
import logging
import pytest
from pytest_check import check
@pytest.fixture(scope='session', autouse=True)
def setup_logging():
# logging config
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
fh = logging.FileHandler('session.log')
fh.setLevel(logging.DEBUG)
fh.setFormatter(logging.Formatter('--- %(asctime)s.%(msecs)03d ---\n%(message)s',
datefmt='%Y-%m-%d %H:%M:%S'))
log.addHandler(fh)
# log start of tests
log.info("---------\nStarting test run\n---------")
# have check failures log to file
def log_failure(message):
log.error(message)
check.call_on_fail(log_failure)
```
With that setup, the file will end up looking something like this:
```
$ cat session.log
--- 2026-02-26 09:46:39.822 ---
---------
Starting test run
---------
--- 2026-02-26 09:46:39.831 ---
FAILURE: check 1 == 2
test_log2.py:4 in test_one() -> check.equal(1, 2)
--- 2026-02-26 09:46:39.832 ---
FAILURE: check 5 == 6
test_log2.py:7 in test_two() -> check.equal(5, 6)
```
Of course, you can modify the logging config to make it look however you want.
## Contributing
Contributions are very welcome. Tests can be run with [tox](https://tox.readthedocs.io/en/latest/).
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](http://opensource.org/licenses/MIT) license, "pytest-check" is free and open source software
## Issues
If you encounter any problems, please [file an issue](https://github.com/okken/pytest-check/issues) along with a detailed description.
## Changelog
See [changelog.md](https://github.com/okken/pytest-check/blob/main/changelog.md)
|