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


一、setup/teardown,setup_class/teardown_class所有


为什么需要这些功能?
比如:web自动化执行用例之前,请问需要打开浏览器吗?用例执行后需要关闭浏览器?
和unittest不一样,全是小写

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/4/6 11:37

# @File    : test_class.py
import pytest


class TestHello:

    def setup_class(self):
        print('在每个类执行前的初始化工作:如创建日志对象,创建数据库的连接')

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

    def test_01_jame(self):
        print('\n测试JAME')

    def test_02_lucy(self):
        print('\n测试Lucy')

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

    def teardown_class(self):
        print('在每个类执行前的初始化工作:如:销毁日志对象,销毁数据库连接')


if __name__ == '__main__':
    # pytest.main(['-vs', '--html=../report/report.html'])
    pytest.main(['-vs', '--html=../report/report.html'])

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

pytest.fixture的scope作用范围
scope有4个作用范围:function、class、module、session

function:每个函数或方法都会调用
class:每个类只调用1次
module:每个模块只调用1次
session:多个模块调用1次,通常写在conftest中


@pytest.fixture(scope=””,params=””,autouse=””,ids=””,name=””)
-----------------------------------------------------------------------------------------------
(1)scope表示的是被@pytest.fixture标记的方法的作用域。function(默认),class,module,package/session
   scope的讲解参考:https://www.cnblogs.com/bxbyy/articles/13419710.html 
(2)params:参数化(支持:列表[],元组[], 字典列表[{},{},{}] ,字典元组 ({},{},{})    )

params=['张三','李四','王五']这里params是参数名,有s。
request.param这里是属性名,是没有s的。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/4/6 11:37 
# @File    : test_class.py
import pytest


@pytest.fixture(scope='function',params=['张三','李四','王五'])
def my_fixture(request):
    return request.param


class TestFixture:

    def test_01_jame(self):
        print('\n测试JAME')

    def test_02_lucy(self, my_fixture):
        print('\n测试Lucy')
        print('---------'+str(my_fixture))


if __name__ == '__main__':
    pytest.main(['-vs', '--html=../report/report.html'])

运行结果:

testcase/test_fixture.py::TestFixture::test_01_jame
测试JAME
PASSED
testcase/test_fixture.py::TestFixture::test_02_lucy[\u5f20\u4e09]
测试Lucy
---------张三
PASSED
testcase/test_fixture.py::TestFixture::test_02_lucy[\u674e\u56db]
测试Lucy
---------李四
PASSED
testcase/test_fixture.py::TestFixture::test_02_lucy[\u738b\u4e94]
测试Lucy
---------王五
PASSED

(3)autouse=True :自动使用,默认False
(4)ids:当使用params参数化时,给每一个值设置一个变量名,意义不大
(5)name:表示给被@pytest.fixture标记的方法取一个别名。
  注意:当给方法my_fixture起了别名aaa后,原来的名称my_fixture就用不了了,再使用只能用别名aaa

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/4/6 11:37
# @File    : test_class.py
import pytest


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


class TestFixture:

    def test_01_jame(self):
        print('\n测试JAME')

    def test_02_lucy(self):
        print('\n测试Lucy')


if __name__ == '__main__':
    pytest.main(['-vs', '--html=../report/report.html'])

autouse=True时,执行结果如下:

testcase/test_fixture_auto.py::TestFixture::test_01_jame 这是前置的方法

测试JAME
PASSED这是后置的方法

testcase/test_fixture_auto.py::TestFixture::test_02_lucy 这是前置的方法

测试Lucy
PASSED这是后置的方法

scope='class'时

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/4/6 11:37
# @File    : test_class.py
import pytest


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


class TestFixture:

    def test_01_jame(self):
        print('\n测试JAME')

    def test_02_lucy(self):
        print('\n测试Lucy')


class TestFixtureClass:

    def test_03_apple(self):
        print('\n测试apple')

    def test_04_orange(self):
        print('\n测试orange')


if __name__ == '__main__':
    pytest.main(['-vs', '--html=../report/report.html'])

运行结果如下:

testcase/test_fixture_class.py::TestFixture::test_01_jame 这是前置的方法

测试JAME
PASSED
testcase/test_fixture_class.py::TestFixture::test_02_lucy
测试Lucy
PASSED这是后置的方法

testcase/test_fixture_class.py::TestFixtureClass::test_03_apple 这是前置的方法

测试apple
PASSED
testcase/test_fixture_class.py::TestFixtureClass::test_04_orange
测试orange
PASSED这是后置的方法

scope=‘modle’时

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/4/6 11:37
# @File    : test_class.py
import pytest


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


class TestFixture:

    def test_01_jame(self):
        print('\n测试JAME')

    def test_02_lucy(self):
        print('\n测试Lucy')


class TestFixtureClass:

    def test_03_apple(self):
        print('\n测试apple')

    def test_04_orange(self):
        print('\n测试orange')


if __name__ == '__main__':
    pytest.main(['-vs', '--html=../report/report.html'])

运行结果如下:

testcase/test_fixture_module.py::TestFixture::test_01_jame 这是前置的方法

测试JAME
PASSED
testcase/test_fixture_module.py::TestFixture::test_02_lucy
测试Lucy
PASSED
testcase/test_fixture_module.py::TestFixtureClass::test_03_apple
测试apple
PASSED
testcase/test_fixture_module.py::TestFixtureClass::test_04_orange
测试orange
PASSED这是后置的方法

yield和return都表示返回的意思,但return后边不能有代码,但yield可以返回后,后面可以接代码。

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

(比如:项目的全局登录,模块的全局处理)
1.conftest.py文件是单独存放的一个夹具配置文件,名称是不能更改。
2.用处可以在不同的py文件中使用同一个fixture函数
3.原则上conftest.py需要和运行的用例放到统一层,并且不需要做任何的import导入的操作。

总结:

  1. setup/teardown,setup_class/teardown_class 它是作用于所有用例或者所有的类
  2. @pytest.fixture() 它的作用是既可以部分也可以全部前后置。
  3. conftest.py和pytes.fixture()结合使用,作用于全局的前后置

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐