betamaxpy / betamax

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

py.test fixture is not friendly to parametrized tests #112

Closed hroncok closed 8 years ago

hroncok commented 8 years ago

Consider the following test:

@pytest.mark.usefixtures('betamax_session')
@pytest.mark.parametrize('filename', ('foo.txt', 'bar.txt'))
def test_upload_file(betamax_session, filename):
    result = somepkg.upload(file=filename, session=betamax_session)
    assert result['filename'] == filename

I would like this to work with simple URL and METHOD matchers, but unfortunatelly not, becouse the name of the cassette does not contian any parameters, and will simply be ...test_upload_file.json.

It would be awesome if the cassette name could somehow contain the arguments for parameters, such as in this case ...test_upload_file[foo.txt].json. Would thet be possible? Thanks

sigmavirus24 commented 8 years ago

Would thet be possible?

I don't know how to do that, but it might be possible. If you can research that and send a pull request, I would happily review it.

hroncok commented 8 years ago

For the record: Here's how the name is determined in betamax's code: https://github.com/sigmavirus24/betamax/blob/492116a368b5682ccd68e93b85414d47f51556e0/betamax/fixtures/pytest.py#L41

hroncok commented 8 years ago

And that line shall be replaced with:

cassette_name += request.node.name

I can do that as PR, but this as a change that breaks backwards compatibility. Any idea how to handle that nicely?

sigmavirus24 commented 8 years ago

The permanent link for future viewers of this issue is https://github.com/sigmavirus24/betamax/blob/492116a368b5682ccd68e93b85414d47f51556e0/betamax/fixtures/pytest.py#L41

sigmavirus24 commented 8 years ago

Any idea how to handle that nicely?

Have a second fixture for parameterized tests (e.g., betamax_parameterized_session and have both use the warnings module to indicate that betamax 1.0.0 will change the behaviour of betamax_session and remove betamax_parameterized_session.

hroncok commented 8 years ago

First draft is in #113, however this will be problematic with unprintable or path-specific characters like \n or /. So I need to come up with some sanitizing function that would make it good file name.