1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
# Migrate from requests
## responses
Here's a few examples on how to migrate your code *from* the `responses` library *to* `respx`.
### Patching the Client
#### Decorator
``` python
@responses.activate
def test_foo():
...
```
``` python
@respx.mock
def test_foo():
...
```
> See [Router Settings](guide.md#router-settings) for more details.
#### Context Manager
``` python
def test_foo():
with responses.RequestsMock() as rsps:
...
```
``` python
def test_foo():
with respx.mock() as respx_mock:
...
```
> See [Router Settings](guide.md#router-settings) for more details.
#### unittest setUp
``` python
def setUp(self):
self.responses = responses.RequestsMock()
self.responses.start()
self.addCleanup(self.responses.stop)
```
``` python
def setUp(self):
self.respx_mock = respx.mock()
self.respx_mock.start()
self.addCleanup(self.respx_mock.stop)
```
> See [unittest examples](examples.md#reuse-setup-teardown) for more details.
### Mock a Response
``` python
responses.add(
responses.GET, "https://example.org/",
json={"foo": "bar"},
status=200,
)
```
``` python
respx.get("https://example.org/").respond(200, json={"foo": "bar"})
```
> See [Routing Requests](guide.md#routing-requests) and [Mocking Responses](guide.md#mocking-responses) for more details.
### Mock an Exception
``` python
responses.add(
responses.GET, "https://example.org/",
body=Exception("..."),
)
```
``` python
respx.get("https://example.org/").mock(side_effect=ConnectError)
```
> See [Exception Side Effect](guide.md#exceptions) for more details.
### Subsequent Responses
``` python
responses.add(responses.GET, "https://example.org/", status=200)
responses.add(responses.GET, "https://example.org/", status=500)
```
``` python
respx.get("https://example.org/").mock(
side_effect=[Response(200), Response(500)]
)
```
> See [Iterable Side Effect](guide.md#iterable) for more details.
### Callbacks
``` python
def my_callback(request):
headers = {"Content-Type": "application/json"}
body = {"foo": "bar"}
return (200, headers, json.dumps(resp_body))
responses.add_callback(
responses.GET, "http://example.org/",
callback=my_callback,
)
```
``` python
def my_side_effect(request, route):
return Response(200, json={"foo": "bar"})
respx.get("https://example.org/").mock(side_effect=my_side_effect)
```
> See [Mock with a Side Effect](guide.md#mock-with-a-side-effect) for more details.
### History and Assertions
### History
``` python
responses.calls[0].request
responses.calls[0].response
```
``` python
respx.calls[0].request
respx.calls[0].response
request, response = respx.calls[0]
respx.calls.last.response
```
> See [Call History](guide.md#call-history) for more details.
#### Call Count
``` python
responses.assert_call_count("http://example.org/", 1)
```
``` python
route = respx.get("https://example.org/")
assert route.call_count == 1
```
> See [Call History](guide.md#call-history) for more details.
#### All Called
``` python
with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
...
```
``` python
with respx.mock(assert_all_called=False) as respx_mock:
...
```
> See [Assert all Called](guide.md#assert-all-called) for more details.
### Modify Mocked Response
``` python
responses.add(responses.GET, "http://example.org/", json={"data": 1})
responses.replace(responses.GET, "http://example.org/", json={"data": 2})
```
``` python
respx.get("https://example.org/").respond(json={"data": 1})
respx.get("https://example.org/").respond(json={"data": 2})
```
### Pass Through Requests
``` python
responses.add_passthru("https://example.org/")
```
``` python
respx.route(url="https://example.org/").pass_through()
```
> See [Pass Through](guide.md#pass-through) for more details.
## requests-mock
*todo ... contribution welcome* ;-)
|