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

在 python 單元測試中模擬類屬性的更好方法

Better way to mock class attribute in python unit test(在 python 單元測試中模擬類屬性的更好方法)
本文介紹了在 python 單元測試中模擬類屬性的更好方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個定義類屬性的基類和一些依賴它的子類,例如

I have a base class that defines a class attribute and some child classes that depend on it, e.g.

class Base(object):
    assignment = dict(a=1, b=2, c=3)

我想用不同的作業對這個類進行單元測試,例如空字典,單項等.當然這非常簡化,不是重構我的類或測試的問題

I want to unittest this class with different assignments, e.g. empty dictionary, single item, etc. This is extremely simplified of course, it's not a matter of refactoring my classes or tests

我想出的(pytest)測試最終是有效的

The (pytest) tests I have come up with, eventually, that work are

from .base import Base

def test_empty(self):
    with mock.patch("base.Base.assignment") as a:
        a.__get__ = mock.Mock(return_value={})
        assert len(Base().assignment.values()) == 0

def test_single(self):
    with mock.patch("base.Base.assignment") as a:
        a.__get__ = mock.Mock(return_value={'a':1})
        assert len(Base().assignment.values()) == 1

這感覺相當復雜和 hacky - 我什至不完全理解它為什么起作用(雖然我熟悉描述符).mock 是否會自動將類屬性轉換為描述符?

This feels rather complicated and hacky - I don't even fully understand why it works (I am familiar with descriptors though). Does mock automagically transform class attributes into descriptors?

感覺更合乎邏輯的解決方案不起作用:

A solution that would feel more logical does not work:

def test_single(self):
    with mock.patch("base.Base") as a:
        a.assignment = mock.PropertyMock(return_value={'a':1})
        assert len(Base().assignment.values()) == 1

或者只是

def test_single(self):
    with mock.patch("base.Base") as a:
        a.assignment = {'a':1}
        assert len(Base().assignment.values()) == 1

我嘗試過的其他變體也不起作用(作業在測試中保持不變).

Other variants that I've tried don't work either (assignments remains unchanged in the test).

模擬類屬性的正確方法是什么?有沒有比上述更好/更容易理解的方法?

What's the proper way to mock a class attribute? Is there a better / more understandable way than the one above?

推薦答案

base.Base.assignment 被簡單地替換為 Mock 對象.您通過添加 __get__ 方法使其成為描述符.

base.Base.assignment is simply replaced with a Mock object. You made it a descriptor by adding a __get__ method.

這有點冗長,有點不必要;您可以直接設置 base.Base.assignment:

It's a little verbose and a little unnecessary; you could simply set base.Base.assignment directly:

def test_empty(self):
    Base.assignment = {}
    assert len(Base().assignment.values()) == 0

當然,這在使用測試并發時不太安全.

This isn't too safe when using test concurrency, of course.

要使用 PropertyMock,我會使用:

with patch('base.Base.assignment', new_callable=PropertyMock) as a:
    a.return_value = {'a': 1}

甚至:

with patch('base.Base.assignment', new_callable=PropertyMock, 
           return_value={'a': 1}):

這篇關于在 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(檢查多個模擬的調用順序)
主站蜘蛛池模板: 欧美国产日韩在线观看 | 久久精彩视频 | 精品国产一区二区三区性色av | 中文字幕人成乱码在线观看 | 久久亚洲高清 | 久久久成人动漫 | 亚洲欧洲精品成人久久奇米网 | a级免费视频| 国产在线二区 | 亚洲欧洲小视频 | 国产一区免费视频 | av天天澡天天爽天天av | 精品欧美乱码久久久久久 | 欧美日韩免费视频 | 中文字幕97 | 天天射天天操天天干 | 69电影网 | 亚洲啪啪一区 | 激情一区 | 亚洲精品9999 | 天天操天天天干 | 成人欧美 | 日韩av在线不卡 | 嫩草视频网 | 国产精品1区2区3区 男女啪啪高潮无遮挡免费动态 | 日韩在线免费视频 | 四虎网站在线观看 | 午夜丁香视频在线观看 | 亚洲免费在线 | 国产亚洲精品综合一区 | 鲁大师一区影视 | 亚洲97 | 亚洲图片一区二区三区 | 中文字幕1区2区3区 亚洲国产成人精品女人久久久 | 香蕉大人久久国产成人av | 国产中文在线观看 | 成人免费视频在线观看 | 久久久久久久综合 | 久久国产精品视频 | 久久精品国产一区二区电影 | 精品国产久 |