久久久久久久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 | 国产精品久久久久一区二区三区 | 亚洲二区精品 | 国产xxx在线观看 | 在线日韩 | 国产精品久久精品 | 久久精品毛片 | 精品一区二区视频 | 国产精品小视频在线观看 | 国产一区二区不卡 | 午夜在线精品 | 日本在线免费观看 | 麻豆天堂 | 国产精品一区二区三区久久 | 久久在线精品 | 天天综合日日夜夜 | 免费色网址 | 国产精品久久午夜夜伦鲁鲁 | 在线观看中文字幕视频 | 97精品一区二区 | 99这里只有精品视频 | 人人看人人爽 | 99精品亚洲国产精品久久不卡 | 免费看国产精品视频 | 国产91精品久久久久久久网曝门 | 色精品| 国产一级毛片精品完整视频版 | 日韩欧美视频在线 | 美美女高清毛片视频免费观看 | 性色综合 | 中文字幕在线一区 | 天天射天天操天天干 |