From 37d7a316509ff4b65294bc5414cff6041f06b7f1 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Mon, 14 Sep 2026 18:15:41 +0200 Subject: [PATCH] start with test_api (async, using fastapi) --- pyproject.toml | 9 +++++---- scopes/api/main.py | 4 ++-- scopes/tests/test_api.py | 27 +++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 scopes/tests/test_api.py diff --git a/pyproject.toml b/pyproject.toml index ff5b794..4686dc6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,13 +32,14 @@ app = [ auth = ["pyjwt[crypto]", "cryptography", "requests"] api = ["fastapi[standard]"] test = ["zope.testrunner"] -#test = ["pytest"] +apitest = ["pytest", "httpx2"] [tool.setuptools] packages = ["scopes"] -#[tool.pytest.ini_options] -#addopts = "-vv" -#python_files = "test_standard.py" # default: run only `standard` tests +[tool.pytest.ini_options] +addopts = ["-s"] +#test_paths = ["tests"] +python_files = ["test_api.py"] # use .pytest.ini file with `python_files = test_*.py` to run all tests diff --git a/scopes/api/main.py b/scopes/api/main.py index c9cefb7..f646b89 100644 --- a/scopes/api/main.py +++ b/scopes/api/main.py @@ -5,10 +5,10 @@ from fastapi import FastAPI app = FastAPI() @app.get('/') -def read_root(): +async def read_root(): return {'Hello': 'World'} @app.get('/persons/{name}') -def read_person(name: str, q: str | None = None): +async def read_person(name: str, q: str | None = None): return {'name': name, 'query': q} diff --git a/scopes/tests/test_api.py b/scopes/tests/test_api.py new file mode 100644 index 0000000..1b2bef8 --- /dev/null +++ b/scopes/tests/test_api.py @@ -0,0 +1,27 @@ +# scopes.tests.test_api + +"""Tests for the fastapi- (and asyncio-) based stuff.""" + +import sys +print('***', sys.path) + +import pytest +from fastapi.testclient import TestClient +from httpx2 import ASGITransport, AsyncClient + +from scopes.api.main import app + +def async_client(): + return AsyncClient(transport=ASGITransport(app=app), base_url='http://test') + +#@pytest.mark.anyio +#async def test_root(): + #async with async_client() as clt: + #response = await clt.get('/') + +def test_root(): + with TestClient(app) as clt: + response = clt.get('/') + assert response.status_code == 200 + assert response.json() == {'Hello': 'World'} +