[英]Issue with unit test for tiny Flask app
I have a very primitive Flask application which works as I'm expecting, but I'm failing to write a unit test for it. 我有一个非常原始的Flask应用程序,它可以按我的期望工作,但是我无法为其编写单元测试。 The code of the app is following (i omit insignificant part): 该应用程序的代码如下(我忽略了不重要的部分):
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
The test is: 测试是:
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()
The traceback: 追溯:
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 "<frozen importlib._bootstrap_external>", line 384, in _check_name_wrapper
ImportError: loader for app cannot handle <module 'app' from '/Users/bulrathi/Yandex.Disk.localized/Обучение/Code/Django practice/ailove/keygen/app.py'>
----------------------------------------------------------------------
Ran 1 test in 0.003s
FAILED (errors=1)
Thanks for your time. 谢谢你的时间。
You have a few issues with the posted code (indentation aside): 您在发布的代码中有一些问题(不包括缩进):
First, in tests.py
you import app
and use it, but app
is the module rather than the app
object from app.py
. 首先,在tests.py
您import app
并使用import app
,但是app
是模块而不是app.py
的app
对象。 You should import the actual app
object using 您应该使用导入实际的app
对象
from app import app
and secondly, you are using that app
object (assuming we fix the import) as a parameter to another Flask()
constructor—essentially saying: 其次,您正在使用该app
对象(假设我们已修复导入)作为另一个Flask()
构造函数的参数-本质上说:
app = Flask(Flask(app))
Since we've imported app
from app.py
, we can just use it directly, so we remove the app = Flask(app)
line (and the associated import
statement as we don't need it anymore) and your test file becomes: 由于我们已经从app.py
导入了app
, app.py
我们可以直接使用它,因此我们删除了app = Flask(app)
行(以及不再需要的关联的import
语句),您的测试文件将变为:
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()
You should note also that the from flask import *
form is discouraged in favour of importing specific parts of the module, so 您还应注意,不建议使用from flask import *
形式来支持导入模块的特定部分,因此
from flask import Flask, jsonify
would be better in your app.py
. 在您的app.py
会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.