首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以下是使用Quart的扩展Quart-OpenAPI进行pytest测试的完整示例?

以下是使用Quart的扩展Quart-OpenAPI进行pytest测试的完整示例?
EN

Stack Overflow用户
提问于 2019-01-31 06:36:19
回答 1查看 715关注 0票数 1

我想将pytest与quart的扩展quart-openapi一起使用,但是文档示例和谷歌搜索都没有帮助。

我在哪里可以找到与quart-openapi一起使用的pytest测试工具的具体示例?

到目前为止,我已经通读了这些资源:

Quart's blog tutorial with testing

Quart documentation on testing

Stack Overflow question on similar tools

项目结构为:

代码语言:javascript
复制
├── app
│   ├── __init__.py
│   ├── api.py
│   ├── log.py
│   
├── requirements.txt
├── run.py
└── tests
    ├── __init__.py
    └── test_endpoint.py

app/__init__.py

代码语言:javascript
复制
    from .api import QUART_APP

api.py

代码语言:javascript
复制
    """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

代码语言:javascript
复制
    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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-01 00:31:16

查看pytest fixture文档,您给出的fixture名称就是您要引用的名称。因此,我可能会将"name“参数改为”testapp“,如下所示:

代码语言:javascript
复制
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

当我在我自己的目录中设置它时,上面的代码可以工作并通过,所以它应该适用于您。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54450601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档