3

I'd like to use pytest with Quart's extension quart-openapi, but the documentation examples and googling have not helped.

Where can I find a concrete, example of the pytest testing tool being used with quart-openapi?

So far I've read through these sources:

Quart's blog tutorial with testing

Quart documentation on testing

Stack Overflow question on similar tools

The project structure is:

├── 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

Actual results: ERROR at setup of test_app ... fixture 'app' not found

Expected results: I'm able to test with quart-openapi using pytest.

Farzad Vertigo
  • 2,458
  • 1
  • 29
  • 32
Eli H.
  • 35
  • 9

1 Answers1

4

Looking at the pytest fixture docs the name you give the fixture is the name you would reference. So I'd probably change the "name" param to be 'testapp' like so:

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

When I set this up in my own directory the above works and passes so it should work for you.

Zeroshade
  • 463
  • 2
  • 8