Chaining pytest fixtures

杀马特。学长 韩版系。学妹 提交于 2020-05-13 18:09:37

问题


I've been unable to find the magic keywords to google this problem or find it in the pytest documentation.

I'm looking to be able to setup my tests to combine multiple fixtures into a single fixture - or inversely filter fixtures from another fixture. An example will explain it much better:

import pytest

@pytest.fixture(params=[0,1,2,3,4,5,6])
def number(request):
    return request.param

@pytest.fixture()
def odd_number(number):
    if number % 2 == 1:
        return number
    else:
        return None  # Skip (or some kid of filter)

def test_all_positive(number):  # want to be called with 0, 1, 2, 3, 4, 5, and 6
    assert number >= 0

def test_all_odds_positive(odd_number):  # want to be called with 1, 3, and 5
    assert odd_number >= 0

This is clearly not the the way pytest expects me to get the effect I want the odd_number fixture being passed into test_all_odds_positive to not include Nones.

来源:https://stackoverflow.com/questions/47696002/chaining-pytest-fixtures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!