samedi 18 janvier 2020

How to pytest a function that reads from a db?

I have a Flask app with a function that is something like this:

def get_user(user_id):
    user = User.query.filter(id == user_id).first()
    data = user.to_dict()
    return data

I am using pytest and I have two fixtures:

@pytest.fixture(scope='module')
def test_app():
    app = create_app()
    app.config.from_object('application.config.TestingConfig')
    with app.app_context():
        yield app


@pytest.fixture(scope='module')
def test_database():
    db.create_all()
    yield db
    db.session.remove()
    db.drop_all()

In my test file, I am importing the get_user() function. And in the test function I am passing the test_app and test_database fixtures.

I add a user to the test database successfully, but when I call the function it tries to access the dev database, rather than the testing database.

I don't know how to get the function to connect to the test_database. Can someone point me in the right direction.

Aucun commentaire:

Enregistrer un commentaire