pytest测试框架(三) fixtures

pytest可以用@pytest.fixture 装饰器来装饰一个方法。
方法:fixture(scope="function", params=None, autouse=False, ids=None, name=None)
常用参数:

  • scope:被标记方法的作用域
    • function (default):作用于每个测试方法,每个test都运行一次
    • class:作用于整个类,每个class的所有test只运行一次
    • module:作用于整个模块,每个module的所有test只运行一次
    • session:作用于整个session(慎用),每个session只运行一次
  • params:(list类型)提供参数数据,供调用标记方法的函数使用
  • autouse:是否自动运行,默认为False不运行,设置为True自动运行

yield分隔前置和后置

# 定义一个函数名叫open_url的fixture前后置,前置为打开链接,后置为退出浏览器
def open_url():
# 前置
driver = webdriver.Chrome()
driver.get(url) #url为链接地址
yield driver    #yield之前代码是前置,之后的代码就是后置。
# 后置
driver.quit()

通过函数参数引用

class Test_A:
    @pytest.fixture()
    def before(self):
        print("------->before")
    def test_a(self,before): # ️ test_a方法传入了被fixture标识的函数,已变量的形式
        print("------->test_a")
        assert 1
if __name__ == '__main__':
    pytest.main(["-s", "test_a.py"])
执行结果:
    test_a.py 
        ------->before # 发现before会优先于测试函数运行
        ------->test_a
         .

通过函数引用


import pytest


class Test_ABC:
    @pytest.fixture()
    def before(self):
        print("------->before")
        return 2

    @pytest.mark.usefixtures("before")
    def test_a(self):
        print("------->test_a")
        assert 1


if __name__ == '__main__':
    pytest.main(["-s", "test_abc.py"])
  执行结果:
      test_abc.py 
      ------->before
      ------->test_a
      .

设置自动运行

import pytest


class Test_ABC:
    @pytest.fixture(autouse=True)  # 设置为默认运行
    def before(self):
        print("------->before")

    def test_a(self):
        print("------->test_a")
        assert 1


if __name__ == '__main__':
    pytest.main(["-s", "test_abc.py"])
执行结果:
    test_abc.py 
    ------->before 
    ------->test_a
        .

设置作用域为function

import pytest


@pytest.fixture(scope='function', autouse=True)  # 作用域设置为function,自动运行
def before():
    print("------->before")


class Test_ABC:

    def test_a(self):
        print("------->test_a")
        assert 1

    def test_b(self):
        print("------->test_b")
        assert 1


if __name__ == '__main__':
    pytest.main(["-s", "test_abc.py"])
运行结果:
test_abc.py 
------->before
------->test_a
.------->before
------->test_b
.

设置作用域为class

import pytest


@pytest.fixture(scope='class', autouse=True)  # 作用域设置为class,自动运行
def before():
    print("------->before")


class Test_ABC:

    def test_a(self):
        print("------->test_a")

        assert 1

    def test_b(self):
        print("------->test_b")
        assert 1


if __name__ == '__main__':
    pytest.main(["-s", "test_abc.py"])
运行结果:
test_abc.py 
------->before
------->test_a
.------->test_b
.

返回值

import pytest


@pytest.fixture()
def need_data():
    return 2  # 返回数字2


class Test_ABC:

    def test_a(self, need_data):
        print("------->test_a")
        assert need_data == 2  # 拿到返回值做一次断言


if __name__ == '__main__':
    pytest.main(["-s", "test_abc.py"])
运行结果:
test_abc.py ------->test_a
.

传递参数

import pytest


@pytest.fixture(params=[1, 2, 3])
def need_data(request):  # 传入参数request 系统封装参数
    return request.param  # 取列表中单个值,默认的取值方式


class Test_ABC:

    def test_a(self, need_data):
        print("------->test_a")
        assert need_data < 5  # 断言need_data


if __name__ == '__main__':
    pytest.main(["-s", "test_abc.py"])
运行结果:
test_abc.py 
------->test_a
.------->test_a
.------->test_a
.
posted @ 2022-09-09 17:28  小小滴人a  阅读(83)  评论(0编辑  收藏  举报