Created
June 20, 2025 14:16
-
-
Save dstanek/326bc12ed3315d5c84aa80beb90fc5e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import threading | |
import urllib.request | |
from http.server import SimpleHTTPRequestHandler, HTTPServer | |
import pytest | |
@pytest.fixture(scope="session", autouse=True) | |
def webserver(): | |
"""Start a simple web server automatically for the test session.""" | |
# Find a free port | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | |
s.bind(('', 0)) | |
port = s.getsockname()[1] | |
server_address = ("127.0.0.1", port) | |
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler) | |
thread = threading.Thread(target=httpd.serve_forever) | |
thread.daemon = True | |
thread.start() | |
print(f"\n[webserver fixture] Started at http://{server_address[0]}:{server_address[1]}") | |
yield {"host": server_address[0], "port": port, "url": f"http://{server_address[0]}:{port}"} | |
httpd.shutdown() | |
thread.join() | |
print("\n[webserver fixture] Stopped") | |
def test_fetch_index(webserver): | |
response = urllib.request.urlopen(webserver["url"]) | |
assert response.status == 200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment