3 ms·
Is there a reason you need specialized assertions over something like Python's generic assert? >>> big = 2; small = 3 >>> assert big >= small, "big is
by aftbit 2y ago
Is there a reason you need specialized assertions over something like Python's generic assert?
>>> big = 2; small = 3
>>> assert big >= small, "big is smaller than small"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError: big is smaller than small
In pytest, assertions are rewritten[1] to return something more useful like:
def test():
big = 2
small = 3
> assert big > small
E assert 2 > 3
test/test_dumb.py:4: AssertionError
========== short test summary info ==========
FAILED test/test_dumb.py::test - assert 2 > 3
1: https://github.com/pytest-dev/pytest/blob/f373974707f57a0b28d12563e4d03c7cd54c70d9/src/_pytest/assertion/rewrite.py#L610 https://github.com/pytest-dev/pytest/blob/f373974707f57a0b28...
- Arnavion 2y agoRust has a generic assert too `assert!(foo >= bar);`. I assume (haven't used the crate myself) the advantage of `assertable::assert_ge!(foo, bar)` is that it prints the values of foo and bar in the assert message. The `assert_eq!(foo, bar)` and `assert_ne!(foo, bar)` macros provided by Rust libstd also do this. But the generic `assert!()` just sees the boolean result of its expression and only prints that in its message. The values of the variables can be included in the generic macro's message via a custom message format, like `assert!(foo >= bar, "foo = {foo}, bar = {bar}");` but having the macro do it by default is convenient. There is an old discussion to have the `assert!()` macro parse its expression to figure out what variables are there and print them out by default, but it's still WIP. ( https://github.com/rust-lang/rfcs/blob/master/text/2011-generic-assert.md https://github.com/rust-lang/rfcs/blob/master/text/2011-gene... https://github.com/rust-lang/rust/issues/44838 https://github.com/rust-lang/rust/issues/44838 )
- o11c 2y agoWhat's more, pytest errs on the side of "just capture more" and In my experience it's quite useful: ============================= test session starts ============================== platform linux -- Python 3.11.2, pytest-7.2.1, pluggy-1.0.0+repack rootdir: /tmp/dl/py collected 1 item test_bigsmall.py F [100%] =================================== FAILURES =================================== ________________________________ test_something ________________________________ def test_something(): > assert get_big_number() > get_small_number() E assert 0 > 1 E + where 0 = get_big_number() E + and 1 = get_small_number() test_bigsmall.py:8: AssertionError =========================== short test summary info ============================ FAILED test_bigsmall.py::test_something - assert 0 > 1 ============================== 1 failed in 0.06s ===============================