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

Provide a py.test fixture in addition to the decorator #47

Closed
ThiefMaster opened this issue Feb 13, 2015 · 8 comments
Closed

Provide a py.test fixture in addition to the decorator #47

ThiefMaster opened this issue Feb 13, 2015 · 8 comments

Comments

@ThiefMaster
Copy link

When using py.test for tests it's usually nicer to activate features like this using a fixture. The usage with the fixture (assuming it's named responses) could be like this:

def test_my_api(responses):
    responses.add(responses.GET, 'http://twitter.com/api/1/foobar',
                  body='{"error": "not found"}', status=404,
                  content_type='application/json')

    # ...

I.e. instead of the decorator you'd use a responses fixture and instead of calling add as a global from the imported module (if you register it as a pytest plugin the user doesn't need to import it at all) you'd call it on the object returned by the fixture function (which could simple be the module responses, but a wrapper would be cleaner of course).

@floer32
Copy link

floer32 commented Feb 18, 2015

👍

1 similar comment
@nichochar
Copy link

👍

@aequitas
Copy link

Have not tested it in all situations yet, but this works for me:

# conftest.py
import pytest
from responses import mock


@pytest.yield_fixture
def responses():
    rsps = mock
    rsps.start()
    yield rsps
    rsps.stop()
    rsps.reset()

@floer32
Copy link

floer32 commented Jul 23, 2015

Seems like a good start; how to make it obvious/deterministic where to call responses.add though? Maybe do all of that in a responses_setup fixture and make the start/stop/reset fixture (responses_activate) depend on that?

I suppose we could KISS and put both items just in an example in the README, eh?

@greenmoon55
Copy link

👍

@tomviner
Copy link
Contributor

tomviner commented Jun 29, 2016

Or even more simply:

@pytest.yield_fixture
def responses():
    with responses.RequestsMock() as rsps:
        yield rsps

Should we add this to the README?

@floer32
Copy link

floer32 commented Jun 29, 2016

A README addition makes sense

tomviner added a commit to tomviner/responses that referenced this issue Jun 29, 2016
tomviner added a commit to tomviner/responses that referenced this issue Jul 4, 2016
@edmorley
Copy link
Contributor

Since pytest 3.0 yield_fixture is deprecated in favour of using fixture directly:
http://doc.pytest.org/en/latest/yieldfixture.html

ie:

@pytest.fixture
def responses():
    with responses.RequestsMock() as rsps:
        yield rsps

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

7 participants