Python 参数化中的pytest相关参数

Python 参数化中的pytest相关参数,python,dependencies,pytest,Python,Dependencies,Pytest,我希望一个参数化依赖于较早的参数化: @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

我希望一个参数化依赖于较早的参数化:

@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循环,但这样我就不会“正式”运行那么多测试。

我认为py.test不支持带有in-tests的测试,如果我根据您的示例代码理解正确,那么您只需要测试货币,假设
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