为什么 Pytest 返回集合错误“在 test_billing_month_year:函数不使用参数“日期”,即使使用并定义了日期?
billing_month_year() 函数仅返回当前日期的上个月和上一年。
import datetime as dt
import pytest
from mock import patch
def billing_month_year():
today = dt.datetime.utcnow()
#last month from current date
last_month = today.month - 1 if today.month>1 else 12
#last year from current date
last_month_year = today.year if today.month > last_month else today.year - 1
return last_month, last_month_year
@pytest.mark.parametrize(
'date, expected',
[
#last month in previous year
(dt.datetime(year=2020, month=1, day=21), (12, 2019)),
#last month in current year
(dt.datetime(year=2020, month=2, day=21), (01, 2020)),
]
)
@patch('dt.datetime.utcnow')
def test_billing_month_year(date, expected, mock_utcnow):
mock_utcnow.return_value = date
test = billing_month_year()
assert test == expected
Run Code Online (Sandbox Code Playgroud)
装饰器总是以与添加它们相反的顺序应用,例如,在这种情况下,首先应用patch装饰器,然后pytest.mark.parametrize应用装饰器。这意味着参数应按各自的顺序排列:
@pytest.mark.parametrize(
'date, expected',
[
(dt.datetime(year=2020, month=1, day=21), (12, 2019)),
(dt.datetime(year=2020, month=2, day=21), (01, 2020)),
]
)
@patch('dt.datetime.utcnow')
def test_billing_month_year(mock_utcnow, date, expected):
mock_utcnow.return_value = date
test = billing_month_year()
assert test == expected
Run Code Online (Sandbox Code Playgroud)
修补程序可能也不起作用,请参阅此问题的答案以获取解决方案。
| 归档时间: |
|
| 查看次数: |
1664 次 |
| 最近记录: |