探索pytest的fixture(下)

这篇文章接着上一篇《探索pytest的fixture(上)》的内容讲。

fixture的网络图片2

使用fixture函数的fixture

我们不仅可以在测试函数中使用fixture,而且fixture函数也可以使用其他fixture,这有助于fixture的模块化设计,并允许在许多项目中重新使用框架特定的fixture。例如,我们可以扩展前面的例子,并实例化一个app对象,我们把已经定义好的smtp资源粘贴到其中,新建一个test_appsetup.py文件,输入以下代码:

import pytest

class App(object):
    def __init__(self, smtp):
        self.smtp = smtp

@pytest.fixture(scope="module")
def app(smtp):
    return App(smtp)

def test_smtp_exists(app):
    assert app.smtp

在这里,我们声明一个app fixture,用来接收之前定义的smtp fixture,并用它实例化一个App对象,让我们来运行它:

test_appsetup.py文件执行截图

由于smtp的参数化,测试将运行两次不同的App实例和各自的smtp服务器。pytest将完全分析fixture依赖关系图,因此app fixture不需要知道smtp参数化。

还要注意一下的是,app fixture具有一个module(模块)范围,并使用module(模块)范围的smtp fixture。如果smtp被缓存在一个session(会话)范围内,这个例子仍然可以工作,fixture使用更大范围内的fixture是好的,但是不能反过来,session(会话)范围的fixture不能以有意义的方式使用module(模块)范围的fixture。

通过fixture实例自动分组测试

pytest在测试运行期间会最小化活动fixture的数量,如果我们有一个参数化的fixture,那么所有使用它的测试将首先执行一个实例,然后在下一个fixture实例被创建之前调用终结器。除此之外,这可以简化对创建和使用全局状态的应用程序的测试。

以下示例使用两个参数化fixture,其中一个作用于每个模块,所有功能都执行print调用来显示设置流程,修改之前的test_module.py文件,输入以下代码:

import pytest

@pytest.fixture(scope="module", params=["mod1", "mod2"])
def modarg(request):
    param = request.param
    print ("  设置 modarg %s" % param)
    yield param
    print ("  拆卸 modarg %s" % param)

@pytest.fixture(scope="function", params=[1,2])
def otherarg(request):
    param = request.param
    print ("  设置 otherarg %s" % param)
    yield param
    print ("  拆卸 otherarg %s" % param)

def test_0(otherarg):
    print ("  用 otherarg %s 运行 test0" % otherarg)
def test_1(modarg):
    print ("  用 modarg %s 运行 test1" % modarg)
def test_2(otherarg, modarg):
    print ("  用 otherarg %s 和 modarg %s 运行 test2" % (otherarg, modarg))

让我们使用pytest -v -s test_module.py运行详细模式测试并查看打印输出:

test_module.py文件执行截图

我们可以看到参数化的module(模块)范围的modarg资源影响了测试执行的排序,使用了最少的活动资源。mod1参数化资源的终结器是在mod2资源建立之前执行的。特别要注意test_0是完全独立的,会首先完成,然后用mod1执行test_1,再然后用mod1执行test_2,再然后用mod2执行test_1,最后用mod2执行test_2otherarg参数化资源是function(函数)的范围,是在每次使用测试之后建立起来的。

使用类、模块或项目的fixture

有时测试函数不需要直接访问一个fixture对象,例如,测试可能需要使用空目录作为当前工作目录,不关心具体目录。这里使用标准的tempfile和pytest fixture来实现它,我们将fixture的创建分隔成一个conftest.py文件:

import pytest
import tempfile
import os

@pytest.fixture()
def cleandir():
    newpath = tempfile.mkdtemp()
    os.chdir(newpath)

并通过usefixtures标记声明在测试模块中的使用,新建一个test_setenv.py文件,输入以下代码:

import os
import pytest

@pytest.mark.usefixtures("cleandir")
class TestDirectoryInit(object):
    def test_cwd_starts_empty(self):
        assert os.listdir(os.getcwd()) == []
        with open("myfile", "w") as f:
            f.write("hello")

    def test_cwd_again_starts_empty(self):
        assert os.listdir(os.getcwd()) == []

由于使用了usefixtures标记,所以执行每个测试方法都需要cleandir fixture,就像为每个测试方法指定了一个cleandir函数参数一样,让我们运行它来验证我们的fixture已激活:

test_setenv.py文件执行截图

我们可以像这样指定多个fixture:

@pytest.mark.usefixtures("cleandir", "anotherfixture")

我们可以使用标记机制的通用功能来指定测试模块级别的fixture使用情况:

pytestmark = pytest.mark.usefixtures("cleandir")

要注意的是,分配的变量必须被称为pytestmark,例如,分配foomark不会激活fixture。最后,我们可以将项目中所有测试所需的fixture放入一个pytest.ini文件中:

[pytest]
usefixtures = cleandir

自动使用fixture

有时候,我们可能希望自动调用fixture,而不是显式声明函数参数或使用usefixtures装饰器,例如,我们有一个数据库fixture,它有一个开始、回滚、提交的体系结构,我们希望通过一个事务和一个回滚自动地包含每一个测试方法,新建一个test_db_transact.py文件,输入以下代码:

import pytest

class DB(object):
    def __init__(self):
        self.intransaction = []
    def begin(self, name):
        self.intransaction.append(name)
    def rollback(self):
        self.intransaction.pop()

@pytest.fixture(scope="module")
def db():
    return DB()

class TestClass(object):
    @pytest.fixture(autouse=True)
    def transact(self, request, db):
        db.begin(request.function.__name__)
        yield
        db.rollback()

    def test_method1(self, db):
        assert db.intransaction == ["test_method1"]

    def test_method2(self, db):
        assert db.intransaction == ["test_method2"]

类级别的transact fixture被标记为autouse=true,这意味着类中的所有测试方法将使用该fixture,而不需要在测试函数签名或类级别的usefixtures装饰器中陈述它。如果我们运行它,会得到两个通过的测试:

test_db_transact.py文件执行截图

以下是在其他范围内如何使用自动fixture:

  • 自动fixture遵循scope=关键字参数,如果一个自动fixture的scope='session',它将只运行一次,不管它在哪里定义。scope='class'表示每个类会运行一次,等等。
  • 如果在一个测试模块中定义一个自动fixture,所有的测试函数都会自动使用它。
  • 如果在conftest.py文件中定义了自动fixture,那么在其目录下的所有测试模块中的所有测试都将调用fixture。
  • 最后,要小心使用自动fixture,如果我们在插件中定义了一个自动fixture,它将在插件安装的所有项目中的所有测试中被调用。例如,在pytest.ini文件中,这样一个全局性的fixture应该真的应该做任何工作,避免无用的导入或计算。

最后还要注意,上面的的transact fixture可能是我们希望在项目中提供的fixture,而没有通常的激活,规范的方法是将定义放在conftest.py文件而不使用自动运行:

@pytest.fixture
def transact(request, db):
    db.begin()
    yield
    db.rollback()

然后例如,有一个测试类通过声明使用它需要:

@pytest.mark.usefixtures("transact")
class TestClass(object):
    def test_method1(self):
        ......

在这个测试类中的所有测试方法将使用transact fixture,而模块中的其他测试类或函数将不会使用它,除非它们也添加一个transact引用。

覆盖不同级别的fixture

在相对较大的测试套件中,我们很可能需要使用本地定义的套件重写全局或根fixture,从而保持测试代码的可读性和可维护性。

覆盖文件夹级别的fixture

鉴于测试文件的结构是:

tests/
    __init__.py

    conftest.py
        # tests/conftest.py
        import pytest

        @pytest.fixture
        def username():
            return 'username'

    test_something.py
        # tests/test_something.py
        def test_username(username):
            assert username == 'username'

    subfolder/
        __init__.py

        conftest.py
            # tests/subfolder/conftest.py
            import pytest

            @pytest.fixture
            def username(username):
                return 'overridden-' + username

        test_something.py
            # tests/subfolder/test_something.py
            def test_username(username):
                assert username == 'overridden-username'

正如上面代码所示,具有相同名称的fixture可以在某些测试文件夹级别上被覆盖,但是要注意的是,basesuper fixture可以轻松地从上面的fixture进入,并在上面的例子中使用。

覆盖测试模块级别的fixture

鉴于测试文件的结构是:

tests/
    __init__.py

    conftest.py
        # tests/conftest.py
        @pytest.fixture
        def username():
            return 'username'

    test_something.py
        # tests/test_something.py
        import pytest

        @pytest.fixture
        def username(username):
            return 'overridden-' + username

        def test_username(username):
            assert username == 'overridden-username'

    test_something_else.py
        # tests/test_something_else.py
        import pytest

        @pytest.fixture
        def username(username):
            return 'overridden-else-' + username

        def test_username(username):
            assert username == 'overridden-else-username'

在上面的例子中,某个测试模块可以覆盖同名的fixture。

直接用测试参数化覆盖fixture

鉴于测试文件的结构是:

tests/
    __init__.py

    conftest.py
        # tests/conftest.py
        import pytest

        @pytest.fixture
        def username():
            return 'username'

        @pytest.fixture
        def other_username(username):
            return 'other-' + username

    test_something.py
        # tests/test_something.py
        import pytest

        @pytest.mark.parametrize('username', ['directly-overridden-username'])
        def test_username(username):
            assert username == 'directly-overridden-username'

        @pytest.mark.parametrize('username', ['directly-overridden-username-other'])
        def test_username_other(other_username):
            assert other_username == 'other-directly-overridden-username-other'

在上面的例子中,fixture值被测试参数值覆盖,即使测试不直接使用它,fixture的值也可以用这种方式重写。

使用非参数化参数替代参数化的fixture

鉴于测试文件的结构是:

tests/
    __init__.py

    conftest.py
        # tests/conftest.py
        import pytest

        @pytest.fixture(params=['one', 'two', 'three'])
        def parametrized_username(request):
            return request.param

        @pytest.fixture
        def non_parametrized_username(request):
            return 'username'

    test_something.py
        # tests/test_something.py
        import pytest

        @pytest.fixture
        def parametrized_username():
            return 'overridden-username'

        @pytest.fixture(params=['one', 'two', 'three'])
        def non_parametrized_username(request):
            return request.param

        def test_username(parametrized_username):
            assert parametrized_username == 'overridden-username'

        def test_parametrized_username(non_parametrized_username):
            assert non_parametrized_username in ['one', 'two', 'three']

    test_something_else.py
        # tests/test_something_else.py
        def test_username(parametrized_username):
            assert parametrized_username in ['one', 'two', 'three']

        def test_username(non_parametrized_username):
            assert non_parametrized_username == 'username'

在上面的例子中,一个参数化的fixture被一个非参数化的版本覆盖,一个非参数化的fixture被某个测试模块的参数化版本覆盖,这同样适用于测试文件夹级别。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容