-
-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Idea: context manager API #9
Comments
This is brilliant. I hope it works |
0.3.3 added a with check(): assert a < 1
with check(): assert 1 < a < 10 |
Also, kinda really stoked that you took a look at the plugin. |
Heh thanks. Not wanting to nitpick but I had hoped for a singleton-ish api (no function call, as it seems unnecessary). I'll make a PR to fix it what do you think? |
Another thing that I noticed - only AssertionErrors are collected - is that intentional? |
I like the Singleton idea. Wasn’t sure how to accomplish it. So please, PR |
Only catching assertions. Ya. It was intentional. But maybe not in line with normal pytest behavior of all exceptions causing failure. Do you have an opinion? |
So now that I think of it more, I'd prolly woulnd't like seeing dozen undefined variable failures, so it's fine as it is now. |
- Support nested `subtests.test()` contexts similar to `unittest.TestCase.subTest()` chaining of argument sets. - Make `subtests` instance a re-entrable context manager similar to `subtests.test()`. - Make `subtests` instance a callable that sets context (scope) for further subtests or for nested scopes only if return value is used as a context manager. - Add `check` concise alias for the `subtests` fixture. Essentially, the goal is to adapt `pytest-subtests` for checking of multiple aspects of compound objects, e.g. a class fields, withing a single unit test and to get report with all failures at once instead of the first error only. from datetime import datetime def test_fields(check): dt = datetime.utcfromtimestamp(1234567890) with check: assert dt.year == 2009, "Year is OK" with check: assert dt.month == 1 with check: assert dt.day == 13, "Day is OK" with check: assert dt.hour == 27 Use with `--tb=short` option. The bright idea of context manager shortcut belongs to @ionelmc: okken/pytest-check#9
So one thing in pytest-check that is reminiscent of unittest and nose is the assertion api. While it should work fine with pytest's assertion rewriter you still need to remember what the method name is. Is it
check.less
orcheck.less_than
? Is itcheck.equal
orcheck.is_equal
? You can never tell.So an alternative:
Context managers can "silence" exceptions by returning True in
__exit__
.The text was updated successfully, but these errors were encountered: