久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Flask 單元測試中,如何模擬請求全局“g"

in a Flask unit-test, how can I mock objects on the request-global `g` object?(在 Flask 單元測試中,如何模擬請求全局“g對象上的對象?)
本文介紹了在 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"對象上的對象?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 天天天堂 | 欧美精品一区二区三区在线播放 | 羞羞网站免费 | 久免费视频 | 亚洲欧美综合精品久久成人 | 国产精品爱久久久久久久 | 日韩精品1区2区 | 成人欧美一区二区三区在线观看 | 欧美日韩亚洲视频 | 成人黄色av网址 | 亚洲高清视频一区二区 | 亚洲视频中文字幕 | 伊人精品国产 | 欧美电影网 | 国产羞羞视频在线观看 | 在线免费观看黄网 | 国产精品影视在线观看 | 狠狠干av | 欧美黄视频 | 91精品国产综合久久久动漫日韩 | 日韩欧美不卡 | 视频一区中文字幕 | 国产精品久久久久久久久久久久久 | 国内精品久久精品 | 国产亚洲精品久久久久久豆腐 | 亚洲美女一区二区三区 | 成人美女免费网站视频 | 亚洲男人网 | 国产剧情一区 | 中文字幕av一区 | 免费国产视频 | 国产精品揄拍一区二区 | 国产区一区| 色婷婷久久久久swag精品 | 99久久婷婷国产综合精品首页 | 九九热精品在线 | 国产精品福利视频 | 亚洲国产成人在线视频 | 国产精品毛片久久久久久久 | 亚洲精品久久久久avwww潮水 | 日韩视频精品在线 |