Skip to content

Commit a6d87dc

Browse files
authored
Merge pull request #19 from ShipChain/feature/httpretty-asserter-mixin
Modify HTTPretty to allow for assertion testing
2 parents 0bd243a + 3702247 commit a6d87dc

File tree

7 files changed

+504
-97
lines changed

7 files changed

+504
-97
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,45 @@ json_asserter.HTTP_200(response,
190190
If there is a class where every test may wish to use the `json_asserter`, than it may be easier to use to the `JsonAsserterMixin` found in `shipchain_common.test_utils`.
191191
This will automatically add the `json_asserter` and set it as a class attribute before the tests are run.
192192
This allows you to just call `self.json_asserter`, allowing for cleaner unit tests imports.
193+
194+
195+
### HTTPrettyAsserter Usage
196+
197+
When mocking calls, this can help in ensuring all calls, and only those, were made as expected, with the desired parameters.
198+
In order to use, simply import the HTTPrettyAsserter from test_utils and use it in place of the usual httpretty:
199+
```python
200+
@pytest.yield_fixture
201+
def modified_http_pretty():
202+
HTTPrettyAsserter.enable(allow_net_connect=False)
203+
yield HTTPrettyAsserter
204+
HTTPrettyAsserter.disable()
205+
```
206+
207+
Then, you just need to register the uris for the calls you want to mock, and ensure that it is returned in the mocking:
208+
```python
209+
@pytest.fixture
210+
def http_pretty_list_mocking(modified_http_pretty):
211+
modified_http_pretty.register_uri(modified_http_pretty.POST, 'http://google.com/path', status=status.HTTP_200_OK)
212+
modified_http_pretty.register_uri(modified_http_pretty.POST, 'http://google.com/other_path',
213+
status=status.HTTP_200_OK)
214+
modified_http_pretty.register_uri(modified_http_pretty.POST, 'http://bing.com/bing_path',
215+
status=status.HTTP_200_OK)
216+
return modified_http_pretty
217+
```
218+
219+
In a test that you want to check the calls on, you simply need to use the mocking fixture and call `.assert_calls(assertions)` on the fixture.
220+
These assertions will be a list of details that the call should have made. An example assertion is this:
221+
```python
222+
{
223+
'path': '/path',
224+
'body': {
225+
'integer': 1
226+
},
227+
'query': {
228+
'query_param_1': 1
229+
},
230+
'host': 'google.com',
231+
}
232+
```
233+
Only the path and the host are required parameters for the assertion. The body and query can be left out, but if included will be tested against.
234+
If there is a difference between the amount of calls made and the amount of assertions, no assertion will be made and instead an error will return.

0 commit comments

Comments
 (0)