start with test_api (async, using fastapi)
This commit is contained in:
parent
7b9b3e2d47
commit
37d7a31650
3 changed files with 34 additions and 6 deletions
|
|
@ -32,13 +32,14 @@ app = [
|
||||||
auth = ["pyjwt[crypto]", "cryptography", "requests"]
|
auth = ["pyjwt[crypto]", "cryptography", "requests"]
|
||||||
api = ["fastapi[standard]"]
|
api = ["fastapi[standard]"]
|
||||||
test = ["zope.testrunner"]
|
test = ["zope.testrunner"]
|
||||||
#test = ["pytest"]
|
apitest = ["pytest", "httpx2"]
|
||||||
|
|
||||||
[tool.setuptools]
|
[tool.setuptools]
|
||||||
packages = ["scopes"]
|
packages = ["scopes"]
|
||||||
|
|
||||||
#[tool.pytest.ini_options]
|
[tool.pytest.ini_options]
|
||||||
#addopts = "-vv"
|
addopts = ["-s"]
|
||||||
#python_files = "test_standard.py" # default: run only `standard` tests
|
#test_paths = ["tests"]
|
||||||
|
python_files = ["test_api.py"]
|
||||||
# use .pytest.ini file with `python_files = test_*.py` to run all tests
|
# use .pytest.ini file with `python_files = test_*.py` to run all tests
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,10 @@ from fastapi import FastAPI
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
@app.get('/')
|
@app.get('/')
|
||||||
def read_root():
|
async def read_root():
|
||||||
return {'Hello': 'World'}
|
return {'Hello': 'World'}
|
||||||
|
|
||||||
@app.get('/persons/{name}')
|
@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}
|
return {'name': name, 'query': q}
|
||||||
|
|
||||||
|
|
|
||||||
27
scopes/tests/test_api.py
Normal file
27
scopes/tests/test_api.py
Normal file
|
|
@ -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'}
|
||||||
|
|
||||||
Loading…
Add table
Reference in a new issue