I would like to perform fuzzy assertions on the request body. Since that doesn't seem possible with the default RequestHandler, I added my own handler that does the assertions, and would like that assertion errors produced in these handlers are propagated to the pytest run via httpserver.check_assertions().
What I've currently done is:
my test:
def assert_appender(httpserver, handler):
def _handler(response):
try:
return handler(response)
except AssertionError as e:
httpserver.add_assertion(e)
raise
return _handler
def test_append(httpserver: HTTPServer):
def handler(request):
actual_data = request.data.decode()
assert 'hello' in actual_data
# ... more asserts
return Response('', 200)
httpserver.expect_request("/path").respond_with_handler(assert_appender(httpserver, handler))
httpserver.check_assertions() # should re-raise the assertion error from "handler"
assert response.ok
and i've modified this bit here:
|
def check_assertions(self): |
|
""" |
|
Raise AssertionError when at least one assertion added |
|
|
|
The first assertion added by :py:meth:`add_assertion` will be raised and |
|
it will be removed from the list. |
|
|
|
This method can be useful to get some insights into the errors happened in |
|
the sever, and to have a proper error reporting in pytest. |
|
""" |
|
|
|
if self.assertions: |
|
raise AssertionError(self.assertions.pop(0)) |
to do the following:
if self.assertions:
assertion = self.assertions.pop(0)
if isinstance(assertion, AssertionError):
raise assertion
raise AssertionError(assertion)
now i get the proper assertion formatting in pytest:
def handler(request):
actual_data = request.data.decode()
> assert "hello" in actual_data
E assert 'hello' in '{"foo":"bar"}'
test_http.py:34: AssertionError
would this be interesting to add? i'm happy to contribute a PR
I would like to perform fuzzy assertions on the request body. Since that doesn't seem possible with the default
RequestHandler, I added my own handler that does the assertions, and would like that assertion errors produced in these handlers are propagated to the pytest run viahttpserver.check_assertions().What I've currently done is:
my test:
and i've modified this bit here:
pytest-httpserver/pytest_httpserver/httpserver.py
Lines 900 to 912 in d98a6c4
to do the following:
now i get the proper assertion formatting in pytest:
would this be interesting to add? i'm happy to contribute a PR