Automatically skip tests when veilid-server isn't running

This commit is contained in:
Teknique 2023-07-21 16:54:32 -07:00
parent b28330135a
commit 1e529c2474
3 changed files with 138 additions and 61 deletions

View file

@ -1,35 +1,30 @@
import os
from functools import cache
"""Common test fixtures."""
from typing import AsyncGenerator
import pytest
import pytest_asyncio
import veilid
from veilid.json_api import _JsonVeilidAPI
import veilid
from .api import VeilidTestConnectionError, api_connector
pytest_plugins = ("pytest_asyncio",)
@cache
def server_info() -> tuple[str, int]:
"""Return the hostname and port of the test server."""
VEILID_SERVER = os.getenv("VEILID_SERVER")
if VEILID_SERVER is None:
return "localhost", 5959
hostname, *rest = VEILID_SERVER.split(":")
if rest:
return hostname, int(rest[0])
return hostname, 5959
async def simple_update_callback(update: veilid.VeilidUpdate):
print(f"VeilidUpdate: {update}")
@pytest_asyncio.fixture
async def api_connection() -> AsyncGenerator[_JsonVeilidAPI, None]:
hostname, port = server_info()
api = await veilid.json_api_connect(hostname, port, simple_update_callback)
try:
api = await api_connector(simple_update_callback)
except VeilidTestConnectionError:
pytest.skip("Unable to connect to veilid-server.")
return
async with api:
# purge routes to ensure we start fresh
await api.debug("purge routes")