Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,45 @@ json_asserter.HTTP_200(response,
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`.
This will automatically add the `json_asserter` and set it as a class attribute before the tests are run.
This allows you to just call `self.json_asserter`, allowing for cleaner unit tests imports.


### HTTPrettyAsserter Usage

When mocking calls, this can help in ensuring all calls, and only those, were made as expected, with the desired parameters.
In order to use, simply import the HTTPrettyAsserter from test_utils and use it in place of the usual httpretty:
```python
@pytest.yield_fixture
def modified_http_pretty():
HTTPrettyAsserter.enable(allow_net_connect=False)
yield HTTPrettyAsserter
HTTPrettyAsserter.disable()
```

Then, you just need to register the uris for the calls you want to mock, and ensure that it is returned in the mocking:
```python
@pytest.fixture
def http_pretty_list_mocking(modified_http_pretty):
modified_http_pretty.register_uri(modified_http_pretty.POST, 'http://google.com/path', status=status.HTTP_200_OK)
modified_http_pretty.register_uri(modified_http_pretty.POST, 'http://google.com/other_path',
status=status.HTTP_200_OK)
modified_http_pretty.register_uri(modified_http_pretty.POST, 'http://bing.com/bing_path',
status=status.HTTP_200_OK)
return modified_http_pretty
```

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.
These assertions will be a list of details that the call should have made. An example assertion is this:
```python
{
'path': '/path',
'body': {
'integer': 1
},
'query': {
'query_param_1': 1
},
'host': 'google.com',
}
```
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.
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.
Loading