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.

Allow applying @responses.activate to a class

See original GitHub issue

Right now I have to apply the decorator to every method I want to mock responses in; I’d prefer to apply the decorator only to the TestCase declaration as my TestCase does http in every test method. This would keep requests consistent with the mock.patch behaviour.

E.g.:

@responses.activate
class CommandHandlerTestCase(unittest.TestCase):

    def test_function_1(self):
        responses.add(...)

    def test_function_2(self):
        responses.add(...)

    def test_function_3(self):
        responses.add(...)

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:14
  • Comments:18 (5 by maintainers)

github_iconTop GitHub Comments

16reactions
asfaltboycommented, Sep 29, 2017

Just wanted to share how I solved this, using a mixin class, that uses TestCase setUp and tearDown to call the start, stop and reset methods of responses, and adds an (optional) default response. The one I use and maintain is here: https://gist.github.com/asfaltboy/bebd2c6943c34b0b27a7e3060448049b

Here’s the “gist” of it (no pun intended):

class ResponsesMixin(object):
    def setUp(self):
        assert responses, 'responses package required to use ResponsesMixin'
        responses.start()
        super(ResponsesMixin, self).setUp()

    def tearDown(self):
        super(ResponsesMixin, self).tearDown()
        responses.stop()
        responses.reset()

Then use in a TestCase:

class TestReportClient(ResponsesMixin, unittest.TestCase):
    def test_function_1(self):
        responses.add(...)

@dcramer let me know if you would like a PR of this, i.e with tests and docs

Update: Updated to support versions > 0.5.1

9reactions
bblanchoncommented, Jun 25, 2020

Even if this mixing doesn’t end up in the library, it should at least be in the documentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A Utility for Mocking Out The Python Requests Library - Morioh
Responses. A utility library for mocking out the requests Python library. Note. Responses requires Python 3.7 or newer, and requests >= 2.0 ...
Read more >
How can I mock requests and the response? - python
Here is a solution with requests Response class. It is cleaner IMHO. import json from ...
Read more >
API Reference — HTTPretty 1.1.4 documentation
import re, json import httpretty httpretty.enable() # request passes through fake socket response = requests.get('https://httpbin.org') httpretty.disable() ...
Read more >
Activate Early Access class teams created by your IT Admin
Follow these steps to activate your class teams and give students access after ... These instructions apply to you if the IT Admin...
Read more >
Mocking Python Requests with Responses - cra mr
If this post was valuable and you'd like me to restore it, let me know! ... @responses.activate def test_yipit_api_returning_deals(): ...
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