-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Waiting without pytest and without starting the server #251
Comments
Hi Boldizsár, For the first sight I thing doing server stop/start from the In pytest we also don't stop the server after each test as it causes some time penalty (0.1 sec or similar), so instead of stopping/starting we are clearing the server state instead. Because of this, it would be complicated to implement the stopping logic in the wait method. I think with the unittest code you have, there are still possibilities.
What do you think, would any of these work for you? |
This is what I actually have currently but maybe I'll switch to the classmethods and I haven't considered not I think I mostly wanted to We do run the |
I found that there's setUpModule and tearDownModule also, for module level setup and teardown. Example: import unittest
from pytest_httpserver import HTTPServer
import requests
httpserver = HTTPServer()
def setUpModule():
httpserver.start()
def tearDownModule():
httpserver.stop()
class TestMyStuff(unittest.TestCase):
def test_foo(self):
httpserver.expect_request("/foo").respond_with_data("OK")
self.assertEqual(requests.get(httpserver.url_for("/foo")).text, "OK")
def test_bar(self):
# no cross-talk
self.assertNotEqual(requests.get(httpserver.url_for("/foo")).text, "OK")
def tearDown(self):
httpserver.clear() There's some limited support for fixtures: https://docs.pytest.org/en/7.1.x/how-to/unittest.html#mixing-pytest-fixtures-into-unittest-testcase-subclasses-using-marks But using setUp and tearDown (module and class levels, as well) is the idiomatic way to use the unittest module, I think. |
For some reason, having this code without any # test_pytest_httpserver.py
# python -m unittest test_pytest_httpserver
import unittest
import requests
from pytest_httpserver import HTTPServer
class TestMyStuff(unittest.TestCase):
def setUp(self):
self.server = HTTPServer()
self.server.start()
def tearDown(self):
self.server.check()
self.server.stop()
def test_the_test_server_setup(self):
self.server.expect_oneshot_request("/foobar").respond_with_json({"foo": "bar"})
requests.get(self.server.url_for("/UNEXPECTED"))
self.assertEqual(len(self.server.log), 1) $ python -m unittest test_pytest_httpserver
127.0.0.1 - - [23/May/2023 11:10:53] "GET /UNEXPECTED HTTP/1.1" 500 -
F
======================================================================
FAIL: test_the_test_server_setup (test_pytest_httpserver.TestMyStuff)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/example/test_pytest_httpserver.py", line 11, in tearDown
self.server.check()
File "/Users/example/venv/venv-pytest-httpserver/lib/python3.8/site-packages/pytest_httpserver/httpserver.py", line 776, in check
self.check_assertions()
File "/Users/example/venv/venv-pytest-httpserver/lib/python3.8/site-packages/pytest_httpserver/httpserver.py", line 795, in check_assertions
raise AssertionError(assertion)
AssertionError: No handler found for request <Request 'http://localhost:53222/UNEXPECTED' [GET]>.
Ordered matchers:
none
Oneshot matchers:
<RequestMatcher uri='/foobar' method='__ALL' query_string=None headers={} data=None json=<UNDEFINED>>
Persistent matchers:
none
----------------------------------------------------------------------
Ran 1 test in 0.017s
FAILED (failures=1)
^CException ignored in: <module 'threading' from '/opt/homebrew/Caskroom/miniconda/base/lib/python3.8/threading.py'>
Traceback (most recent call last):
File "/opt/homebrew/Caskroom/miniconda/base/lib/python3.8/threading.py", line 1388, in _shutdown
lock.acquire()
KeyboardInterrupt: Is there something I'm doing wrong here? Note that after the line |
Now I think I remember better, this is why I started using |
This is the current best setup I have: # test_pytest_httpserver.py
# python -m unittest test_pytest_httpserver
import contextlib
import unittest
import requests
from pytest_httpserver import HTTPServer
class TestMyStuff(unittest.TestCase):
def setUp(self):
self.server = HTTPServer()
self._exit_stack = contextlib.ExitStack()
self.server.start()
self._exit_stack.callback(self.server.stop)
self._exit_stack.callback(self.server.check)
waiter = self.server.wait()
self._exit_stack.enter_context(waiter)
def tearDown(self):
self._exit_stack.close()
def test_the_test_server_setup(self):
self.server.expect_oneshot_request("/foobar").respond_with_json({"foo": "bar"})
response = requests.get(self.server.url_for("/foobar"))
response.raise_for_status()
self.assertDictEqual(response.json(), {"foo": "bar"})
@unittest.expectedFailure
def test_the_test_server_setup_for_failed_test_cases(self):
self.server.expect_oneshot_request("/foobar").respond_with_json({"foo": "bar"})
requests.get(self.server.url_for("/UNEXPECTED"))
# We need this call, otherwise the @unittest.expectedFailure won't have any effect
# on the exception coming from the tearDown() after this test case
self._exit_stack.close() The actual test case code is very minimal, only |
Hi Zsolt!
As someone just getting started with this library, I ran into this issue:
I figured out the issue was with my code, the
server
needs to be started, and since my project doesn't usepytest
, that needs to be done with the context manager manually.or more briefly:
I first thought I'd just let you know that I got confused about this part of the API.
Then I was wondering, what would you think about the
.wait()
context manager also doing the work of theHTTPServer
context manager if the server isn't started yet? That would make the original code work, but I'm not sure about potential downsides. Maybe there are situations where you want to start.wait()
-ing before you start a server?The text was updated successfully, but these errors were encountered: