in a Flask unit-test, how can I mock objects on the request-global `g` object?(在 Flask 单元测试中,如何模拟请求全局“g对象上的对象?)
问题描述
我有一个烧瓶应用程序,它在 before_filter 中设置数据库连接,非常类似于 这个:
I have a flask application that is setting up a database connection in a before_filter, very similar to this:
@app.before_request
def before_request():
g.db = connect_db()
现在:我正在编写一些单元测试,但我确实不希望它们进入数据库.我想用可以设置期望的模拟对象替换 g.db.
Now: I am writing some unit-tests and I do not want them to hit the database. I want to replace g.db with a mock object that I can set expectations on.
我的测试使用 app.test_client(),正如烧瓶文档 这里.示例测试类似于
My tests are using app.test_client(), as is demonstrated in the flask documentation here. An example test looks something like
def test(self):
response = app.test_client().post('/endpoint', data={..})
self.assertEqual(response.status_code, 200)
...
测试工作并通过,但它们正在访问数据库,正如我所说,我想用模拟对象替换 db 访问.我在 test_client 中看不到任何访问 g 对象或更改 before_filters 的方法.
The tests work and pass, but they are hitting the database and as I said I want to replace db access with mock objects. I do not see any way in test_client to access the g object or alter the before_filters.
推荐答案
这可行
test_app.py
from flask import Flask, g
app = Flask(__name__)
def connect_db():
print 'I ended up inside the actual function'
return object()
@app.before_request
def before_request():
g.db = connect_db()
@app.route('/')
def root():
return 'Hello, World'
test.py
from mock import patch
import unittest
from test_app import app
def not_a_db_hit():
print 'I did not hit the db'
class FlaskTest(unittest.TestCase):
@patch('test_app.connect_db')
def test_root(self, mock_connect_db):
mock_connect_db.side_effect = not_a_db_hit
response = app.test_client().get('/')
self.assertEqual(response.status_code, 200)
if __name__ == '__main__':
unittest.main()
所以这将打印出我没有命中数据库",而不是我最终进入了实际函数".显然,您需要根据实际用例调整模拟.
So this will print out 'I did not hit the db', rather than 'I ended up inside the actual function'. Obviously you'll need to adapt the mocks to your actual use case.
这篇关于在 Flask 单元测试中,如何模拟请求全局“g"对象上的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Flask 单元测试中,如何模拟请求全局“g"
基础教程推荐
- 尝试制作WhatsApp机器人 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
