gpt4 book ai didi

python - 链接pytest装置

转载 作者:行者123 更新时间:2023-12-04 16:45:02 25 4
gpt4 key购买 nike

我一直找不到神奇的关键字来谷歌这个问题或在 pytest 中找到它。文档。

我希望能够设置我的测试以将多个 fixture 组合成一个 fixture - 或者从另一个 fixture 反向过滤 fixture 。一个例子会更好地解释它:

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 fixture 被传递到 test_all_odds_positive不包括 None s。

最佳答案

你能在灯具之外提取过滤吗?

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 ============================================

关于python - 链接pytest装置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47696002/

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