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

如何通過 unittest setUp 在 python 中正確使用 mock

How to properly use mock in python with unittest setUp(如何通過 unittest setUp 在 python 中正確使用 mock)
本文介紹了如何通過 unittest setUp 在 python 中正確使用 mock的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

在我嘗試學(xué)習(xí) TDD 的過程中,嘗試學(xué)習(xí)單元測試并在 python 中使用 mock.慢慢掌握它,但不確定我是否正確執(zhí)行此操作.預(yù)先警告:我堅持使用 python 2.4,因為供應(yīng)商 API 是作為預(yù)編譯的 2.4 pyc 文件提供的,所以我使用的是 mock 0.8.0 和 unittest(不是 unittest2)

In my attempt to learn TDD, trying to learn unit testing and using mock with python. Slowly getting the hang of it, but unsure if I'm doing this correctly. Forewarned: I'm stucking using python 2.4 because the vendor API's come as pre-compiled 2.4 pyc files, so I'm using mock 0.8.0 and unittest ( not unittest2 )

鑒于 'mymodule.py' 中的示例代碼

Given this example code in 'mymodule.py'

import ldap

class MyCustomException(Exception):
    pass

class MyClass:
    def __init__(self, server, user, passwd):
        self.ldap = ldap.initialize(server)
        self.user = user
        self.passwd = passwd

    def connect(self):
        try:
            self.ldap.simple_bind_s(self.user, self.passwd)
        except ldap.INVALID_CREDENTIALS:
            # do some stuff
            raise MyCustomException

現(xiàn)在在我的測試用例文件test_myclass.py"中,我想模擬 ldap 對象.ldap.initialize 返回 ldap.ldapobject.SimpleLDAPObject,所以我認為這是我必須模擬出來的方法.

Now in my test case file 'test_myclass.py', I want to mock the ldap object out. ldap.initialize returns the ldap.ldapobject.SimpleLDAPObject, so I figured that'd be the method I'd have to mock out.

import unittest
from ldap import INVALID_CREDENTIALS
from mock import patch, MagicMock
from mymodule import MyClass

class LDAPConnTests(unittest.TestCase):
    @patch('ldap.initialize')
    def setUp(self, mock_obj):
        self.ldapserver = MyClass('myserver','myuser','mypass')
        self.mocked_inst = mock_obj.return_value

    def testRaisesMyCustomException(self):
        self.mocked_inst.simple_bind_s = MagicMock()
        # set our side effect to the ldap exception to raise
        self.mocked_inst.simple_bind_s.side_effect = INVALID_CREDENTIALS
        self.assertRaises(mymodule.MyCustomException, self.ldapserver.connect)

    def testMyNextTestCase(self):
        # blah blah

讓我想到幾個問題:

  1. 看起來對嗎?:)
  2. 這是嘗試模擬在我正在測試的類中實例化的對象的正確方法嗎?
  3. 是否可以在 setUp 上調(diào)用 @patch 裝飾器,否則會導(dǎo)致奇怪的副作用?
  4. 有沒有辦法模擬引發(fā) ldap.INVALID_CREDENTIALS 異常而不必將異常導(dǎo)入我的測試用例文件?
  5. 我應(yīng)該改用 patch.object() 嗎?如果可以,該怎么做?

謝謝.

推薦答案

你可以使用patch() 作為類裝飾器,而不僅僅是作為函數(shù)裝飾器.然后你可以像以前一樣傳入模擬函數(shù):

You can use patch() as a class decorator, not just as a function decorator. You can then pass in the mocked function as before:

@patch('mymodule.SomeClass')
class MyTest(TestCase):

    def test_one(self, MockSomeClass):
        self.assertIs(mymodule.SomeClass, MockSomeClass)

參見:對每個測試方法應(yīng)用相同的補丁(其中還列出了替代方法)

See: Applying the same patch to every test method (which also lists alternatives)

如果您希望對所有測試方法進行修補,那么在 setUp 上以這種方式設(shè)置修補程序更有意義.

It makes more sense to set up the patcher this way on setUp if you want the patching to be done for all the test methods.

這篇關(guān)于如何通過 unittest setUp 在 python 中正確使用 mock的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How should I verify a log message when testing Python code under nose?(在鼻子下測試 Python 代碼時,我應(yīng)該如何驗證日志消息?)
Patch __call__ of a function(修補函數(shù)的 __call__)
How to call self in a mock method of an object in Python?(如何在 Python 中對象的模擬方法中調(diào)用 self?)
Mocking only a single method on an object(僅模擬對象上的單個方法)
Mocking a subprocess call in Python(在 Python 中模擬子進程調(diào)用)
Checking call order across multiple mocks(檢查多個模擬的調(diào)用順序)
主站蜘蛛池模板: www亚洲精品 | 久久伊人国产 | 成人福利在线观看 | 男人添女荫道口图片 | 欧美中文字幕在线观看 | 国产在线观看一区 | 日本国产欧美 | av手机在线观看 | 插插插av| 成人小网站 | 狠狠做深爱婷婷久久综合一区 | 天天干天天色天天射 | 日韩精品视频免费在线观看 | 青青草福利视频 | 日韩av专区 | 日本三级久久 | 天天干天天色 | 一级片大全 | 欧美精品一二三 | 国产成人午夜高潮毛片 | 日本免费在线 | 一级中国毛片 | 成年网站在线观看 | 国产传媒视频在线观看 | 欧美性大战xxxxx久久久 | 中文字幕一区二 | 91亚洲成人 | 免费成人毛片 | 日日夜夜精品免费 | 成人在线视频网 | av大片在线观看 | 综合av网| 中国女人真人一级毛片 | 黄色在线免费观看视频 | 黄色影院在线观看 | 欧美精品www | 亚洲自拍偷拍一区 | 国产理论在线观看 | 久久久久久久久久久国产 | 国产九九热 | 免费观看一级一片 |