在不更改方法工作原理的情况下修补方法? [英] Patching a method without changing how the method works?
问题描述
我正在尝试测试是否使用某些值调用了pandas方法.
I'm trying to test that a pandas method gets called with some values.
但是,仅通过应用@patch装饰器会导致修补的方法在熊猫中抛出ValueError
,而实际方法却没有.我只是想测试Stock.calc_sma
正在调用基础pandas.rolling_mean
函数.
However, just by applying a @patch decorator causes the patched method to throw a ValueError
within pandas, when the actual method does not. I'm just trying to test that Stock.calc_sma
is calling the underlying pandas.rolling_mean
function.
我假设@patch
装饰器基本上在我正在修补的东西中添加了一些魔术"方法,使我可以检查该函数是否被调用.如果是这种情况,为什么pandas.rolling_mean
函数无论是否已修补与未修补,其行为都不相同?
I'm under the assumption that the @patch
decorator basically adds some "magic" methods to the thing I'm patching that allow me to check if the function was called. If this is the case, why doesn't the pandas.rolling_mean
function behave the same whether it's patched vs. not patched?
app/models.py
app/models.py
import pandas as pd
class Stock: # i've excluded a bunch of class methods, including the one that sets self.data, which is a DataFrame of stock prices.
def calc_sma(self, num_days)
if self.data.shape[0] > num_days: # Stock.data holds a DataFrame of stock prices
column_title = 'sma' + str(num_days)
self.data[column_title] = pd.rolling_mean(self.data['Adj Close'], num_days)
app/tests/TestStockModel.py
app/tests/TestStockModel.py
def setUp(self):
self.stock = MagicMock(Stock)
self.stock.ticker = "AAPL"
self.stock.data = DataFrame(aapl_test_data.data)
@patch('app.models.pd.rolling_mean')
def test_calc_sma(self, patched_rolling_mean):
Stock.calc_sma(self.stock, 3)
assert(isinstance(self.stock.data['sma3'], Series))
patched_rolling_mean.assert_any_call()
错误:test_calc_sma(TestStockModel.TestStockModel)
Traceback (most recent call last):
File "/Users/grant/Code/python/chartflux/env/lib/python2.7/site-packages/mock.py", line 1201, in patched
return func(*args, **keywargs)
File "/Users/grant/Code/python/chartflux/app/tests/TestStockModel.py", line 26, in test_calc_sma
Stock.calc_sma(self.stock, 3)
File "/Users/grant/Code/python/chartflux/app/models.py", line 27, in calc_sma
self.data[column_title] = pd.rolling_mean(self.data['Adj Close'], num_days)
File "/Users/grant/Code/python/chartflux/env/lib/python2.7/site-packages/pandas/core/frame.py", line 1887, in __setitem__
self._set_item(key, value)
File "/Users/grant/Code/python/chartflux/env/lib/python2.7/site-packages/pandas/core/frame.py", line 1967, in _set_item
value = self._sanitize_column(key, value)
File "/Users/grant/Code/python/chartflux/env/lib/python2.7/site-packages/pandas/core/frame.py", line 2017, in _sanitize_column
raise ValueError('Length of values does not match length of '
ValueError: Length of values does not match length of index
推荐答案
>>> import os
>>> os.getcwd()
'/'
>>> from unittest.mock import patch
>>> with patch('os.getcwd'):
... print(os.getcwd)
... print(os.getcwd())
... print(len(os.getcwd()))
...
<MagicMock name='getcwd' id='4472112296'>
<MagicMock name='getcwd()' id='4472136928'>
0
默认情况下,patch
用真正的通用模拟对象替换事物.如您所见,调用该模拟只返回另一个模拟.即使替换的对象没有len
,它的len
也为0.它的属性也是通用模拟.
By default patch
replaces things with really generic mock objects. As you can see, calling the mock just returns another mock. It has a len
of 0 even if the replaced object wouldn't have a len
. Its attributes are also generic mocks.
因此,模拟行为需要一些额外的参数,例如:
So to simulate behavior requires things extra arguments like:
>>> with patch('os.getcwd', return_value='/a/wonderful/place'):
... os.getcwd()
...
'/a/wonderful/place'
或通过":
>>> _cwd = os.getcwd
>>> with patch('os.getcwd') as p:
... p.side_effect = lambda: _cwd()
... print(os.getcwd())
...
/
https://docs.python中也有类似的示例. org/3.5/library/unittest.mock-examples.html
这篇关于在不更改方法工作原理的情况下修补方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!