Skip to content
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

Closed
ionelmc opened this issue Feb 23, 2019 · 8 comments
Closed

Idea: context manager API #9

ionelmc opened this issue Feb 23, 2019 · 8 comments

Comments

@ionelmc
Copy link
Contributor

ionelmc commented Feb 23, 2019

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 or check.less_than? Is it check.equal or check.is_equal? You can never tell.

So an alternative:

with check: assert a < 1
with check: assert 1 < a < 10

Context managers can "silence" exceptions by returning True in __exit__.

@okken
Copy link
Owner

okken commented Feb 23, 2019

This is brilliant. I hope it works

@okken
Copy link
Owner

okken commented Feb 23, 2019

0.3.3 added a check() context manager.
So this now works:

with check(): assert a < 1
with check(): assert 1 < a < 10

@okken okken closed this as completed Feb 23, 2019
@okken
Copy link
Owner

okken commented Feb 23, 2019

Also, kinda really stoked that you took a look at the plugin.
I've learned a lot from you over the past few years.

@ionelmc
Copy link
Contributor Author

ionelmc commented Feb 23, 2019

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?

@ionelmc
Copy link
Contributor Author

ionelmc commented Feb 23, 2019

Another thing that I noticed - only AssertionErrors are collected - is that intentional?

@okken
Copy link
Owner

okken commented Feb 23, 2019

I like the Singleton idea. Wasn’t sure how to accomplish it. So please, PR

@okken
Copy link
Owner

okken commented Feb 23, 2019

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?

@ionelmc
Copy link
Contributor Author

ionelmc commented Feb 24, 2019

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.

maxnikulin added a commit to maxnikulin/pytest-subtests that referenced this issue Jun 18, 2020
- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants