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

在 Python 中模擬方法調(diào)用

Mocking Method Calls In Python(在 Python 中模擬方法調(diào)用)
本文介紹了在 Python 中模擬方法調(diào)用的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我一直在搜索堆棧交換并在網(wǎng)絡(luò)上搜索如何做到這一點(diǎn),但我不明白如何模擬方法的行為.我正在嘗試為我的自定義類(lèi)模擬 openpyxl 行為和行為.這是我的嘗試:

導(dǎo)入單元測(cè)試從 unittest.mock 導(dǎo)入 MagicMock導(dǎo)入openpyxl從 MyPythonFile 導(dǎo)入 MyClass類(lèi)TestMyClass(unittest.TestCase):def test_myclass(self):我的班級(jí) = 我的班級(jí)()wb = openpyxl.workbook()ws = openpyxl.worksheet()wbPath = 'wbPath'openpyxl.load_workbook(wbPath, data_only = True) = MagicMock(return_value=wb)

當(dāng)我嘗試最后一行時(shí),我收到錯(cuò)誤無(wú)法分配給函數(shù)調(diào)用".我需要使用 patch.object('openpyxl','load_workbook') 嗎?我習(xí)慣用 Groovy 在 Java 中進(jìn)行模擬,它非常簡(jiǎn)單.

*想根據(jù)@alxwrd 的響應(yīng)添加測(cè)試的最終版本

導(dǎo)入單元測(cè)試從 unittest.mock 導(dǎo)入 MagicMock導(dǎo)入openpyxl導(dǎo)入配置解析器從 MyPythonFile 導(dǎo)入 MyClass類(lèi)TestMyClass(unittest.TestCase):def test_myclass(self):我的班級(jí) = 我的班級(jí)()wb = openpyxl.workbook()ws = openpyxl.worksheet()config = configparser.ConfigParser()openpyxl.load_workbook = MagicMock(return_value=wb)wb.get_sheet_by_name = MagicMock(return_value=ws)config.sections() = MagicMock(return_value=['Section1'])config.get = MagicMock(side_effect=['Value1','Value2'])

請(qǐng)注意 config.get 使用 side_effect 參數(shù)提供多個(gè)返回,因此如果在代碼中調(diào)用 config.get() 一次,它會(huì)返回 'Value1' 以及何時(shí)config.get() 在返回 'Value2' 時(shí)被第二次調(diào)用.

解決方案

你不能重寫(xiě)函數(shù)調(diào)用,但你可以重寫(xiě)函數(shù)本身.

來(lái)自 docs:

<塊引用>

>>>從 unittest.mock 導(dǎo)入 MagicMock>>>東西=生產(chǎn)類(lèi)()>>>thing.method = MagicMock(return_value=3)>>>thing.method(3, 4, 5, key='value')3>>>thing.method.assert_call_with(3, 4, 5, key='value')

所以在你的情況下:

openpyxl.load_workbook = MagicMock(return_value=wb)

I have been searching stack exchange and around the web for how to do this, but I cannot understand how to mock behaviors for methods. I am trying to mock openpyxl behaviors and behaviors for my custom class. Here is my attempt:

import unittest
from unittest.mock import MagicMock
import openpyxl 
from MyPythonFile import MyClass

class TestMyClass(unittest.TestCase):
  def test_myclass(self):
    myclass = MyClass()
    wb = openpyxl.workbook()
    ws = openpyxl.worksheet()
    wbPath = 'wbPath'

    openpyxl.load_workbook(wbPath, data_only = True) = MagicMock(return_value=wb)

When I try the final line I get the error "can't assign to function call". Do I need to use patch.object('openpyxl','load_workbook')? I am used to mocking in Java with Groovy and it's pretty straightforward.

*Edit: wanted to add the finalized version of the test based on the response from @alxwrd

import unittest
from unittest.mock import MagicMock
import openpyxl 
import configparser
from MyPythonFile import MyClass

class TestMyClass(unittest.TestCase):
  def test_myclass(self):
    myclass = MyClass()
    wb = openpyxl.workbook()
    ws = openpyxl.worksheet()
    config = configparser.ConfigParser()

    openpyxl.load_workbook = MagicMock(return_value=wb)
    wb.get_sheet_by_name = MagicMock(return_value=ws)

    config.sections() = MagicMock(return_value=['Section1'])
    config.get = MagicMock(side_effect=['Value1','Value2']) 

Notice that config.get gives multiple returns with the side_effect parameter, so if config.get() is called once in the code it returns 'Value1' and when config.get() is called a second time it returns 'Value2'.

解決方案

You can't override a function call, but you can override the function itself.

From the docs:

>>> from unittest.mock import MagicMock
>>> thing = ProductionClass()
>>> thing.method = MagicMock(return_value=3)
>>> thing.method(3, 4, 5, key='value')
3
>>> thing.method.assert_called_with(3, 4, 5, key='value')

So in your case:

openpyxl.load_workbook = MagicMock(return_value=wb)

這篇關(guān)于在 Python 中模擬方法調(diào)用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點(diǎn)小數(shù)點(diǎn)/精度)
Converting Float to Dollars and Cents(將浮點(diǎn)數(shù)轉(zhuǎn)換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計(jì)算可以返回 NaN?)
Python float to ratio(Python浮動(dòng)比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 日韩精品免费一区二区在线观看 | 国产亚洲一区二区三区在线观看 | 99精品国产一区二区青青牛奶 | 天天操天天拍 | 天天色天天 | 国产精品久久久久久久久久久久久久 | 精品欧美激情精品一区 | 国产成人综合在线 | 国产一二三区免费视频 | 午夜影院操| 中文字幕二区三区 | 国产精品电影网 | 成人免费精品视频 | 7777精品伊人久久精品影视 | 国产大片黄色 | 亚洲免费人成在线视频观看 | 日韩爱爱网站 | 国产精品美女久久久久久久网站 | 欧美国产在线一区 | 久久成人精品视频 | 在线观看国产视频 | 99这里只有精品视频 | 91精品综合久久久久久五月天 | 自拍偷拍亚洲欧美 | 久久久这里只有17精品 | 欧美日韩在线免费 | 一区二区影视 | 亚洲精品久久久久久久久久久 | 国产特级毛片aaaaaa喷潮 | 中文字幕亚洲区一区二 | 日日日日操 | 婷婷综合激情 | 九九热最新地址 | 另类亚洲视频 | 999久久久 | 精品国产一区二区三区久久影院 | 国产精品久久久久久久三级 | 久久精品成人 | 国产一区二区三区在线看 | 精品国产成人 | 午夜小电影 |