久久久久久久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 的意思不同)
主站蜘蛛池模板: 91精品成人久久 | 在线国产一区 | 亚洲人成人一区二区在线观看 | 成人精品一区二区三区 | 中文字幕亚洲视频 | 国产中文区二幕区2012 | 中文字幕一区二区不卡 | 久久精品一区二区 | 中文字幕在线一区二区三区 | 日本一区二区不卡 | 欧美视频在线一区 | 欧美精品一区二区三区四区五区 | 亚卅毛片| 性一交一乱一伦视频免费观看 | 亚洲一区二区三区四区五区午夜 | 国产欧美在线一区 | 亚洲国产中文字幕 | 国产一区二区三区视频 | 国产区精品视频 | 一区二区三区av | 国产成人精品一区二区三区在线观看 | 亚洲一区二区三区久久久 | 欧美在线二区 | 91在线精品视频 | 成人免费在线 | 亚洲综合无码一区二区 | 国产精品久久欧美久久一区 | 国产精品二区三区 | 亚洲一区二区三区在线播放 | 99久久久久久久久 | 久久精品免费观看 | 国产情品| 中文字幕高清免费日韩视频在线 | 色悠悠久| 日本在线网站 | 中文字幕在线一区二区三区 | 日日夜夜精品视频 | av三级在线观看 | 国产激情自拍视频 | 三区四区在线观看 | 国产麻豆乱码精品一区二区三区 |