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

一個模擬/存根 python 模塊如何像 urllib

How can one mock/stub python module like urllib(一個模擬/存根 python 模塊如何像 urllib)
本文介紹了一個模擬/存根 python 模塊如何像 urllib的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要測試一個需要使用 urllib.urlopen 查詢外部服務器上的頁面的函數(它也使用 urllib.urlencode).服務器可能已關閉,頁面可能會更改;我不能依賴它進行測試.

I need to test a function that needs to query a page on an external server using urllib.urlopen (it also uses urllib.urlencode). The server could be down, the page could change; I can't rely on it for a test.

控制 urllib.urlopen 返回什么的最佳方法是什么?

What is the best way to control what urllib.urlopen returns?

推薦答案

另一個簡單的方法是讓你的測試覆蓋 urllib 的 urlopen() 函數.例如,如果您的模塊有

Another simple approach is to have your test override urllib's urlopen() function. For example, if your module has

import urllib

def some_function_that_uses_urllib():
    ...
    urllib.urlopen()
    ...

你可以這樣定義你的測試:

You could define your test like this:

import mymodule

def dummy_urlopen(url):
    ...

mymodule.urllib.urlopen = dummy_urlopen

然后,當您的測試調用 mymodule 中的函數時,將調用 dummy_urlopen() 而不是真正的 urlopen().像 Python 這樣的動態語言使得為測試提取方法和類變得非常容易.

Then, when your tests invoke functions in mymodule, dummy_urlopen() will be called instead of the real urlopen(). Dynamic languages like Python make it super easy to stub out methods and classes for testing.

請參閱我在 http://softwarecorner.wordpress.com/ 上的博客文章,了解有關存根的更多信息測試的依賴項.

See my blog posts at http://softwarecorner.wordpress.com/ for more information about stubbing out dependencies for tests.

這篇關于一個模擬/存根 python 模塊如何像 urllib的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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(檢查多個模擬的調用順序)
主站蜘蛛池模板: 午夜在线免费观看 | 一本色道久久综合亚洲精品酒店 | 黄网站免费大全入口 | www.国产精品.com | 韩国精品一区二区 | 国产欧美日本 | 激情播播网| 国产中文在线观看 | 日本精品在线视频 | 国产精品96 | 国内精品在线播放 | 精品久久久一区二区 | 男女啪啪网站 | 国产做受视频 | 国产中文字幕在线 | 亚洲国产精品一区 | 99国产精品99久久久久久 | 毛片网站免费观看 | 国产精品aaa | 在线视频日本 | 欧美视频一区二区 | 久久国产综合 | 成人午夜又粗又硬又大 | 天堂中文字幕免费一区 | 国产日韩免费 | 欧美日韩毛片 | 久久久网 | 中文字幕一区二 | 日韩午夜在线 | 日韩高清精品免费观看 | 99久久婷婷国产综合精品草原 | 国产丝袜视频 | 无遮挡毛片 | 国产高清视频在线 | 久久机热| 日韩一区二区三区在线 | 国产精品久久久久久久久 | 狠狠网| 69er小视频| 91午夜精品亚洲一区二区三区 | 狠狠做 |