question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide a py.test fixture in addition to the decorator

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:7
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
edmorleycommented, Mar 20, 2017

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
5reactions
tomvinercommented, 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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

pytest fixtures: explicit, modular, scalable
Software test fixtures initialize test functions. They provide a fixed baseline so that tests execute reliably and produce consistent, repeatable, results.
Read more >
How do I make pytest fixtures work with decorated functions?
Creating the decorator using the decorator package seems to do the trick. ... Fixture feature depends on test function signature.
Read more >
Fixtures and Parameters: Testing Code with Pytest - Medium
The first and easiest way to instantiate some dataset is to use pytest fixtures. pytest.fixture decorator makes it possible to inject the return ......
Read more >
End-To-End Tutorial For Pytest Fixtures With Examples
Along with parameterized test fixtures, pytest also provides decorators using which you can parameterize test functions.
Read more >
Five Advanced Pytest Fixture Patterns - Inspired Python
The pytest package is a great test runner, and it comes with a battery of features — among them the fixtures feature. A...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found