For python exist pytest
module with great assertion messages. For example instead self.assert_equals(a, b)
it allow write assert a == b
with same informative messages:
standard unittest
assertion for simple variable:
a = 1
b = 2
> self.assertEqual(a, b)
E AssertionError: 1 != 2
pytest
assertion for simple variable:
a = 1
b = 2
> assert a == b
E AssertionError: assert 1 == 2
If I run assert a == b
without pytest
it just show assertion error without variable information and this behaviour the same for rust. Rust provide assert!
and assert_eq!
macro, but not assert_ne!
, assert_gt!
, assert_ge!
, assert_lt!
, assert_le!
, however anybody can implement it himself, but I think that more readable use assert!(a == b)
, assert!(a != b)
, assert!(a > b)
, assert!(a >= b)
, assert!(a < b)
, assert(a <= b)
instead.
As I understood rust macro not allow detect operators ==
, !=
, >=
, >
, <
, <=
to handle this implementation.
Also messages:
#[test]
fn assert_eq_test() {
let a = 1i64;
let b = 2i64;
assert_eq!(a, b);
}
---- assert_eq_test stdout ----
task 'assert_eq_test' failed at 'assertion failed: `(left == right) && (right == left)` (left: `1`, right: `2`)', /proj/paht/tests/test_file.rs:103
And:
#[test]
fn assert_test() {
let a = 1i64;
let b = 2i64;
assert!(a == b);
}
---- assert_test stdout ----
task 'assert_test' failed at 'assertion failed: a == b', /proj/path/tests/test_bbox.rs:95
I think it also will be more readable write what line got assertion code part, for example:
---- assert_eq_test stdout ----
task 'assert_eq_test' failed at 'assertion failed' /proj/paht/tests/test_file.rs:103:
> assert_eq!(a, b);
E assertion failed: 1 == 2
But as I uderstood it also can’t be implemented with macro now.
What do you think about this?