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

Add insert_pytest_raises()

Open zmievsa opened this issue 2 years ago • 3 comments

Every time I used insert_assert I was ecstatic with my new productivity but something was missing: it was always painful to catch and match exceptions. Only recently have I realized that same tooling and approach could be used to solve that problem as well.

Note that this is only a working prototype without tests or proper structure/naming.

If you like the idea -- I'll be happy to provide the rest :)

zmievsa avatar Jul 09 '23 13:07 zmievsa

Codecov Report

Merging #131 (0a8ac24) into main (1172e81) will increase coverage by 0.85%. Report is 8 commits behind head on main. The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #131      +/-   ##
==========================================
+ Coverage   96.27%   97.12%   +0.85%     
==========================================
  Files           8        8              
  Lines         725      766      +41     
  Branches      110      121      +11     
==========================================
+ Hits          698      744      +46     
+ Misses         21       17       -4     
+ Partials        6        5       -1     
Files Changed Coverage Δ
devtools/pytest_plugin.py 92.52% <100.00%> (+4.38%) :arrow_up:

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 1172e81...0a8ac24. Read the comment docs.

codecov[bot] avatar Jul 09 '23 13:07 codecov[bot]

@samuelcolvin

The mechanism is exactly the same as in insert_assert, except that it's a context manager that catches the error and then replaces itself with a pytest.raises that captures the main error details. re.escape is used because pytest.raises checks using regexes by default so we avoid "invalid regex" errors using re.escape. I have been using it ever since I made this pull request and loved every second of it: it amazingly complements insert_assert. I'd like to write sections on it and on insert_assert in README/docs of this repo as I think that more people should know about these tools. But let's do it in a separate PR.

Demonstration

Before running the test: image After running the test: image

I am not a huge fan of my naming but I can't come up with a better name for this function. I am happy that you like the idea. I'll write the tests and comments now so that it's closer to being ready to be merged.

Update

Added tests. Mostly just copying the insert_assert tests

zmievsa avatar Aug 17 '23 14:08 zmievsa

@samuelcolvin A gentle reminder to review this feature whenever you have a minute

zmievsa avatar Sep 03 '23 17:09 zmievsa