首页 > 其他 > 详细

pytest之fixture使用

时间:2021-07-13 20:22:46      阅读:11      评论:0      收藏:0      [点我收藏+]

前言 

setup和teardown能实现在测试用例执行之前或之后做一些操作,但是这种是整个测试脚本全局生效的;

如果我们想实现在某些用例执行之前进行登录,某些用例执行之前不需要进行登录,这种场景我们再使用setup和teardown就无法实现了,这时候我们就需要用到fixture功能。

fixture方法

fixture(scope="function", params=None, autouse=False, ids=None, name=None)

参数:

scope:被fixture标记的方法的作用域,可选值:function(默认)、class、module、session

  • function:作用于每个方法或函数,每个方法或函数都运行一次
  • class:作用于整个class类,每个class中的所有test只运行一次
  • module:作用于整个模块,每个module中的所有test只运行一次
  • session:作用于整个session,整个session只运行一次(慎用)

params:list类型,一个可选的参数列表,它将会多次调用被fixture标记的方法和所有用到这个fixture的test测试用例,当前调用参数可以用 request.param 来获取。

autouse:如果为True,则为所有test测试用例激活fixture,运行测试用例的时候会自动运行被fixture标记的方法;如果为False,则需要显示指定来激活fixture,不会自动运行。

ids:id字符串列表,与params相对应,因此它们也是测试的一部分。如果没有提供ids,那么将会从params来自动生成。

name:fixture的名称。默认为被fixture装饰器标记的函数名。

fixture的使用

通过参数引用fixture

举例:

# file_name:test_fixture.py


import pytest


class Test_A:

    @pytest.fixture()
    def before(self):
        print("\n--------before fixture has ran--------")

    def test_a(self, before):    # test_a方法以参数的形式传入了被fixture标记的函数,fixture的名称默认为被fixture标记的函数名
        print(-------test_a has ran-------)
        assert 1


if __name__ == __main__:
    pytest.main([-s, test_fixture.py])

运行结果:

技术分享图片

从结果中可以看到被fixture标记的函数before会优先于测试用例test_a运行。

通过使用name参数来引用fixture

# file_name:test_fixture.py


import pytest


class Test_A:

    @pytest.fixture(name="before_fixture_name")
    def before(self):
        print("\n--------before fixture has ran--------")

    def test_a(self, before_fixture_name):    # test_a方法以参数的形式传入了被fixture标记的函数,这里的fixture名称为:before_fixture_name,如果不设置name参数,则fixture的名称默认为被fixture标记的函数名
        print(-------test_a has ran-------)
        assert 1


if __name__ == __main__:
    pytest.main([-s, test_fixture.py])

通过函数的形式引用fixture

举例:

# file_name: test_fixture.py


import pytest


@pytest.fixture()  # 被fixture标记的函数也可以应用在测试类的外部
def before():
    print("\n--------before fixture has ran--------")


@pytest.mark.usefixtures("before")  # 通过使用usefixtures()来引用fixture
class Test_A:

    def test_a(self):
        print(-------test_a has ran-------)
        assert 1


if __name__ == __main__:
    pytest.main([-s, test_fixture.py])

运行结果:

技术分享图片

从结果中可以看到被fixture标记的函数before会优先于测试用例test_a运行。

通过autouse=True设置默认运行fixture

举例:

# file_name: test_fixture.py


import pytest


@pytest.fixture(autouse=True)  # 通过参数autouse=True来设置fixture默认运行
def before():
    print("\n--------before fixture has ran--------")


class Test_A:

    def test_a(self):
        print(-------test_a has ran-------)
        assert 1

    def test_b(self):
        print(-------test_b has ran-------)
        assert 1


if __name__ == __main__:
    pytest.main([-s, test_fixture.py])

运行结果:

技术分享图片

从结果中可以看到我们并没有显示指定test_a和test_b使用fixture,但是在执行测试用例之前却执行了fixture,这就是因为我们将fixture设置成了autouse=True。

fixture作用域设置成function

举例:

# file_name: test_fixture.py


import pytest


@pytest.fixture(scope="function", autouse=True)  # 作用域设置成function,通过参数autouse=True来设置fixture默认运行
def before():
    print("\n--------before fixture has ran--------")


class Test_A:

    def test_a(self):
        print(-------test_a has ran-------)
        assert 1

    def test_b(self):
        print(-------test_b has ran-------)
        assert 1


if __name__ == __main__:
    pytest.main([-s, test_fixture.py])

运行结果:

技术分享图片

从结果中可以看到将fixture的作用域设置成scope=function后,每个test测试用例执行前都会执行一次被fixture标记的函数。并且通过跟上一个例子对比我们可以看到设置scope=function和不设置scope参数的执行结果是一致的,这说明scope参数的默认值是function

fixture作用域设置成class

举例:

# file_name: test_fixture.py


import pytest


@pytest.fixture(scope="class", autouse=True)  # 作用域设置成class,通过参数autouse=True来设置fixture默认运行
def before():
    print("\n--------before fixture has ran--------")


class Test_A:

    def test_a(self):
        print(-------test_a has ran-------)
        assert 1

    def test_b(self):
        print(-------test_b has ran-------)
        assert 1


if __name__ == __main__:
    pytest.main([-s, test_fixture.py])

运行结果:

技术分享图片

从运行结果中可以看到测试类中有两个测试用例,但是fixture却只执行了一次。

fixture的返回值使用

举例:

# file_name: test_fixture.py


import pytest


@pytest.fixture()
def return_data():
    print("\n--------before fixture has ran--------")
    return 2    # 返回值


class Test_A:

    def test_a(self, return_data):
        print(-------test_a has ran-------)
        assert 1 == return_data # 拿到返回值做断言


if __name__ == __main__:
    pytest.main([-s, test_fixture.py])

运行结果:

技术分享图片

从结果中看到我们拿到了fixture的返回值为2,在测试用例中拿到返回值做断言,断言失败。

fixture的params参数使用

举例:

# file_name: test_fixture.py


import pytest


@pytest.fixture(params=[1, 2, 3])
def return_data(request):   # 传入参数request,request系统内置的fixture
    print("\n--------before fixture has ran--------")
    return request.param  # 通过request.param 获取当前传入的参数


class Test_A:

    def test_a(self, return_data):
        print(-------test_a has ran,return_data的值为:{}-------.format(return_data))
        assert 1 == return_data  # 拿到返回值做断言


if __name__ == __main__:
    pytest.main([-s, test_fixture.py])

运行结果:

技术分享图片

从结果中我们可以看到测试用例执行了3次。通过设置params参数会导致多次调用被fixture标记的函数,并且使用该fixture函数的测试用例也会执行多次。

 

pytest之fixture使用

原文:https://www.cnblogs.com/hls-code/p/15007616.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!