FlaskClient, созданный с помощью conftest.py (test_client), находится вне контекста запроса. - PullRequest
0 голосов
/ 05 июля 2019

Так я создал прибор для test_client

    @pytest.fixture(scope='session')
    def test_client(app):
        client = app.test_client()
        ctx = app.app_context()
        ctx.push()
        yield client
        ctx.pop()

Вот как я называю первый тест:

    import pytest


    @pytest.mark.parametrize('url, status_code', [
        ('/',  200)
    ])
    def test_api_request(url, status_code, test_client):
        res = test_client.get(url)
        assert res.status_code == status_code

Я получаю эту ошибку:

    name = 'request'

        def _lookup_req_object(name):
            top = _request_ctx_stack.top
            if top is None:
    >           raise RuntimeError(_request_ctx_err_msg)
    E           RuntimeError: Working outside of request context.
    E           
    E           This typically means that you attempted to use functionality that needed
    E           an active HTTP request.  Consult the documentation on testing for
    E           information about how to avoid this problem.

Я совершенно растерялся, потому что следовал этому документу: http://flask.pocoo.org/docs/1.0/testing/ Мне не нужно соединение БД.

Когда я печатаю test_client, чтобы увидеть, инициализирован ли он с помощью прибора:

    <FlaskClient <Flask 'my_app_name'>>
...