Skip to content

Waiting without pytest and without starting the server #251

Open
@palotasb

Description

@palotasb

Hi Zsolt!

As someone just getting started with this library, I ran into this issue:

import unittest
import requests
from pytest_httpserver import HTTPServer

class TestMyStuff(unittest.TestCase):
    def test_1(self):
        server = HTTPServer()
        server.expect_oneshot_request("/foobar").respond_with_json({"foo": "bar"})
        with server.wait(stop_on_nohandler=True, raise_assertions=True, timeout=1.0) as waiting:
              requests.get(server.url_for("/foobar"))  # raises requests.exceptions.ConnectionError
        assert waiting.result

I figured out the issue was with my code, the server needs to be started, and since my project doesn't use pytest, that needs to be done with the context manager manually.

          server.expect_oneshot_request("/foobar").respond_with_json({"foo": "bar"})
-         with server.wait(stop_on_nohandler=True, raise_assertions=True, timeout=1.0) as waiting:
-               requests.get(server.url_for("/foobar"))  # raises requests.exceptions.ConnectionError
+         with server:
+             with server.wait(stop_on_nohandler=True, raise_assertions=True, timeout=1.0) as waiting:
+                   requests.get(server.url_for("/foobar"))  # OK now

or more briefly:

          server.expect_oneshot_request("/foobar").respond_with_json({"foo": "bar"})
-         with server.wait(stop_on_nohandler=True, raise_assertions=True, timeout=1.0) as waiting:
+         with server, server.wait(stop_on_nohandler=True, raise_assertions=True, timeout=1.0) as waiting:
-               requests.get(server.url_for("/foobar"))  # raises requests.exceptions.ConnectionError
+               requests.get(server.url_for("/foobar"))  # OK now

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 the HTTPServer 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions