Monday, 5 August 2019

How to Completely Teardown Flask App After Each Test in pytest?

I am testing my Flask app using Pytest. I have 3 files

tests/
--conftest.py
--test_one.py
--test_two.py

If I run test_one.py or test_two.py using the following command, it will work without issue.

python -m pytest tests/test_one.py

The issue arises when I try to run all tests with the following command:

python -m pytest tests

I get this error:

AssertionError: View function mapping is overwriting an existing endpoint function: serve_static

Im not actually surprised by this error since init_app(app) gets called twice (once per file) and I don't ever teardown the actual app.

My question is, Is there a way to completely teardown a flask app and rebuild it for each individual test?

Edit: I am open to alternative ways of setting up a Flask app testing environment, if it would solve this problem (without creating new ones).

conftest.py

import os
import pytest

from app import app, init_app
from config import TestConfig


@pytest.fixture(scope='function')
def client():

    app.config.from_object(TestConfig)

    with app.test_client() as client:
        with app.app_context():
            init_app(app)
        yield client

    try:
        os.remove('tests/testing.db')
    except FileNotFoundError:
        pass



from How to Completely Teardown Flask App After Each Test in pytest?

No comments:

Post a Comment