|
12 | 12 | import pytest |
13 | 13 |
|
14 | 14 | from litestar.testing import subprocess_async_client, subprocess_sync_client |
| 15 | +from litestar.testing.client.subprocess_client import StartupError, run_app |
15 | 16 |
|
16 | 17 | if sys.platform == "win32": |
17 | 18 | asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) |
18 | 19 |
|
19 | 20 | ROOT = pathlib.Path(__file__).parent |
| 21 | +APP = "demo:app" |
20 | 22 |
|
21 | 23 |
|
22 | 24 | @pytest.fixture(name="async_client") |
23 | 25 | async def fx_async_client() -> AsyncIterator[httpx.AsyncClient]: |
24 | | - async with subprocess_async_client(workdir=ROOT, app="demo:app") as client: |
| 26 | + async with subprocess_async_client(workdir=ROOT, app=APP) as client: |
25 | 27 | yield client |
26 | 28 |
|
27 | 29 |
|
28 | 30 | @pytest.fixture(name="sync_client") |
29 | 31 | def fx_sync_client() -> Iterator[httpx.Client]: |
30 | | - with subprocess_sync_client(workdir=ROOT, app="demo:app") as client: |
| 32 | + with subprocess_sync_client(workdir=ROOT, app=APP) as client: |
31 | 33 | yield client |
32 | 34 |
|
33 | 35 |
|
| 36 | +async def test_run_app() -> None: |
| 37 | + """Ensure that method returns application url if started successfully""" |
| 38 | + with run_app(workdir=ROOT, app=APP) as url: |
| 39 | + assert isinstance(url, str) |
| 40 | + assert url.startswith("http://127.0.0.1:") |
| 41 | + |
| 42 | + |
| 43 | +async def test_run_app_exception() -> None: |
| 44 | + """ |
| 45 | + Ensure that method throws a StartupError if the application fails to start. |
| 46 | + To simulate this, we set retry_count=0, so that we don't check if the application has started. |
| 47 | + """ |
| 48 | + with pytest.raises(StartupError): |
| 49 | + with run_app(workdir=ROOT, app=APP, retry_count=0): |
| 50 | + ... |
| 51 | + |
| 52 | + |
34 | 53 | async def test_subprocess_async_client(async_client: httpx.AsyncClient) -> None: |
35 | 54 | """Demonstrates functionality of the async client with an infinite SSE source that cannot be tested with the |
36 | 55 | regular async test client. |
|
0 commit comments