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

python:模擬一個模塊

python: mock a module(python:模擬一個模塊)
本文介紹了python:模擬一個模塊的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

是否可以使用 unittest.mock 在 python 中模擬模塊?我有一個名為 config 的模塊,在運行測試時我想通過另一個模塊 test_config 來模擬它.我怎樣才能做到這一點 ?謝謝.

Is it possible to mock a module in python using unittest.mock? I have a module named config, while running tests I want to mock it by another module test_config. how can I do that ? Thanks.

config.py:

CONF_VAR1 = "VAR1"
CONF_VAR2 = "VAR2"

test_config.py:

test_config.py:

CONF_VAR1 = "test_VAR1"
CONF_VAR2 = "test_VAR2" 

所有其他模塊從 config 模塊讀取配置變量.在運行測試時,我希望他們從 test_config 模塊中讀取配置變量.

All other modules read config variables from the config module. While running tests I want them to read config variables from test_config module instead.

推薦答案

如果你總是像這樣訪問 config.py 中的變量:

If you're always accessing the variables in config.py like this:

import config
...
config.VAR1

您可以替換由您實際嘗試測試的任何模塊導入的 config 模塊.所以,如果你正在測試一個名為 foo 的模塊,并且它導入并使用 config,你可以說:

You can replace the config module imported by whatever module you're actually trying to test. So, if you're testing a module called foo, and it imports and uses config, you can say:

from mock import patch
import foo
import config_test
....
with patch('foo.config', new=config_test):
   foo.whatever()

但這實際上并不是全局替換模塊,它只是在 foo 模塊的命名空間內替換它.所以你需要在它導入的任何地方修補它.如果 foo 這樣做而不是 import config ,它也不起作用:

But this isn't actually replacing the module globally, it's only replacing it within the foo module's namespace. So you would need to patch it everywhere it's imported. It also wouldn't work if foo does this instead of import config:

from config import VAR1

你也可以使用 sys.modules 來做到這一點:

You can also mess with sys.modules to do this:

import config_test
import sys
sys.modules["config"] = config_test
# import modules that uses "import config" here, and they'll actually get config_test

但一般來說,弄亂sys.modules并不是一個好主意,我認為這種情況沒有什么不同.我會贊成所有其他建議.

But generally it's not a good idea to mess with sys.modules, and I don't think this case is any different. I would favor all of the other suggestions made over it.

這篇關于python:模擬一個模塊的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How should I verify a log message when testing Python code under nose?(在鼻子下測試 Python 代碼時,我應該如何驗證日志消息?)
Patch __call__ of a function(修補函數的 __call__)
How to call self in a mock method of an object in Python?(如何在 Python 中對象的模擬方法中調用 self?)
Mocking only a single method on an object(僅模擬對象上的單個方法)
Mocking a subprocess call in Python(在 Python 中模擬子進程調用)
Checking call order across multiple mocks(檢查多個模擬的調用順序)
主站蜘蛛池模板: 国产精品自拍视频 | 午夜在线免费观看 | 亚洲精品日韩综合观看成人91 | 9999视频 | 97久久久久久 | 欧美久久一区二区三区 | 在线观看欧美日韩视频 | 嫩草影院网址 | 成人一区二区三区 | 久久伊人精品一区二区三区 | 婷婷综合激情 | 国产精品久久久乱弄 | 国产精品1区2区3区 一区中文字幕 | 中文字幕一区二区三区不卡 | 国产精品18久久久久久白浆动漫 | 国产精品久久片 | 欧美一区二区三区的 | 国产精品a久久久久 | 欧美福利在线 | 日韩在线视频观看 | 老外黄色一级片 | 色久伊人 | 国产日韩一区二区三区 | av色站 | 美女一级a毛片免费观看97 | 韩国成人在线视频 | 成人在线视频免费播放 | 国产片侵犯亲女视频播放 | 久久久www成人免费无遮挡大片 | 黄瓜av | 99视频网 | 日韩免费在线 | 免费看黄色小视频 | 亚洲精品久久久久中文字幕二区 | 日韩a在线 | 福利网站导航 | 日韩中文字幕网 | 中文字幕视频在线免费 | 妖精视频一区二区三区 | 国产精品久久久久久久久图文区 | 日韩欧美一区二区在线播放 |