具有范围“类"的 Pytest 固定装置在每种方法上运行 [英] Pytest fixture with scope "class" running on every method

查看:58
本文介绍了具有范围“类"的 Pytest 固定装置在每种方法上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Pytest 创建一个测试环境.这个想法是将测试方法分组到类中.

I'm trying to create a test environment with Pytest. The idea is to group test methods into classes.

对于每个班级/组,我想附加一个将被参数化的 config 固定装置.这样我就可以使用配置 A"运行所有测试,然后使用配置 B"运行所有测试,依此类推.

For every class/group, I want to attach a config fixture that is going to be parametrized. So that I can run all the tests with "configuration A" and then all tests with "configuration B" and so on.

而且,我想要一个 reset 固定装置,它可以在特定方法或类的所有方法之前执行.

But also, I want a reset fixture, that can be executed before specific methods or all methods of a class.

我遇到的问题是,一旦我应用我的 reset 固定装置(到一个方法或整个类),config 固定装置似乎工作在函数作用域而不是类作用域中.因此,一旦我应用了 reset 固定装置,config 固定装置就会在类中的每个方法之前/之后被调用.

The problem I have there is, once I apply my reset fixture (to a method or to a whole class), the config fixture seems to work in the function scope instead of the class scope. So, once I apply the reset fixture, the config fixture is called before/after every method in the class.

以下代码重现了该问题:

The following piece of code reproduces the problem:

import pytest
from pytest import *

@fixture(scope='class')
def config(request):
    print("\nconfiguring with %s" % request.param)
    yield
    print("\ncleaning up config")

@fixture(scope='function')
def reset():
    print("\nreseting")

@mark.parametrize("config", ["config-A", "config-B"], indirect=True)
#@mark.usefixtures("reset")
class TestMoreStuff(object):

    def test_a(self, config):
        pass

    def test_b(self, config):
        pass

    def test_c(self, config):
        pass

该测试显示了 config 夹具应该如何工作,整个类只执行一次.如果取消注释 usefixtures 装饰,您会注意到 config 夹具将在每个测试方法中执行.是否可以在不触发此行为的情况下使用 reset 固定装置?

The test shows how the config fixture should work, being executed only once for the whole class. If you uncomment the usefixtures decoration, you can notice that the config fixture will be executed in every test method. Is it possible to use the reset fixture without triggering this behaviour?

推荐答案

正如我在评论中提到的,这似乎是 Pytest 3.2.5 中的一个错误.

As I mentioned in a comment, that seems to be a bug in Pytest 3.2.5.

有一种解决方法,即强制"参数化的范围.因此,在这种情况下,如果您在 parametrize 装饰器中包含 scope="class",您将获得所需的行为.

There's a workaround, which is to "force" the scope of a parametrization. So, in this case if you include the scope="class" in the parametrize decorator, you get the desired behaviour.

import pytest
from pytest import *

@fixture(scope='class')
def config(request):
    print("\nconfiguring with %s" % request.param)
    yield
    print("\ncleaning up config")

@fixture(scope='function')
def reset():
    print("\nreseting")

@mark.parametrize("config", ["config-A", "config-B"], indirect=True, scope="class")
@mark.usefixtures("reset")
class TestMoreStuff(object):

    def test_a(self, config):
        pass

    def test_b(self, config):
        pass

    def test_c(self, config):
        pass

这篇关于具有范围“类"的 Pytest 固定装置在每种方法上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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