Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't pytest find the 'client' fixture when testing a Flask app?

I have a simple flask app, i want to test it using pytest.

my conftest.py:

@pytest.fixture
def app(self):
    app = create_app(TestingConfig)

    return app

my test_view.py:

class TestMainView:

def test_ping(self, client):
    res = client.get(url_for('main.index'))
    assert res.status_code == 200

when i run the test's using pytest it's throwing an error saying:

fixture 'client' not found
>       available fixtures: app, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_xml_property, recwarn, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

i have another testing file where i test default configs and it passes:

class TestTestingClass(object):
app = create_app(TestingConfig)

def test_app_is_test(self):
    ''' test the test enviroment is set okay and works'''
    assert self.app.config['SECRET_KEY'] == 'something hard'
    assert self.app.config['DEBUG'] == True
    assert self.app.config['TESTING'] == True
    assert current_app is not None
    assert self.app.config['SQLALCHEMY_DATABASE_URI'] == 'sqlite:////tmp/testing.db'

edit: i was able to pass an empty test by changing to:

conftest.py:

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

    return app


@pytest.fixture
def client(app):
    return app.test_client()

and my test file to:

def test_index(client):
   assert True

but, still i can't pass the test_index if it was:

def test_index(client):
   assert client.get(url_for('main.index')).status_code == 200

but this time, I'm getting an error stating that says:

RuntimeError: Attempted to generate a URL without the application context being pushed. This has to be executed when application context is available.
like image 508
senaps Avatar asked Jan 29 '23 01:01

senaps


1 Answers

i tried so many different things. i updated pip for this project's virtualenvironment and updated the pytest and pytest-flask. but none did work.

I was able to pass the tests by:

  • removed the pytest and pytest-flask from virtualenvironment.
  • removed my system-wide installations of them.
  • strangely, i had a package named flask-pytest i removed it(in the env)
  • installed them again system-wide.
  • installed them again on virtualenvironment.

i don't know how this had anything with the tests, but it worked. the only thing different is that i didn't installed the said flask-pytest thing.

like image 124
senaps Avatar answered Feb 14 '23 01:02

senaps