Skip to content

Commit 5158590

Browse files
committed
fix import error and add check() method
1 parent ac318bf commit 5158590

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

pytest_httpserver/httpserver.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,15 @@ def add_assertion(self, obj):
906906
"""
907907
self.assertions.append(obj)
908908

909+
def check(self):
910+
"""
911+
Raises AssertionError or Errors raised in handlers.
912+
913+
Runs both :py:meth:`check_assertions` and :py:meth:`check_handler_errors`
914+
"""
915+
self.check_assertions()
916+
self.check_handler_errors()
917+
909918
def check_assertions(self):
910919
"""
911920
Raise AssertionError when at least one assertion added
@@ -925,6 +934,12 @@ def check_assertions(self):
925934
raise AssertionError(assertion)
926935

927936
def check_handler_errors(self):
937+
"""
938+
Re-Raises any errors caused in request handlers
939+
940+
The first error raised by a handler will be re-raised here, and then
941+
removed from the list.
942+
"""
928943
if self.handler_errors:
929944
raise self.handler_errors.pop(0)
930945

tests/test_handler_errors.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import requests
33
import werkzeug
44

5-
from pytest_httpserver import HTTPServer, Error
5+
from pytest_httpserver import HTTPServer
66

77

88
def test_check_assertions_raises_handler_assertions(httpserver: HTTPServer):
@@ -65,3 +65,28 @@ def test_missing_matcher_raises_exception(httpserver):
6565

6666
with pytest.raises(AssertionError):
6767
httpserver.check_assertions()
68+
69+
70+
def test_check_raises_errors_in_order(httpserver):
71+
def handler1(_):
72+
assert 1 == 2
73+
74+
def handler2(_):
75+
pass # does nothing
76+
77+
def handler3(_):
78+
raise ValueError
79+
80+
httpserver.expect_request("/foobar1").respond_with_handler(handler1)
81+
httpserver.expect_request("/foobar2").respond_with_handler(handler2)
82+
httpserver.expect_request("/foobar3").respond_with_handler(handler3)
83+
84+
requests.get(httpserver.url_for("/foobar1"))
85+
requests.get(httpserver.url_for("/foobar2"))
86+
requests.get(httpserver.url_for("/foobar3"))
87+
88+
with pytest.raises(AssertionError):
89+
httpserver.check()
90+
91+
with pytest.raises(ValueError):
92+
httpserver.check()

0 commit comments

Comments
 (0)