Skip to content

Commit b21fa57

Browse files
committed
Add pytest_assertrepr_compare hook to show detailed assert failures
1 parent 8d1fbd2 commit b21fa57

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ typing:
1717

1818
.PHONY: test
1919
test:
20-
PYTHONPATH=.:$$PYTHONPATH pytest --cov=email_validator -k "not network"
20+
PYTHONPATH=.:$$PYTHONPATH pytest --cov=email_validator -k "not network" -vv
2121

2222
.PHONY: testcov
2323
testcov: test

‎tests/conftest.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from email_validator import ValidatedEmail
2+
3+
4+
def pytest_assertrepr_compare(config, op, left, right):
5+
if isinstance(left, ValidatedEmail) and isinstance(right, ValidatedEmail) and op == "==":
6+
leftDict = left.as_dict()
7+
rightDict = right.as_dict()
8+
keys = set(leftDict) | set(rightDict)
9+
table = [ [key, leftDict.get(key), rightDict.get(key)]
10+
for key in keys ]
11+
maxLen = [max(len(repr(row[col])) for row in table) for col in (0, 1, 2)]
12+
return [repr(left) + " != " + repr(right)] \
13+
+ [(t[0].rjust(maxLen[0])
14+
+ " " + repr(t[1]).ljust(maxLen[1])
15+
+ (" ==" if t[1] == t[2] else " !=")
16+
+ " " + repr(t[2]).ljust(maxLen[2]))
17+
for t in table]

0 commit comments

Comments
 (0)