Skip to content

Commit 883887b

Browse files
committed
Get rid of main.create_app hardcoded import in favour to pytest ini settings
1 parent 0f46d94 commit 883887b

1 file changed

Lines changed: 30 additions & 6 deletions

File tree

aidbox_python_sdk/pytest_plugin.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,51 @@
1+
import importlib
12
import os
23
from typing import cast
34

45
import pytest
56
from aiohttp import BasicAuth, ClientSession, web
67
from yarl import URL
78

8-
from main import create_app as _create_app
9-
109
from . import app_keys as ak
1110

1211

13-
async def start_app(aiohttp_client):
14-
app = await aiohttp_client(_create_app(), server_kwargs={"host": "0.0.0.0", "port": 8081})
12+
def pytest_addoption(parser):
13+
parser.addini(
14+
"aidbox_create_app",
15+
"Dotted path to the create_app callable (module:name or module.name), e.g. main:create_app",
16+
default="main:create_app",
17+
)
18+
19+
20+
def _load_create_app(path: str):
21+
"""Import and return the create_app callable from the given dotted path."""
22+
if ":" in path:
23+
module_path, attr = path.split(":", 1)
24+
else:
25+
module_path, attr = path.rsplit(".", 1)
26+
mod = importlib.import_module(module_path)
27+
return getattr(mod, attr)
28+
29+
30+
@pytest.fixture(scope="session")
31+
def create_app(request):
32+
"""App factory; override in conftest or set pytest ini option aidbox_create_app."""
33+
path = request.config.getini("aidbox_create_app")
34+
return _load_create_app(path)
35+
36+
37+
async def start_app(aiohttp_client, create_app):
38+
app = await aiohttp_client(create_app(), server_kwargs={"host": "0.0.0.0", "port": 8081})
1539
sdk = cast(web.Application, app.server.app)[ak.sdk]
1640
sdk._test_start_txid = -1
1741

1842
return app
1943

2044

2145
@pytest.fixture()
22-
async def client(aiohttp_client):
46+
async def client(aiohttp_client, create_app):
2347
"""Instance of app's server and client"""
24-
return await start_app(aiohttp_client)
48+
return await start_app(aiohttp_client, create_app)
2549

2650

2751
class AidboxSession(ClientSession):

0 commit comments

Comments
 (0)