umap/umap/tests/integration/conftest.py
Alexis Métaireau 2825ebbd17
tests(sync): Change the way the websocket server is run in the tests
Using [pytest-xprocess](https://pytest-xprocess.readthedocs.io/) proved
not being as useful as I thought at first, because it was causing
intermitent failures when starting the process.

The code now directly uses `subprocess.popen` calls to start the server.
The tests are grouped together using the following decorator:

`@pytest.mark.xdist_group(name="websockets")`

Tests now need to be run with the `pytest --dist loadgroup` so that all
tests of the same group happen on the same process.

More details on this blogpost:

  https://blog.notmyidea.org/start-a-process-when-using-pytest-xdist.html
2024-06-07 16:32:25 +02:00

60 lines
1.8 KiB
Python

import os
import subprocess
import time
from pathlib import Path
import pytest
from playwright.sync_api import expect
@pytest.fixture(autouse=True)
def set_timeout(context):
timeout = int(os.environ.get("PLAYWRIGHT_TIMEOUT", 7500))
context.set_default_timeout(timeout)
context.set_default_navigation_timeout(timeout)
expect.set_options(timeout=timeout)
@pytest.fixture(autouse=True)
def mock_osm_tiles(page):
if not bool(os.environ.get("PLAYWRIGHT_USE_TILES", False)):
page.route("*/**/osmfr/**", lambda route: route.fulfill())
@pytest.fixture
def login(context, settings, live_server):
def do_login(user):
# TODO use storage state to do login only once per session
# https://playwright.dev/python/docs/auth
settings.ENABLE_ACCOUNT_LOGIN = True
page = context.new_page()
page.goto(f"{live_server.url}/en/")
page.locator(".login").click()
page.get_by_placeholder("Username").fill(user.username)
page.get_by_placeholder("Password").fill("123123")
page.locator('#login_form input[type="submit"]').click()
return page
return do_login
@pytest.fixture
def websocket_server():
# Find the test-settings, and put them in the current environment
settings_path = (Path(__file__).parent.parent / "settings.py").absolute().as_posix()
os.environ["UMAP_SETTINGS"] = settings_path
ds_proc = subprocess.Popen(
[
"umap",
"run_websocket_server",
],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
time.sleep(2)
# Ensure it started properly before yielding
assert not ds_proc.poll(), ds_proc.stdout.read().decode("utf-8")
yield ds_proc
# Shut it down at the end of the pytest session
ds_proc.terminate()