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

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

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

問題描述

我是 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.我試圖定義一個新函數(應該替換原來的函數)并將其作為 side_effect 提供給模擬.但是我怎樣才能讓模擬函數改變類中對象的屬性.這是我的代碼:

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 現在是如何理解替換器的 self 參數的呢?是測試類的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?

推薦答案

如果我不明白您要做什么,請提前道歉,但我認為這可能有效:

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 設置了不同的值,并且在運行測試時,它給出了結果:

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

回答有關如何在不模擬類的情況下在測試函數中執行的問題...

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()

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

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()

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

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.

這篇關于在 Python 中模擬類方法并更改某些對象屬性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 的意思不同)
主站蜘蛛池模板: 日韩在线免费 | 亚洲在线一区 | 999国产精品视频 | 国产精品国产 | 成人综合在线视频 | 99只有精品 | 亚洲精品久久视频 | 久久精品国产一区 | 日本三级电影在线看 | 色妞av| 国产精品久久久久久妇女6080 | 亚洲成人在线免费 | 少妇精品亚洲一区二区成人 | 国产成人精品免高潮在线观看 | 成人亚洲一区 | 日本不卡免费新一二三区 | 成人午夜免费视频 | 日韩电影一区二区三区 | 国产精品一区久久久久 | 欧美一二三区 | 国产亚洲一区二区三区在线观看 | 粉嫩国产精品一区二区在线观看 | 日韩在线中文字幕 | 日韩一区和二区 | 亚洲福利在线视频 | 欧美精品在欧美一区二区 | 综合网视频 | 欧美日韩亚洲一区二区 | 在线午夜 | 久久人人爽人人爽 | 国产精品久久久久无码av | 精品日韩一区二区 | 久久精品视频一区二区三区 | 黄色网址免费在线观看 | 日本精品在线观看 | 99久久婷婷| 久久精品小视频 | 一级一级毛片免费看 | 国产精品欧美精品 | 日韩一区二区三区在线观看 | 色资源站|