python-devtools icon indicating copy to clipboard operation
python-devtools copied to clipboard

`insert_assert`

Open samuelcolvin opened this issue 3 years ago • 5 comments

we should move https://github.com/samuelcolvin/dirty-equals/pull/51 to this package.

Function should be debug.insert_assert, but we still need the library to become a pytest plugin so tests with insert_assert can fail.

samuelcolvin avatar Nov 25 '22 10:11 samuelcolvin

Hey, I tried insert_assert and loved it! I would like and could implement a nice feature but would be interested whether you'd like to merge it:

Add a version (or a configuration) of insert_assert that captures values from locals(), compares their values to parts of the object to be asserted (i.e. if it's a dict, then we'll check the keys and values recursively), and inserts variable names instead of raw values. It is very useful when the value being checked is dynamic such as a date or a uuid.

Essentially using aliases in place of literals whenever possible.

zmievsa avatar Jun 05 '23 18:06 zmievsa

I think this is often an anti pattern in testing as it can make the test less declarative, I'd rather avoid it.

If you want to implement it, happy to review a PR, but it should be behind a flag.

samuelcolvin avatar Jun 05 '23 22:06 samuelcolvin

Is this issue still open as a reminder to add insert_assert to the documentation?

alexmojaki avatar Sep 10 '23 14:09 alexmojaki

I guess so.

zmievsa avatar Sep 10 '23 15:09 zmievsa

@zmievsa I'm hoping to add something like what you're describing to a similar library here: https://github.com/15r10nk/inline-snapshot/pull/53

It won't automatically generate code using variables, but if it sees variables or other expressions inside an existing snapshot, it'll try to preserve them.

alexmojaki avatar Feb 25 '24 20:02 alexmojaki