問題描述
我有一個燒瓶應用程序,它在 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"對象上的對象?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!