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

在 Python 中模擬類方法并更改某些對象屬性

Mocking a class method and changing some object attributes in Python(在 Python 中模擬類方法并更改某些對象屬性)
本文介紹了在 Python 中模擬類方法并更改某些對象屬性的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我是 Python 中的新手.我想知道如何在用另一個類方法進行測試時替換(模擬)一個類方法,知道原來的只是改變了 self 的一些屬性而不返回任何值.例如:

I am new to mock in Python. I want to know how to replace (mock) a class method while testing with another one, knowing that the original just changes some attributes of self without returning any value. For example:

def some_method(self):   
    self.x = 4   
    self.y = 6   

所以在這里我不能只更改模擬的 return_value.我試圖定義一個新函數(shù)(應(yīng)該替換原來的函數(shù))并將其作為 side_effect 提供給模擬.但是我怎樣才能讓模擬函數(shù)改變類中對象的屬性.這是我的代碼:

So here I can't just change the return_value of the mock. I tried to define a new function (that should replace the original) and give it as side_effect to the mock. But how can I make the mocking function change attributes of the object in the class. Here is my code:

@patch('path.myClass.some_method')
def test_this(self,someMethod):

    def replacer(self):
        self.x = 5
        self.y = 16

some_method.side_effect = replacer

那么 Python 現(xiàn)在是如何理解替換器的 self 參數(shù)的呢?是測試類的self,還是被測類的對象self?

So how does Python now understands the self argument of replacer? Is that the self of the test class, or the self as the object of the class under test?

推薦答案

如果我不明白您要做什么,請?zhí)崆暗狼福艺J(rèn)為這可能有效:

Apologies in advance if I don't understand what you are trying to do, but I think this might work:

import unittest
from unittest.mock import patch

class MyClass:

    def __init__(self):
        self.x = 0
        self.y = 0

    def some_method(self):   
        self.x = 4   
        self.y = 6    

class OtherClass:

    def other_method(self):
        self.x = 5
        self.y = 16

class MyTestClass(unittest.TestCase):

    @patch('__main__.MyClass.some_method', new=OtherClass.other_method)
    def test_patched(self):
        a = MyClass()
        a.some_method()
        self.assertEqual(a.x, 5)
        self.assertEqual(a.y, 16)

    def test_not_patched(self):
        a = MyClass()
        a.some_method()
        self.assertEqual(a.x, 4)
        self.assertEqual(a.y, 6)

if __name__ == "__main__":
    unittest.main()

這在打補丁的時候用 other_method() 替換了 some_method(),它為屬性 x、y 設(shè)置了不同的值,并且在運行測試時,它給出了結(jié)果:

This replaces some_method() with other_method() when patched, which sets different values for attributes x, y, and when the test is run, it gives the results:

..
----------------------------------------------------------------------
Ran 2 tests in 0.020s

OK

回答有關(guān)如何在不模擬類的情況下在測試函數(shù)中執(zhí)行的問題...

to answer question about how to do inside the test function without mocking a class...

def test_inside_patch(self):
    def othermethod(self):
        self.x = 5
        self.y = 16
    patcher = patch('__main__.MyClass.some_method', new=othermethod)
    patcher.start()
    a = MyClass()
    a.some_method()
    self.assertEqual(a.x, 5)
    self.assertEqual(a.y, 16) 
    patcher.stop()

確保在補丁程序上調(diào)用 start() 和 stop() ,否則您可能會遇到補丁處于活動狀態(tài)而您不希望它處于活動狀態(tài)的情況.請注意,在測試代碼函數(shù)中定義模擬函數(shù),我沒有使用補丁作為裝飾器,因為模擬函數(shù)必須在補丁中使用'new'關(guān)鍵字之前定義.如果你想使用補丁作為裝飾器,你必須在補丁之前的某個地方定義模擬函數(shù),在 MyTestClass 中定義它也可以,但似乎你真的希望在測試函數(shù)代碼中定義模擬函數(shù).

Make sure you call start() and stop() on the patcher otherwise you can get into a situation where the patch is active and you don't want it to be. Note that to define the mock function inside the test code function, I didn't use patch as a decorator, because the mock function has to be defined before using the 'new' keyword in patch. If you want to use patch as a decorator you have to define the mock function someplace before the patch, defining it inside of MyTestClass also works, but it seems you really want to have the mock function defined inside your test function code.

添加了我看到的 4 種方法的摘要...

added summary of 4 ways I see to do this...

# first way uses a class outside MyTest class
class OtherClass:
    def other_method(self):
        ...

class MyTest(unittest.TestCase):

    @patch('path_to_MyClass.some_method', new=OtherClass.other_method)
    def test_1(self)
        ...

    # 2nd way uses class defined inside test class    
    class MyOtherClass:
        def other_method(self):
            ...
    @patch('path_to_MyClass.some_method', new=MyOtherClass.other_method)    
    def test_2(self):
        ...

    # 3rd way uses function defined inside test class but before patch decorator 
    def another_method(self):
        ...
    @patch('path_to_MyClass.some_method', new=another_method)    
    def test_3(self):
        ...

    # 4th way uses function defined inside test function but without a decorator
    def test_4(self):
        def yet_another_method(self):
            ...
        patcher = patch('path_to_MyClass.some_method', new=yet_another_method)
        patcher.start()
        ...
        patcher.stop()

這些都沒有使用副作用,但它們都解決了模擬類方法和更改某些屬性的問題.您選擇哪一種取決于應(yīng)用程序.

None of these uses a side_effect, but they all solve the problem of mocking a class method and changing some attributes. Which one you choose depends on the application.

這篇關(guān)于在 Python 中模擬類方法并更改某些對象屬性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數(shù)點/精度)
Converting Float to Dollars and Cents(將浮點數(shù)轉(zhuǎn)換為美元和美分)
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 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 人操人人干人 | 欧美日韩高清在线一区 | 精品三级在线观看 | 国产一区二区在线视频 | 欧产日产国产精品视频 | 黄网站在线播放 | 91精品国产91久久久久久最新 | 日韩中文字幕在线视频 | 久久伦理中文字幕 | 中文字幕亚洲欧美日韩在线不卡 | 欧美欧美欧美 | www.日韩高清| 日韩欧美三级电影 | 欧美极品视频 | 国产精品自拍视频网站 | 欧美一区二区三区在线观看视频 | 亚洲精品一区二区三区蜜桃久 | 欧美日韩久 | 91在线精品视频 | 国产国产精品久久久久 | 亚洲福利av | av在线天天| 精品二区| 日韩综合在线播放 | 国产成年人小视频 | 久久青青| 国产高清视频在线观看 | 亚洲专区在线 | 国产精品久久久久无码av | 久久久精品网站 | 九九热这里只有精品在线观看 | 亚洲综合在线一区 | 91在线一区二区 | 岛国精品| 一级电影免费看 | 一级免费毛片 | 日本亚洲一区二区 | 狠狠的干 | 国产免费视频 | 美女视频一区二区三区 | 午夜在线精品 |