pytest-dev / pytest-flask

A set of pytest fixtures to test Flask applications

Home Page:http://pytest-flask.readthedocs.org/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using live_server fixture leads to ScopeMismatch Error

ren1982 opened this issue · comments

I'm trying to do the most basic live server test (based on Quickstart and documentation) and despite doing the most bare-bones example possible, I keep getting ScopeMismatch: You tried to access the 'function' scoped fixture 'app' with a 'session' scoped request object, involved factories when running the test.

app.py looks like this:

from flask import Flask


def create_app():
    app = Flask(__name__)

    @app.route('/')
    def main():
        return "<h1>Hello</h1> world"

    return app

conftest.py looks like this

import pytest

from app import create_app


@pytest.fixture
def app():
    app = create_app()
    return app

And finally test_app.py looks like this

import pytest


@pytest.mark.usefixtures('live_server')
def test_server_is_up():
    assert True

(Don't be confused as to why the test is just assert True. I've also tried it with using urllib to open a URL then checking the code but that failed the same way as well. I was hoping doing a basic test that doesn't even use the live server would help resolve the issue but it didn't.)

Python 3.8.1
Flask 1.1.1
pytest==5.3.5
pytest-flask==1.0.0

Please change your app fixture to:

@pytest.fixture(scope="session")
def app():
    app = create_app()
    return app

Does that fix it?

Btw thanks for noticing this, I have updated the docs: the live_server fixture is now session-scoped by default, but we failed to update the docs. 👍

Closing for now, feel free to follow up with further questions though. 👍

Thanks for the response!

That got rid of the initial error, but now I'm getting the same error mentioned on #54 . I'll try the workaround/s mentioned in that thread.