pytest框架前后置处理

pytest框架实现一些前后置(固件、夹具)的处理,常用三种

一、为什么需要前后置这些功能呢?

比如:web自动化执行用例之前,请问需要打开浏览器吗?用例执行后需要关闭浏览器吗?

二、setup和teardown

test_demo01.py

class TestDemo01:
    def setup(self):
        print("\n在执行测试用例之前执行的初始化代码:打开浏览器,加载网页")

    def test_01_sunwukong(self):
        print("\n测试孙悟空")

    def test_02_tangseng(self):
        print("\n测试唐僧")

    def teardown(self):
        print("\n在执行测试用例之后扫尾的代码:关闭浏览器")

all.py

import pytest

if __name__ == '__main__':
    pytest.main(["-vs", "./web_testCase/test_demo01.py"])

运行结果:
setup和teardown是在每个用例执行前后都会被执行

web_testCase/test_demo01.py::TestDemo01::test_01_sunwukong 
在执行测试用例之前执行的初始化代码:打开浏览器,加载网页

测试孙悟空
PASSED
在执行测试用例之后扫尾的代码:关闭浏览器

web_testCase/test_demo01.py::TestDemo01::test_02_tangseng 
在执行测试用例之前执行的初始化代码:打开浏览器,加载网页

测试唐僧
PASSED
在执行测试用例之后扫尾的代码:关闭浏览器

三、setup_class、teardown_class

test_demo01.py


class TestDemo01:
    # 在所有的用例之前只执行一次
    def setup_class(self):
        print("\n在每个类执行前的初始化工作:比如:创建日志对象,创建数据库的连接,创建接口的请求对象")

    # 在每个用例之前执行一次
    def setup(self):
        print("\n在执行测试用例之前执行的初始化代码:打开浏览器,加载网页")

    def test_01_sunwukong(self):
        print("\n测试孙悟空")

    def test_02_tangseng(self):
        print("\n测试唐僧")

    def teardown(self):
        print("\n在执行测试用例之后扫尾的代码:关闭浏览器")

    def teardown_class(self):
        print("\n在每个类执行后的扫尾工作:比如:销毁日志对象,断开数据库连接")

all.py

import pytest

if __name__ == '__main__':
    pytest.main(["-vs", "./web_testCase/test_demo01.py"])

运行结果

web_testCase/test_demo01.py::TestDemo01::test_01_sunwukong 
在每个类执行前的初始化工作:比如:创建日志对象,创建数据库的连接,创建接口的请求对象

在执行测试用例之前执行的初始化代码:打开浏览器,加载网页

测试孙悟空
PASSED
在执行测试用例之后扫尾的代码:关闭浏览器

web_testCase/test_demo01.py::TestDemo01::test_02_tangseng 
在执行测试用例之前执行的初始化代码:打开浏览器,加载网页

测试唐僧
PASSED
在执行测试用例之后扫尾的代码:关闭浏览器

在每个类执行后的扫尾工作:比如:销毁日志对象,断开数据库连接

四、使用@pytest.fixture()装饰器来实现部分用例的前后置

1、写法
@pytest.fixture(scope=,params=,autouse=,ids=,name=)
参数说明:
scope :表示的是被@pytest.fixture标记的方法的作用域。 【function:默认,class(类),module(模块),package/session】
params:参数化(支持:列表,元祖,字典列表[{},{},{}],字典元祖[(),(),()])
autouse:True:自动执行,默认False
ids:当 使用params参数化时,给每一个值设置一个变量名,意义不大
name:给表示的是被pytest.fixture标记的方法取一个别名
2、初级代码使用:

import pytest


@pytest.fixture(scope="function")
def my_fixture():
    print("这是前置方法")
    yield 
    print("这是后置方法")


class TestDemo01:

    def test_01_sunwukong(self):
        print("\n测试孙悟空")
     # 如果只让test_02  执行前后置,那么就只给这个方法设置my_fixture
    def test_02_tangseng(self, my_fixture):
        print("\n测试唐僧")

测试结果:

web_testCase/test_demo01.py::TestDemo01::test_01_sunwukong 
测试孙悟空
PASSED
web_testCase/test_demo01.py::TestDemo01::test_02_tangseng 这是前置方法

测试唐僧
PASSED这是后置方法

3、scope不用参数值的区别
①function 函数级别,既每个函数执行,前后置方法也都会被执行

import pytest


@pytest.fixture(scope="function", autouse=True)
def my_fixture():
    print("这是前置方法")
    yield
    print("这是后置方法")


class TestDemo01:

    def test_01_sunwukong(self):
        print("\n测试孙悟空")

    def test_02_tangseng(self):
        print("\n测试唐僧")

测试结果

web_testCase/test_demo01.py::TestDemo01::test_01_sunwukong 这是前置方法

测试孙悟空
PASSED这是后置方法

web_testCase/test_demo01.py::TestDemo01::test_02_tangseng 这是前置方法

测试唐僧
PASSED这是后置方法

②class 类级别,既 前后置方法只执行一次

import pytest


@pytest.fixture(scope="class", autouse=True)
def my_fixture():
    print("这是前置方法")
    yield
    print("这是后置方法")


class TestDemo01:

    def test_01_sunwukong(self):
        print("\n测试孙悟空")

    def test_02_tangseng(self):
        print("\n测试唐僧")

测试结果:
web_testCase/test_demo01.py::TestDemo01::test_01_sunwukong 这是前置方法

测试孙悟空
PASSED
web_testCase/test_demo01.py::TestDemo01::test_02_tangseng 
测试唐僧
PASSED这是后置方法

③modle:模块级别

import pytest


@pytest.fixture(scope="module", autouse=True)
def my_fixture():
    print("这是前置方法")
    yield
    print("这是后置方法")


class TestDemo01:

    def test_01_sunwukong(self):
        print("\n测试孙悟空")

    def test_02_tangseng(self):
        print("\n测试唐僧")


class TestDemo02:
    def test_01_xiaoming(self):
        print("\n测试小明")

    def test_02_xiaohong(self):
        print("\n测试小红")

测试结果:
web_testCase/test_demo01.py::TestDemo01::test_01_sunwukong 这是前置方法

测试孙悟空
PASSED
web_testCase/test_demo01.py::TestDemo01::test_02_tangseng 
测试唐僧
PASSED
web_testCase/test_demo01.py::TestDemo02::test_01_xiaoming 
测试小明
PASSED
web_testCase/test_demo01.py::TestDemo02::test_02_xiaohong 
测试小红
PASSED这是后置方法

4、参数化使用: params

import pytest


@pytest.fixture(scope="function", params=['成龙', '吴京', '易烊千玺'])
def test_fixture(request):
    print("这是前置方法")
    yield request.param  # return和yeild的区别: 都是表示返回值的意思,但是return的后面不能有代码,yeild很后面可以有代码
    print("这是后置方法")
    # return


class TestDemo01:

    def test_01(self):
        print("\n测试用例1")

    # 如果只让test_02  执行前后置,那么就只给这个方法设置test_fixture

    def test_02(self, test_fixture):
        print("\n测试用例2")
        print(test_fixture)


if __name__ == '__main__':
    pytest.main(['-vs'])
 
 测试结果:
 test_demo01.py::TestDemo01::test_01 
测试用例1
PASSED
test_demo01.py::TestDemo01::test_02[\u6210\u9f99] 这是前置方法

测试用例2
成龙
PASSED这是后置方法

test_demo01.py::TestDemo01::test_02[\u5434\u4eac] 这是前置方法

测试用例2
吴京
PASSED这是后置方法

test_demo01.py::TestDemo01::test_02[\u6613\u70ca\u5343\u73ba] 这是前置方法

测试用例2
易烊千玺
PASSED这是后置方法

params=[‘成 ‘,‘甄子丹’,’ 10’]   params是参数名 有s。
request.param  是属性名 是没有s的。

5、ids参数的使用

import pytest


@pytest.fixture(scope="function", params=['成龙', '吴京', '易烊千玺'], ids=['cl', 'wj', 'yyqx'])
def test_fixture(request):
    print("这是前置方法")
    yield request.param  # return和yeild的区别: 都是表示返回值的意思,但是return的后面不能有代码,yeild很后面可以有代码
    print("这是后置方法")
    # return


class TestDemo01:

    def test_01(self):
        print("\n测试用例1")

    # 如果只让test_02  执行前后置,那么就只给这个方法设置test_fixture

    def test_02(self, test_fixture):
        print("\n测试用例2")
        print(test_fixture)


if __name__ == '__main__':
    pytest.main(['-vs'])

测试结果:
在这里插入图片描述
6、name参数的使用
当使用了别名之后,原来的名称就使用不了了。

import pytest


@pytest.fixture(scope="function", params=['成龙', '吴京', '易烊千玺'], ids=['cl', 'wj', 'yyqx'], name='fixture01')
def test_fixture(request):
    print("这是前置方法")
    yield request.param  # return和yeild的区别: 都是表示返回值的意思,但是return的后面不能有代码,yeild很后面可以有代码
    print("这是后置方法")
    # return


class TestDemo01:

    def test_01(self):
        print("\n测试用例1")

    # 如果只让test_02  执行前后置,那么就只给这个方法设置test_fixture

    def test_02(self, fixture01):
        print("\n测试用例2")
        print(fixture01)


if __name__ == '__main__':
    pytest.main(['-vs'])

五、通过conftest.py和@pytest.fixture()结合使用来实现全局的前置应用

1、应用场景:
比如:项目的全局登录,模块的全局处理
2、conftest.py的特点

①conftest.py文件是单独存放的一个夹具配置文件,名称是不能更改的。
②可以在不同的py文件中使用同一个fixture函数
③原则上conftest.py需要和运行的用例放到同一个目录下,并且不需要做任何的import导入操作。

代码实现:
在这里插入图片描述
每个模块设置单独的前后置,也可以使用外部公共的前后置。
product模块:
conftest.py

import pytest


@pytest.fixture(scope="function")
def product_fixture(request):
    print("商品管理前置方法")
    yield
    print("商品管理后置方法")

test_demo01.py

class TestDemo01:

    def test_01(self):
        print("\n测试用例1")

    # 如果只让test_02  执行前后置,那么就只给这个方法设置product_fixture

    def test_02(self, product_fixture):
        print("\n测试用例2")
        print(product_fixture)
   
 测试结果:
 web_testCase/product/test_demo01.py::TestDemo01::test_01 
测试用例1
PASSED
web_testCase/product/test_demo01.py::TestDemo01::test_02 商品管理前置方法

测试用例2
None
PASSED商品管理后置方法

在商品模块的用例中,也可以使用外部的前后置

import pytest


@pytest.fixture(scope="function")
def all_fixture(request):
    print("全局前置方法")
    yield
    print("全局后置方法")

test_demo01.py

class TestDemo01:

    def test_01(self):
        print("\n测试用例1")

    # 如果只让test_02  执行前后置,那么就只给这个方法设置product_fixture

    def test_02(self, all_fixture, product_fixture):
        print("\n测试用例2")
        print(all_fixture)
        print(product_fixture)

测试结果:
web_testCase/product/test_demo01.py::TestDemo01::test_01 
测试用例1
PASSED
web_testCase/product/test_demo01.py::TestDemo01::test_02 全局前置方法
商品管理前置方法

测试用例2
None
None
PASSED商品管理后置方法
全局后置方法

all.py

import pytest

if __name__ == '__main__':
    pytest.main(["-vs"])

六、pytest 的三种实现前后置方式的区别

  • setup/teardown ,setup_class/teardown_class,它是作用于所有的用例或类
  • @pytest.fixture() 它即可以全局前后置,也可以部门前后置
  • conftest.py 和@pytest.fixture()结合使用,作用于全局的前后置。
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值