betamaxpy / betamax

A VCR imitation designed only for python-requests.
https://betamax.readthedocs.io/en/latest/
Other
565 stars 62 forks source link

Issue with betamax not recording requests within unconsumed generators #131

Open guyzmo opened 7 years ago

guyzmo commented 7 years ago

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

--- 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).
sigmavirus24 commented 7 years ago

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.

guyzmo commented 7 years ago

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:

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.

sigmavirus24 commented 7 years ago

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 commented 6 years ago

@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.

hroncok commented 6 years ago

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

guyzmo commented 6 years ago

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).