dirty-equals
dirty-equals copied to clipboard
adding `insert_assert` function and make pytest-plugin
See https://gist.github.com/samuelcolvin/625a1655c2a73e02469fc3c27285ca42
To use now:
install:
pip install git+https://github.com/samuelcolvin/dirty-equals.git@insert_assert
With that you should be able to add insert_assert()
into your tests and it should "just work ™",
Example code:
def test_string():
thing = 'foobar'
insert_assert(thing)
def test_list_callable():
def foobar():
return ['foo', 1, b'bytes']
insert_assert(foobar())
def test_comprehension():
insert_assert([f'x{i}' for i in range(10)])
Still to do:
- [x] tests, or at least ignore where impossible
- [ ] docs
Codecov Report
Merging #51 (6a70b31) into main (724494c) will decrease coverage by
1.70%
. The diff coverage is90.85%
.
Additional details and impacted files
@@ Coverage Diff @@
## main #51 +/- ##
==========================================
- Coverage 99.72% 98.02% -1.71%
==========================================
Files 11 12 +1
Lines 737 911 +174
Branches 188 226 +38
==========================================
+ Hits 735 893 +158
- Misses 2 15 +13
- Partials 0 3 +3
Impacted Files | Coverage Δ | |
---|---|---|
dirty_equals/pytest_plugin.py | 90.80% <90.80%> (ø) |
|
dirty_equals/version.py | 100.00% <100.00%> (ø) |
Continue to review full report at Codecov.
Legend - Click here to learn more
Δ = absolute <relative> (impact)
,ø = not affected
,? = missing data
Powered by Codecov. Last update 724494c...6a70b31. Read the comment docs.
I think it would be really interesting to enable something like this:
# insert_assert(
# resp.json(),
# repl={
# '[]["id"]': IsUUID(),
# '[0]["created_at"]': IsDatetime(
# approx=datetime.utcnow(),
# delta=timedelta(seconds=1),
# )
# }
# )
expected = [
{
"id": IsUUID(),
"created_at": IsDatetime(
approx=datetime.utcnow(),
delta=timedelta(seconds=1),
),
"price": 123.0,
}
]
assert resp.json() == expected
(source: https://twitter.com/adriangb01/status/1575196309891719168)
On reflection, I think I'll move this to https://github.com/samuelcolvin/python-devtools.
Rationale:
- this fix better with what devtools is for
- shared requirements and even some shared logic
- the recommendation is not to install devtools in CI, thus the
insert_assert
could never pass on CI
replaced by https://github.com/samuelcolvin/python-devtools/pull/126, would be nice to try to add support for dirty-equals types (even though this has moved to another repo), in particular:
- stuff where the repr breaks black, the formatting vanishes which is ugly.
- stuff like datetimes close to now and uuids which will change often.