我希望一个参数化依赖于前一个参数:
@pytest.mark.parametrize("locale_name", LOCALES)
@pytest.mark.parametrize("money_string", ["500{currency}", "500 {currency}"])
@pytest.mark.parametrize("currency", LOCALES['locale_name']['currencies'])
def test_do_stuff(locale_name, money_string, currency):
print(locale_name, money_string, currency)
这里,第三个参数化取决于第一个参数。
我试图通过以下方式将其拆分:
@pytest.mark.parametrize("locale_name", LOCALES)
@pytest.mark.parametrize("money_string", ["500{currency}", "500 {currency}"])
def test_currencies(locale_name, money_string):
# locale is actually also needed as an object, cannot be taken out.
locale = LOCALES[locale_name]
@pytest.mark.parametrize("currency", locale['currencies'])
def test_inner_currencies(currency):
print("this does not run however")
但是内部代码没有运行。我不知道除了使用itertools.product
预先生成对之外,我能做些什么呢?(但这看起来很难看)?
请注意,我可能只有一个for循环,但是我不会“正式”运行尽可能多的测试。
答案 0 :(得分:0)
我不认为py.test在测试中支持测试,如果我根据你的示例代码正确理解,你想要的只是测试货币,假设LOCALES
是一个嵌套的dict,我会做一些事情这种解决测试
import pytest
LOCALES = {"US": {"currencies": "dollar"},
"EU": {"currencies": "euro"},
"IN": {"currencies": "rupees"},
"CH": {"currencies": "yuan"}}
def currencies():
return [(local_name, currency["currencies"]) for local_name, currency in
LOCALES.items()]
@pytest.mark.parametrize("currency", currencies())
@pytest.mark.parametrize("money_string", ["500{currency}", "500 {currency}"])
def test_currencies(currency, money_string):
locale_name, currency = currency
print locale_name, money_string, currency
输出
============================= test session starts ==============================
platform darwin -- Python 2.7.10, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /Users/sanjay/Library/Preferences/PyCharm2016.2/scratches, inifile:
plugins: xdist-1.14
collected 8 items
scratch_11.py EU 500{currency} euro
.CH 500{currency} yuan
.US 500{currency} dollar
.IN 500{currency} rupees
.EU 500 {currency} euro
.CH 500 {currency} yuan
.US 500 {currency} dollar
.IN 500 {currency} rupees
.
=========================== 8 passed in 0.03 seconds ===========================
Process finished with exit code 0