5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Responsesでrequestsモジュール経由のHTTPリクエストをモックする

Posted at

ReponsesはHTTP通信を実施するrequestsモジュールをモックするライブラリ。信頼できそうなDropbox製。ライセンスはAPL 2.0。

class APITestCase(TestCase):
    @responses.activate
    def test_api(self):
        import api

        # setup
        responses.add(
            responses.GET,
            'https://example.com/api/v3/users',
            status=200,
            body="[{'user': {'id': 1, 'username': 'test'}}]",
            content_type="application/json",
        )

        # test
        api.get_users()
        assert responses.calls[0].request.method == 'GET'
        assert responses.calls[0].request.url == 'https://example.com/api/v3/users'

似たようなのでhttprettyというのもあるんですが、Responsesの方が自分の好みにあってました。しばらく試してみようと思います。

※Responsesのインターフェースはhttprettyの影響を受けていると思います。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?