我一直无法找到魔术关键词来解决此问题或在pytest
文档中找到它。
我希望能够设置我的测试以将多个灯具组合到一个灯具中 - 或者从另一个灯具反向过滤灯具。一个例子会更好地解释它:
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
这显然不是pytest
期望我获得效果的方式,我希望将odd_number
夹具传递到test_all_odds_positive
以不包含None
。
答案 0 :(得分:0)
你能在灯具之外提取过滤吗?
import pytest
numbers = [0,1,2,3,4,5,6]
@pytest.fixture(params=numbers)
def number(request):
return request.param
def odd_numbers(ns):
return [n for n in ns if n % 2 == 1]
@pytest.fixture(params=odd_numbers(numbers))
def odd_number(request):
return request.param
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
collected 10 items
foo.py::test_all_positive[0] PASSED [ 10%]
foo.py::test_all_positive[1] PASSED [ 20%]
foo.py::test_all_positive[2] PASSED [ 30%]
foo.py::test_all_positive[3] PASSED [ 40%]
foo.py::test_all_positive[4] PASSED [ 50%]
foo.py::test_all_positive[5] PASSED [ 60%]
foo.py::test_all_positive[6] PASSED [ 70%]
foo.py::test_all_odds_positive[1] PASSED [ 80%]
foo.py::test_all_odds_positive[3] PASSED [ 90%]
foo.py::test_all_odds_positive[5] PASSED [100%]
============================================ 10 passed in 0.01s ============================================