title: How to Completely Teardown Flask App After Each Test in pytest?
body: <p>I am testing my Flask app using Pytest. I have 3 files</p>
  
  <pre><code>tests/
  --conftest.py
  --test_one.py
  --test_two.py
  </code></pre>
  
  <p>If I run <code>test_one.py</code> <strong>or</strong> <code>test_two.py</code> using the following command, it will work without issue.</p>
  
  <pre><code>python -m pytest tests/test_one.py
  </code></pre>
  
  <p>The issue arises when I try to run all tests with the following command:</p>
  
  <pre><code>python -m pytest tests
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>AssertionError: View function mapping is overwriting an existing endpoint function: serve_static
  </code></pre>
  
  <p>Im not actually surprised by this error since <code>init_app(app)</code> gets called twice (once per file) and I don't ever teardown the actual <code>app</code>.</p>
  
  <p>My question is, <strong>Is there a way to completely teardown a flask app and rebuild it for each individual test?</strong> I am looking for the actual commands to do the tearing down (i.e. <code>app.teardown()</code>)</p>
  
  <p><strong>Edit:</strong> I am open to alternative ways of setting up a Flask app testing environment, if it would solve this problem (without creating new ones).</p>
  
  <p><strong>Edit 2:</strong> I found <a href="https://stackoverflow.com/questions/41171254/flask-unit-testing-how-to-reset-app-url-map-for-each-test?rq=1">this</a> question. It is similar and would solve my problem but it involves importing inside a function. I suspect there has to be a better way.</p>
  
  <p><strong>conftest.py</strong></p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>app/__init__.py</strong></p>
  
  <pre><code>app = Flask(__name__)
  app.url_map._rules.clear()
  db = SQLAlchemy(app)
  migrate = Migrate(app, db)
  
  def init_app(app):
      ...
  </code></pre>