使用 Quart 的扩展 Quart-OpenAPI 使用 pytest 进行测试的完整示例在哪里?
Where is a complete example of testing with pytest using Quart's extension Quart-OpenAPI?
我想将 pytest 与 Quart 的扩展名 quart-openapi 一起使用,但文档示例和谷歌搜索没有帮助。
在哪里可以找到与 quart-openapi 一起使用的 pytest 测试工具的具体示例?
到目前为止,我已经通读了这些来源:
Quart's blog tutorial with testing
Quart documentation on testing
项目结构为:
├── app
│ ├── __init__.py
│ ├── api.py
│ ├── log.py
│
├── requirements.txt
├── run.py
└── tests
├── __init__.py
└── test_endpoint.py
app/__init__.py
:
from .api import QUART_APP
api.py
:
"""Registered endpoints"""
from quart import jsonify
from quart_openapi import Pint, Resource
# Docs say that Pint will forward all init args to Quart()
QUART_APP = Pint(__name__, title="Quart tts")
@QUART_APP.route('/')
class Home(Resource):
""" Endpoint that reports its own status """
async def get(self):
""" Report status of service """
return jsonify({'OK': True, 'hello': 'world'})
test_endpoint.py
:
import pytest
from app.api import QUART_APP as app
@pytest.fixture(name='test_app')
def _test_app():
return app
@pytest.mark.asyncio
async def test_app(app):
client = app.test_client()
response = await client.get('/')
assert response.status_code == 200
实际结果:ERROR at setup of test_app ... fixture 'app' not found
预期结果:我能够使用 pytest 使用 quart-openapi 进行测试。
查看 pytest fixture 文档,您为 fixture 指定的名称就是您要引用的名称。所以我可能会像这样将 "name" 参数更改为 'testapp':
import pytest
from app.api import QUART_APP as app
@pytest.fixture(name='testapp')
def _test_app():
return app
@pytest.mark.asyncio
async def test_app(testapp):
client = testapp.test_client()
response = await client.get('/')
assert response.status_code == 200
当我在我自己的目录中设置它时,上面的工作和通过所以它应该适合你。
我想将 pytest 与 Quart 的扩展名 quart-openapi 一起使用,但文档示例和谷歌搜索没有帮助。
在哪里可以找到与 quart-openapi 一起使用的 pytest 测试工具的具体示例?
到目前为止,我已经通读了这些来源:
Quart's blog tutorial with testing
Quart documentation on testing
项目结构为:
├── app
│ ├── __init__.py
│ ├── api.py
│ ├── log.py
│
├── requirements.txt
├── run.py
└── tests
├── __init__.py
└── test_endpoint.py
app/__init__.py
:
from .api import QUART_APP
api.py
:
"""Registered endpoints"""
from quart import jsonify
from quart_openapi import Pint, Resource
# Docs say that Pint will forward all init args to Quart()
QUART_APP = Pint(__name__, title="Quart tts")
@QUART_APP.route('/')
class Home(Resource):
""" Endpoint that reports its own status """
async def get(self):
""" Report status of service """
return jsonify({'OK': True, 'hello': 'world'})
test_endpoint.py
:
import pytest
from app.api import QUART_APP as app
@pytest.fixture(name='test_app')
def _test_app():
return app
@pytest.mark.asyncio
async def test_app(app):
client = app.test_client()
response = await client.get('/')
assert response.status_code == 200
实际结果:ERROR at setup of test_app ... fixture 'app' not found
预期结果:我能够使用 pytest 使用 quart-openapi 进行测试。
查看 pytest fixture 文档,您为 fixture 指定的名称就是您要引用的名称。所以我可能会像这样将 "name" 参数更改为 'testapp':
import pytest
from app.api import QUART_APP as app
@pytest.fixture(name='testapp')
def _test_app():
return app
@pytest.mark.asyncio
async def test_app(testapp):
client = testapp.test_client()
response = await client.get('/')
assert response.status_code == 200
当我在我自己的目录中设置它时,上面的工作和通过所以它应该适合你。