700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 测试开发全栈之Python自动化 Pytest 之 fixture

测试开发全栈之Python自动化 Pytest 之 fixture

时间:2023-08-19 06:04:38

相关推荐

测试开发全栈之Python自动化 Pytest 之 fixture

Pytest 之 fixture

unittest 和 nose 都支持 fixture 的,但是 fixture 在 pytest 里使用更灵活。也算是 pytest 的一个闪光点吧

可以理解为一个跟 setup 和 teardown 这种前后置类似的东西。但是比它们要强大、灵活很多

fixtur 当做参数传入

# -*- coding: utf-8 -*-import pytest@pytest.fixture()def login():print('登录系统')# 直接使用函数名做为参数传入def test_01(login):print('测试用例一')def test_02():print('测试用例2')def test03():print('测试用例3')

运行结果

只有 tes_01 调用了 login

遗留问题来了,如果我这里有 10 个方法或更多?是不是都需调用 login 方法?继续看下面的 fixture 参数

testcase.py::test_01 登录系统测试用例一PASSEDtestcase.py::test_02 测试用例2PASSEDtestcase.py::test03 测试用例3PASSED

fixture 语法

# scope有4个作用范围:function(不填则默认)、class、module、sessionfixture(scope='function', params=None, autouse=False, ids=None, name=None)

参数说明

scope:即作用域,function"(默认),“class”,“module”,"session"四个

params:可选参数列表,它将导致多个参数调用 fixture 函数和所有测试使用它。

autouse:默认:False,需要用例手动调用该 fixture;如果是 True,所有作用域内的测试用例都会自动调用该 fixture

ids:params 测试 ID 的一部分。如果没有将从 params 自动生成.

name:默认:装饰器的名称,同一模块的 fixture 相互调用建议写个不同的 name。

session 的作用域:是整个测试会话,即开始执行 pytest 到结束测试 scope 参数作用范围控制 fixture 的作用范围:session>module>class>function

autouse

参数置默认为 False,则需要手动去调用装饰器

# -*- coding: utf-8 -*-import pytest# 当前就算定义了装饰器,也不会调用Login@pytest.fixture()def login():print("打开浏览器")def test1():print("test1里的用例")def test2():print("test2里的用例")

调用方式 1

# -*- coding: utf-8 -*-import pytest@pytest.fixture()def login():print("打开浏览器")# 直接传入函数名def test1(login):print("test1里的用例")def test2(login):print("test2里的用例")

调用方式 2

# -*- coding: utf-8 -*-import pytest# autouse设为True,就能自动调用login的装饰器@pytest.fixture(autouse=True)def login():print("打开浏览器")# 直接传入函数名def test1():print("test1里的用例")def test2():print("test2里的用例")

function

function:作用域为函数

所有的方法都调用了 login

# -*- coding: utf-8 -*-import pytest@pytest.fixture(scope='function', autouse=True)def login():print('登录系统')def test_01():print('测试用例一')def test_02():print('测试用例2')def test03():print('测试用例3')

运行结果

符合用例名设计的都会调用装饰器

login 不符合所以不会调用

testcase.py::test_01 登录系统测试用例一PASSEDtestcase.py::test_02 登录系统测试用例2PASSEDtestcase.py::test03 登录系统测试用例3PASSED

class

class:作用域为类

所以 TestCase1 和 TestCase2 这两个类都会执行 login

# -*- coding: utf-8 -*-# @Time : /1/14 21:05# @Author : 公众号(程序员一凡)import pytest@pytest.fixture(scope='class', autouse=True)def login():print('登录系统')def test_01():print('这个是类外面的用例')class TestCase1:def test_02(self):print('测试用例2')def test03(self):print('测试用例3')class TestCase2:def test_04(self):print('测试用例4')def test05(self):print('测试用例5')

运行结果

类里面的方法只会调用一次

pytest 机制,因为方法是以 test 开头,所以也会调用

testcase.py::test_01 登录系统这个是类外面的用例PASSEDtestcase.py::TestCase1::test_02 登录系统测试用例2PASSEDtestcase.py::TestCase1::test03 测试用例3PASSEDtestcase.py::TestCase2::test_04 登录系统测试用例4PASSEDtestcase.py::TestCase2::test05 测试用例5PASSED

module

module:在当前.py 脚本里面所有用例开始前只执行一次

只要符合用例的设计要求,不管是类里和外边的都会调用

# -*- coding: utf-8 -*-import pytest@pytest.fixture(scope='class', autouse=True)def open():print("打开浏览器,并且打开百度首页")def test_s1():print("用例1:搜索python-1")class TestCase():def test_s2(self):print("用例2:搜索python-2")def test_s3(self):print("用例3:搜索python-3")

运行结果

当前文件里的用例都调用了装饰器

如果类名不是为 Test 开头你试试看是否还会调用装饰器?

testcase.py::test_s1 打开浏览器,并且打开百度首页用例1:搜索python-1PASSEDtestcase.py::TestCase::test_s2 打开浏览器,并且打开百度首页用例2:搜索python-2PASSEDtestcase.py::TestCase::test_s3 用例3:搜索python-3PASSED

session

fixture 为 session 级别是可以跨.py 模块调用的

当我们有多个.py 文件的用例时候,如果多个用例只需调用一次 fixture,那就可以设置为 scope=“session”,并写到 conftest.py 文件里

conftest.py 文件名称是固定的,pytest 会自动识别该文件。放到工程的根目录下,就可以全局调用了

如果放到某个 package 包下,那就只在该 package 内有效

# -*- coding: utf-8 -*-# conftest文件内容import pytest@pytest.fixture(scope="session", autouse=True)def login():print("调用conftest文件的里的方法")

两个用例文件

# -*- coding: utf-8 -*-# testcase1.py import pytestdef test1():print("test1里的用例")def test2():print("test2里的用例")# -*- coding: utf-8 -*-# testcase1.py import pytestdef test3():print("test3里的用例")def test4():print("test4里的用例")

运行结果

两个文件只有 testcase 文件的用例调了 conftest 里的方法

testcase.py::test1 调用conftest文件的里的方法test1里的用例PASSEDtestcase.py::test2 test2里的用例PASSEDtestcase1.py::test3 test3里的用例PASSEDtestcase1.py::test4 test4里的用例PASSED

pytest-allure 生成测试报告

03

文章首发于公众号:程序员一凡

加油吧,测试人!路就在脚下,成功就在明天!

未来的你肯定会感谢现在拼命的自己!

给大家推荐一个软件测试技术交流群:810119819 群友福利免费领取

愿你我相遇,皆有所获! 欢迎关注微信公众号:程序员阿沐

1.免费领取一份216页软件测试工程师面试宝典文档资料。

2.软件测试学习路线以及相对应的视频学习教程免费分享!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。