Skip to main content
 首页 » 编程设计

python之使用 flask 对请求中的嵌套对象进行单元测试

2025年02月15日8傻小

一个单元如何测试用 flask 编写的 REST API 端点,该端点接受请求主体的嵌套字典对象?

这是一个使用 flask 和 webargs 进行输入验证的例子,

from flask import Flask 
from webargs import fields 
from webargs.flaskparser import use_args 
 
app = Flask(__name__) 
 
hello_args = {  
    'a': fields.Nested({'name' : fields.Str()}) 
} 
 
@app.route('/', methods=['POST']) 
@use_args(hello_args) 
def index(args): 
    return 'Hello ' + str(args) 
 
 
def test_app(): 
    app.config['TESTING'] = True 
    test_app = app.test_client(use_cookies=False) 
    test_app.post(data={"a": {"name": "Alice"}}) 
 
 
if __name__ == '__main__': 
    app.run() 

直接使用这个 enpoint 时可以正常工作,

% curl -H "Content-Type: application/json" -X POST \ 
       -d '{"a":{"name": "Alice"}}' http://localhost:5000   
 
Hello {'a': {'name': 'Alice'}}% 

然而,当在单元测试中调用它时,它会在 werkzeug.test.EnvironBuilder 中引发异常,

nosetests /tmp/test.py                                                       
E 
====================================================================== 
ERROR: test.test_app 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
  File "/usr/lib64/python3.4/site-packages/nose/case.py", line 198, in runTest 
    self.test(*self.arg) 
  File "/tmp/test.py", line 26, in test_app 
    test_app.post(data={"a": {"name": "Alice"}}) 
  File "/home/rth/.local/lib64/python3.4/site-packages/werkzeug/test.py", line 788, in post 
    return self.open(*args, **kw) 
  File "/home/rth/.local/lib64/python3.4/site-packages/flask/testing.py", line 103, in open 
    builder = make_test_environ_builder(self.application, *args, **kwargs) 
  File "/home/rth/.local/lib64/python3.4/site-packages/flask/testing.py", line 34, in make_test_environ_builder 
    return EnvironBuilder(path, base_url, *args, **kwargs) 
  File "/home/rth/.local/lib64/python3.4/site-packages/werkzeug/test.py", line 338, in __init__ 
    self._add_file_from_data(key, value) 
  File "/home/rth/.local/lib64/python3.4/site-packages/werkzeug/test.py", line 355, in _add_file_from_data 
    self.files.add_file(key, **value) 
TypeError: add_file() got multiple values for argument 'name' 
 
---------------------------------------------------------------------- 
Ran 1 test in 0.011s 
 
FAILED (errors=1) 

这使用 Python 3.5、flask 0.12 和 webargs 1.5.2。

还在 https://github.com/pallets/flask/issues/2176 提交了一个问题

请您参考如下方法:

看起来尽管使用了 webargs,但输入数据仍必须序列化并明确指定 content_type 才能工作。特别是,更换

test_app.post(data={"a": {"name": "Alice"}}) 

与

test_app.post(data=json.dumps({"a": {"name": "Alice"}}), 
            content_type='application/json') 

修复了这个问题(另见相关的 SO 答案 here )。