将夹具传递给 pytest 中的测试类 [英] pass fixture to test class in pytest

查看:24
本文介绍了将夹具传递给 pytest 中的测试类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下伪代码演示我的问题:

Consider the following pseudocode demonstrating my question:

import pytest


@pytest.fixture
def param1():
    # return smth
    yield "wilma"


@pytest.fixture
def param2():
    # return smth
    yield "fred"


@pytest.fixture
def bar(param1, param2):
    #do smth
    return [Bar(param1, param2), Bar(param1, param2)]


@pytest.fixture
def first_bar(bar):
    return bar[0]


class Test_first_bar:

    # FIXME: how do I do that?
    #def setup_smth???(self, first_bar):
    #    self.bar = first_bar


    def test_first_bar_has_wilma(self):
        # some meaningful check number 1
        assert self.bar.wilma == "wilma"


    def test_first_bar_some_other_check(self):
        # some meaningful check number 2
        assert self.bar.fred == "fred"

基本上我想将 first_bar 固定装置传递给我的 Test_first_bar 类,以便在其所有测试方法中重用该对象.遇到这种情况我该怎么办?

Basically I want to pass first_bar fixture to my Test_first_bar class in order to reuse that object in all its test methods. How should I go about dealing with such situation?

Python 3,如果这很重要的话.

Python 3, if that matters.

推荐答案

在这里,您可以将您的装置定义为 autouse.它将为您班级的所有测试自动调用.在这里,我无法理解什么是[Bar(param1, param2), Bar(param1, param2)].好吧,这不是重点,如果其余代码工作正常,那么您可以尝试以下解决方案.我已经用静态变量替换了代码,以验证它是否正常工作,并且在我的最后工作正常.

Here, you can define your fixture as autouse. Which would be called automatically for all the tests of your class. Here, I cant understand what is [Bar(param1, param2), Bar(param1, param2)]. Well, that's not the point, if rest of the code is working fine than you can try the below solution. I have replaced the code with static variables to verify if it's working or not and it's working fine at my end.

import pytest

@pytest.fixture
def param1():
    # return smth
    yield "wilma"


@pytest.fixture
def param2():
    # return smth
    yield "fred"


@pytest.fixture
def bar(param1, param2):
    # do smth
    return [Bar(param1, param2), Bar(param1, param2)]


@pytest.fixture(scope='function', autouse=True)
def first_bar(bar, request):
    request.instance.bar = bar[0]

class Test_first_bar:

    def test_first_bar_has_wilma(self,request):
        print request.instance.bar

    def test_first_bar_some_other_check(self,request):
        print request.instance.bar

如果你不想将fixture设为autouse,那么你可以在测试之前调用它.喜欢,

And if you do not want to make fixture as autouse, than you can call it before your test. Like,

import pytest

@pytest.fixture
def param1():
    # return smth
    yield "wilma"


@pytest.fixture
def param2():
    # return smth
    yield "fred"


@pytest.fixture
def bar(param1, param2):
    # do smth
    return [Bar(param1, param2), Bar(param1, param2)]


@pytest.fixture(scope='function')
def first_bar(bar, request):
    request.instance.bar = bar[0]

class Test_first_bar:

    @pytest.mark.usefixtures("first_bar")
    def test_first_bar_has_wilma(self,request):
        print request.instance.bar

    @pytest.mark.usefixtures("first_bar")
    def test_first_bar_some_other_check(self,request):
        print request.instance.bar

这篇关于将夹具传递给 pytest 中的测试类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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