久久久久久久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 的意思不同)
主站蜘蛛池模板: 久草精品视频 | 九九九视频精品 | 久久久国产视频 | 久久久久亚洲精品中文字幕 | 99re国产视频| 成人免费视频网站在线观看 | 国产精品久久av | 成人在线中文 | 欧美高清hd | 国产精品无码专区在线观看 | 久久久久久久久久久久久九 | 亚洲人成一区二区三区性色 | 国产午夜精品一区二区三区 | 亚洲欧美日韩中文在线 | 婷婷一级片 | 天久久 | 成人欧美一区二区三区在线播放 | 亚洲午夜精品一区二区三区他趣 | 色毛片 | 欧美精品一区二区免费视频 | 久久久网 | 精品视频在线免费观看 | 粉色午夜视频 | 免费观看黄色一级片 | 精品一二三区 | 日韩一二三区 | 亚洲国产精品一区二区久久 | 欧美性久久| 毛片在线免费 | 九九国产| 一区日韩| 国产精品免费一区二区三区四区 | h视频在线免费观看 | 欧美日韩精品一区二区天天拍 | 精品欧美一区二区中文字幕视频 | 91精品国产综合久久久久久首页 | av免费看片 | 成人福利网站 | 国产在线一区二区 | 日韩电影一区 | 中文字幕一区二区三区不卡在线 |