链接pytest装置 [英] Chaining pytest fixtures

查看:39
本文介绍了链接pytest装置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直找不到神奇的关键字来搜索这个问题,或者在 pytest 文档中找到它.

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

这显然不是 pytest 期望我得到的效果我希望 odd_number 固定装置被传递到 test_all_odds_positive 不包括s.

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.

推荐答案

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

Could you extract the filtering outside the fixtures?

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

这篇关于链接pytest装置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆