gpt4 book ai didi

python - 尝试运行测试时 Pytest 给我一个错误

转载 作者:行者123 更新时间:2023-12-01 00:42:57 24 4
gpt4 key购买 nike

不知道为什么测试不起作用。在我看来,我做的一切都是正确的(根据文档)。

在unittest中一切正常,但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

最佳答案

我相信您不需要将 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

关于python - 尝试运行测试时 Pytest 给我一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57208276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com