我不知道为什么测试不起作用。在我看来,我所做的一切都正确(根据文档)。
在单元测试中,一切正常,但是pytest更加先进,所以我想更改。
import requests
import pytest
def get_historical_currency_rate(currency_code, currency_date) :
url = requests.get(
f'http://api.nbp.pl/api/exchangerates/rates/a/{currency_code}/{currency_date}/?format=json')
r = url.json()
rate = r['rates'][0]['mid']
return round(rate, 2)
@pytest.fixture
def currency_loop_helper():
dates_rate = ['2018-05-25', '2017-02-20', '2013-12-11']
currencies_codes = ['JPY', 'AUD', 'GBP']
expected_rates = [0.03, 3.76, 4.44]
actual_rates = []
for i in range(len(dates_rate)):
result = get_historical_currency_rate(currencies_codes[i], dates_rate[i])
actual_rates.append(result)
actual_list = [(a, b) for a, b in zip(actual_rates, expected_rates)]
return actual_list
@pytest.mark.parametrize('expected, actual', currency_loop_helper)
def test_currency_rate_equal(expected, actual):
assert expected == actual
ERRORS
"...ParameterSet.extract_from(x, force_tuple=force_tuple) for x in argvalues
E TypeError: 'function' object is not iterable
=============================== warnings summary ===============================
/usr/lib/python3/dist-packages/urllib3/util/selectors.py:14
/usr/lib/python3/dist-packages/urllib3/util/selectors.py:14: DeprecationWarning:Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
from collections import namedtuple, Mapping
/usr/lib/python3/dist-packages/socks.py:58
/usr/lib/python3/dist-packages/socks.py:58: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
from collections import Callable
答案 0 :(得分:2)
我相信您不需要使currency_loop_helper
成为一个固定装置。然后,您可以在test_currency_rate_equal
上的参数装饰器中调用该函数。建议的代码更改如下所示:
import requests
import pytest
def get_historical_currency_rate(currency_code, currency_date) :
url = requests.get(
f'http://api.nbp.pl/api/exchangerates/rates/a/{currency_code}/{currency_date}/?format=json')
r = url.json()
rate = r['rates'][0]['mid']
return round(rate, 2)
def currency_loop_helper():
dates_rate = ['2018-05-25', '2017-02-20', '2013-12-11']
currencies_codes = ['JPY', 'AUD', 'GBP']
expected_rates = [0.03, 3.76, 4.44]
actual_rates = []
for i in range(len(dates_rate)):
result = get_historical_currency_rate(currencies_codes[i], dates_rate[i])
actual_rates.append(result)
actual_list = [(a, b) for a, b in zip(actual_rates, expected_rates)]
return actual_list
@pytest.mark.parametrize('expected, actual', currency_loop_helper())
def test_currency_rate_equal(expected, actual):
assert expected == actual