Skip to content

Commit 6ca487b

Browse files
committed
doc: improve tutorial
Add example from #68 to the documentation.
1 parent 911a2cf commit 6ca487b

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

doc/tutorial.rst

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,69 @@ your test will properly fail:
436436
437437
httpserver.check_assertions() # this will raise AssertionError and make the test failing
438438
439+
This will also produce a (hopefully) helpful description about what went wrong::
440+
441+
> raise AssertionError(assertion)
442+
E AssertionError: No handler found for request <Request 'http://localhost:41085/foobaz' [GET]> with data b''.Ordered matchers:
443+
E <RequestMatcher uri='/foobar' method='__ALL' query_string=None headers={} data=None json=<UNDEFINED>>
444+
E <RequestMatcher uri='/foobaz' method='__ALL' query_string=None headers={} data=None json=<UNDEFINED>>
445+
E
446+
E Oneshot matchers:
447+
E none
448+
E
449+
E Persistent matchers:
450+
E none
451+
452+
453+
Calling ``check_assertions()`` for all tests
454+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
455+
456+
Sometimes you want to see the informative message made by ``check_assertions()`` if
457+
your test failed.
458+
459+
In such case you can implement a new fixture or override the behavior:
460+
461+
462+
.. code:: python
463+
464+
from collections.abc import Iterable
465+
from pytest_httpserver import HTTPServer
466+
import requests
467+
468+
import pytest
469+
470+
471+
@pytest.fixture
472+
def httpserver(httpserver: HTTPServer) -> Iterable[HTTPServer]:
473+
yield httpserver
474+
httpserver.check_assertions() # this will raise AssertionError and make the test failing
475+
476+
477+
def test_client(httpserver: HTTPServer):
478+
httpserver.expect_request("/foo").respond_with_data("foo")
479+
httpserver.expect_request("/bar").respond_with_data("bar")
480+
481+
resp = requests.get(httpserver.url_for("/foobar")) # gets 500
482+
resp.raise_for_status() # raises error
483+
484+
485+
When the tests are run with ``-vv`` then it will show both errors::
486+
487+
FAILED example2.py::test_client - requests.exceptions.HTTPError: 500 Server Error: INTERNAL SERVER ERROR for url: http://localhost:37425/foobar
488+
ERROR example2.py::test_client - AssertionError: No handler found for request <Request 'http://localhost:37425/foobar' [GET]> with data b''.
489+
Ordered matchers:
490+
none
491+
492+
Oneshot matchers:
493+
none
494+
495+
Persistent matchers:
496+
<RequestMatcher uri='/foo' method='__ALL' query_string=None headers={} data=None json=<UNDEFINED>>
497+
<RequestMatcher uri='/bar' method='__ALL' query_string=None headers={} data=None json=<UNDEFINED>>
498+
499+
500+
Logs
501+
~~~~
439502

440503
The server writes a log about the requests and responses which were
441504
processed. This can be accessed in the `log` attribute of the http server.

0 commit comments

Comments
 (0)