Issue with unit test for tiny Flask app
<p>I have a very primitive Flask application which works as I'm expecting, but I'm failing to write a unit test for it. The code of the app is following (i omit insignificant part):</p>

<pre><code>app.py

from flask import *
import random
import string

app = Flask(__name__)
keys = []
app.testing = True

@app.route('/keygen/api/keys', methods=['POST'])
def create():
    symbol = string.ascii_letters + string.digits
    key = ''.join(random.choice(symbol) for _ in range(4))
    key_instance = {'key': key, 'is_used': False}
    keys.append(key_instance)

    return jsonify({'keys': keys}), 201
</code></pre>

<p>The test is:</p>

<pre><code>tests.py

import unittest
from flask import *
import app

class TestCase(unittest.TestCase):
    def test_number_one(self):
        test_app = Flask(app)
        with test_app.test_client() as client:
            rv = client.post('/keygen/api/keys')

        ...something...

    if __name__ == '__main__':
    unittest.main()
</code></pre>

<p>The traceback:</p>

<pre><code>ERROR: test_number_one (__main__.TestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests.py", line 12, in test_number_one
    test_app = Flask(app)
  File "/Users/bulrathi/Yandex.Disk.localized/Virtualenvs/ailove/lib/python3.5/site-packages/flask/app.py", line 346, in __init__
    root_path=root_path)
  File "/Users/bulrathi/Yandex.Disk.localized/Virtualenvs/ailove/lib/python3.5/site-packages/flask/helpers.py", line 807, in __init__
    root_path = get_root_path(self.import_name)
  File "/Users/bulrathi/Yandex.Disk.localized/Virtualenvs/ailove/lib/python3.5/site-packages/flask/helpers.py", line 668, in get_root_path
    filepath = loader.get_filename(import_name)
  File "&lt;frozen importlib._bootstrap_external&gt;", line 384, in _check_name_wrapper
ImportError: loader for app cannot handle &lt;module 'app' from '/Users/bulrathi/Yandex.Disk.localized/ÐÐ±ÑÑÐµÐ½Ð¸Ðµ/Code/Django practice/ailove/keygen/app.py'&gt;

----------------------------------------------------------------------
Ran 1 test in 0.003s

FAILED (errors=1)
</code></pre>

<p>Thanks for your time.</p>

<p>You have a few issues with the posted code (indentation aside):</p>

<p>First, in <code>tests.py</code> you <code>import app</code> and use it, but <code>app</code> is the module rather than the <code>app</code> object from <code>app.py</code>. You should import the actual <code>app</code> object using</p>

<pre><code>from app import app
</code></pre>

<p>and secondly, you are using that <code>app</code> object (assuming we fix the import) as a parameter to another <code>Flask()</code> constructorâessentially saying:</p>

<pre><code>app = Flask(Flask(app))
</code></pre>

<p>Since we've imported <code>app</code> from <code>app.py</code>, we can just use it directly, so we remove the <code>app = Flask(app)</code> line (and the associated <code>import</code> statement as we don't need it anymore) and your test file becomes:</p>

<pre><code>import unittest
from app import app

class TestCase(unittest.TestCase):
    def test_number_one(self):
        with app.test_client() as client:
        rv = client.post('/keygen/api/keys')

    ...something...

if __name__ == '__main__':
    unittest.main()
</code></pre>

<p>You should note also that the <code>from flask import *</code> form is discouraged in favour of importing specific parts of the module, so</p>

<pre><code>from flask import Flask, jsonify
</code></pre>

<p>would be better in your <code>app.py</code>.</p>

python unit-testing flask