betamax
betamax copied to clipboard
Issue with betamax not recording requests within unconsumed generators
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
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.
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.
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.
@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.
Let's document this then. "Common mistakes" section sounds great. @guyzmo are you willing to send a PR?
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).