betamax icon indicating copy to clipboard operation
betamax copied to clipboard

Issue with betamax not recording requests within unconsumed generators

Open guyzmo opened this issue 7 years ago • 6 comments

cf sigmavirus24/github3.py#679

tl;dr

I have setup tests with helpers such as:

class TestTheThing:
    def helper_method(self, arg1, arg2, arg3):
        with self.recorder.use_cassette("cassette_name"):
            thing_to_test = self.thing_containing_what_to_test(arg1)
            return self.thing_to_test.iter_items(arg2, arg3)

    def test_with_some_options(self):
        contents = self.helper_method("a", "b", "c")
        assert list(contents) == ['foobar']

in the example above, I'm returning a generator from the helper method. So when in the test I'm transforming it into a list, the iterator runs the generator AFTER the with statement has been exited.

The fix is simply to run the generator within the with block context.

possible actions

  • keep it as is for future readers
  • make it as part of a "stupid mistake" FAQ?
  • add some parameter to betamax to help debug that class of mistakes faster
--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/41524048-issue-with-betamax-not-recording-requests-within-unconsumed-generators?utm_campaign=plugin&utm_content=tracker%2F198445&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F198445&utm_medium=issues&utm_source=github).

guyzmo avatar Jan 31 '17 20:01 guyzmo

add some parameter to betamax to help debug that class of mistakes faster

So the problem is that Betamax replaces the original cassettesadapters. There's no parameter we can provide that would warn someone that they're making a call to a generator or anything like that. We're just not that smart.

make it as part of a "stupid mistake" FAQ?

I wouldn't call it "stupid mistake". I'd label it "common missteps".

keep it as is for future readers

I'm not sure what "it" is here.

sigmavirus24 avatar Feb 01 '17 11:02 sigmavirus24

So the problem is that Betamax replaces the original cassettes. There's no parameter we can provide that would warn someone that they're making a call to a generator or anything like that. We're just not that smart.

Of course not, but I could imagine that a global config option, that adds verbosity to betamax so that within a test context, more output could be thrown at the user:

  • when betamax is hijacking the request (a log point in enter/exit)
  • when the user is doing a call to send a request outside of betamax context make a log point

maybe would it be possible to catch -vv value of py.test and do that at a given increased verbose level?

I'm not sure what "it" is here.

it is just as an issue in the tracker.

guyzmo avatar Feb 01 '17 12:02 guyzmo

maybe would it be possible to catch -vv value of py.test and do that at a given increased verbose level?

That's not possible without looking at sys.argv directly and having to check for both py.test and the verbose options (which is kind of gross).

Further, Betamax aims to be test framework independent. We ship a pytest fixture, but we also ship fixtures for other popular testing frameworks too. I'm asking in #pylib on Freenode IRC about how a library might provide extra debugging information along the way. I'll report back what I'm told.

sigmavirus24 avatar Feb 01 '17 12:02 sigmavirus24

@hroncok for what it's worth, I never received any help in #pylib so feel free to either close this or write some documentation about it. I think it's a great idea to mention common mistakes like this in the documentation but I lost track of this when I tried to rewrite the documentation a while back.

sigmavirus24 avatar Oct 15 '17 14:10 sigmavirus24

Let's document this then. "Common mistakes" section sounds great. @guyzmo are you willing to send a PR?

hroncok avatar Oct 28 '17 19:10 hroncok

I can do that, though please point me out which file and where you want the section to be included (basically a file#line github URL).

guyzmo avatar Oct 30 '17 12:10 guyzmo