pytest框架之fixture详细使用详解
作者:辉辉辉辉a 发布时间:2022-01-22 01:47:19
本人之前写了一套基于unnitest框架的UI自动化框架,但是发现了pytest框架之后觉得unnitest太low,现在重头开始学pytest框架,一边学习一边记录,和大家分享,话不多说,那就先从pytest框架的精髓fixture说起吧!
简介:
fixture区别于unnitest的传统单元测试(setup/teardown)有显著改进:
1.有独立的命名,并通过声明它们从测试函数、模块、类或整个项目中的使用来激活。
2.按模块化的方式实现,每个fixture都可以互相调用。
3.fixture的范围从简单的单元测试到复杂的功能测试,可以对fixture配置参数,或者跨函数function,类class,模块module或整个测试session范围。
(很重要!!!)(很重要!!!)(很重要!!!)
谨记:当我们使用pytest框架写case的时候,一定要拿它的命令规范去case,这样框架才能识别到哪些case需要执行,哪些不需要执行。
用例设计原则
文件名以test_*.py文件和*_test.py
以test_开头的函数
以Test开头的类
以test_开头的方法
fixture可以当做参数传入
定义fixture跟定义普通函数差不多,唯一区别就是在函数上加个装饰器@pytest.fixture(),fixture命名不要以test开头,跟用例区分开。fixture是有返回值得,没有返回值默认为None。用例调用fixture的返回值,直接就是把fixture的函数名称当做变量名称。
ex:
import pytest
@pytest.fixture()
def test1():
a = 'leo'
return a
def test2(test1):
assert test1 == 'leo'
if __name__ == '__main__':
pytest.main('-q test_fixture.py')
输出:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 1 itemtest_fixture.py . [100%]
========================== 1 passed in 0.02 seconds ===========================
Process finished with exit code 0
使用多个fixture
如果用例需要用到多个fixture的返回数据,fixture也可以返回一个元祖,list或字典,然后从里面取出对应数据。
ex:
import pytest
@pytest.fixture()
def test1():
a = 'leo'
b = '123456'
print('传出a,b')
return (a, b)
def test2(test1):
u = test1[0]
p = test1[1]
assert u == 'leo'
assert p == '123456'
print('元祖形式正确')
if __name__ == '__main__':
pytest.main('-q test_fixture.py')
输出结果:
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 1 item
test_fixture.py 传出a,b
.元祖形式正确
[100%]
========================== 1 passed in 0.02 seconds ===========================
Process finished with exit code 0
当然也可以分成多个fixture,然后在用例中传多个fixture参数
import pytest
@pytest.fixture()
def test1():
a = 'leo'
print('\n传出a')
return a
@pytest.fixture()
def test2():
b = '123456'
print('传出b')
return b
def test3(test1, test2):
u = test1
p = test2
assert u == 'leo'
assert p == '123456'
print('传入多个fixture参数正确')
if __name__ == '__main__':
pytest.main('-q test_fixture.py')
输出结果:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 1 item
test_fixture.py
传出a
传出b
.传入多个fixture参数正确
fixture互相调用
import pytest
@pytest.fixture()
def test1():
a = 'leo'
print('\n传出a')
return a
def test2(test1):
assert test1 == 'leo'
print('fixture传参成功')
if __name__ == '__main__':
pytest.main('-q test_fixture.py')
输出结果:
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 1 item
test_fixture.py
传出a
.fixture传参成功
[100%]
========================== 1 passed in 0.03 seconds ===========================
Process finished with exit code 0
介绍完了fixture的使用方式,现在介绍一下fixture的作用范围(scope)
fixture的作用范围
fixture里面有个scope参数可以控制fixture的作用范围:session>module>class>function
-function:每一个函数或方法都会调用
-class:每一个类调用一次,一个类中可以有多个方法
-module:每一个.py文件调用一次,该文件内又有多个function和class
-session:是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module
fixture源码详解
fixture(scope='function',params=None,autouse=False,ids=None,name=None):
scope:有四个级别参数"function"(默认),"class","module","session"
params:一个可选的参数列表,它将导致多个参数调用fixture功能和所有测试使用它。
autouse:如果True,则为所有测试激活fixture func可以看到它。如果为False则显示需要参考来激活fixture
ids:每个字符串id的列表,每个字符串对应于params这样他们就是测试ID的一部分。如果没有提供ID它们将从params自动生成
name:fixture的名称。这默认为装饰函数的名称。如果fixture在定义它的统一模块中使用,夹具的功能名称将被请求夹具的功能arg遮蔽,解决这个问题的一种方法时将装饰函数命令"fixture_<fixturename>"然后使用"@pytest.fixture(name='<fixturename>')"。
具体阐述一下scope四个参数的范围
scope="function"
@pytest.fixture()如果不写参数,参数就是scope="function",它的作用范围是每个测试用例来之前运行一次,销毁代码在测试用例之后运行。
import pytest
@pytest.fixture()
def test1():
a = 'leo'
print('\n传出a')
return a
@pytest.fixture(scope='function')
def test2():
b = '男'
print('\n传出b')
return b
def test3(test1):
name = 'leo'
print('找到name')
assert test1 == name
def test4(test2):
sex = '男'
print('找到sex')
assert test2 == sex
if __name__ == '__main__':
pytest.main('-q test_fixture.py')
输出结果:
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 2 items
test_fixture.py
传出a
.找到name
传出b
.找到sex
[100%]
========================== 2 passed in 0.04 seconds ===========================
放在类中实现结果也是一样的
import pytest
@pytest.fixture()
def test1():
a = 'leo'
print('\n传出a')
return a
@pytest.fixture(scope='function')
def test2():
b = '男'
print('\n传出b')
return b
class TestCase:
def test3(self, test1):
name = 'leo'
print('找到name')
assert test1 == name
def test4(self, test2):
sex = '男'
print('找到sex')
assert test2 == sex
if __name__ == '__main__':
pytest.main(['-s', 'test_fixture.py'])
输出结果:
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 2 items
test_fixture.py
传出a
.找到name
传出b
.找到sex
[100%]
========================== 2 passed in 0.03 seconds ===========================
Process finished with exit code 0
scope="class"
fixture为class级别的时候,如果一个class里面有多个用例,都调用了次fixture,那么此fixture只在此class里所有用例开始前执行一次。
import pytest
@pytest.fixture(scope='class')
def test1():
b = '男'
print('传出了%s, 且只在class里所有用例开始前执行一次!!!' % b)
return b
class TestCase:
def test3(self, test1):
name = '男'
print('找到name')
assert test1 == name
def test4(self, test1):
sex = '男'
print('找到sex')
assert test1 == sex
if __name__ == '__main__':
pytest.main(['-s', 'test_fixture.py'])
输出结果:
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 2 items
test_fixture.py 传出了男, 且只在class里所有用例开始前执行一次!!!
.找到name
.找到sex
[100%]
========================== 2 passed in 0.05 seconds ===========================
Process finished with exit code 0
scope="module"
fixture为module时,在当前.py脚本里面所有用例开始前只执行一次。
import pytest
##test_fixture.py
@pytest.fixture(scope='module')
def test1():
b = '男'
print('传出了%s, 且在当前py文件下执行一次!!!' % b)
return b
def test3(test1):
name = '男'
print('找到name')
assert test1 == name
class TestCase:
def test4(self, test1):
sex = '男'
print('找到sex')
assert test1 == sex
if __name__ == '__main__':
pytest.main(['-s', 'test_fixture.py'])
输出结果:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 2 items
test_fixture.py 传出了男, 且在当前py文件下执行一次!!!
.找到sex
.找到name
[100%]
========================== 2 passed in 0.03 seconds ===========================
Process finished with exit code 0
scope="session"
fixture为session级别是可以跨.py模块调用的,也就是当我们有多个.py文件的用例的时候,如果多个用例只需调用一次fixture,那就可以设置为scope="session",并且写到conftest.py文件里。
conftest.py文件名称时固定的,pytest会自动识别该文件。放到项目的根目录下就可以全局调用了,如果放到某个package下,那就在改package内有效。
文件目录为
import pytest
# conftest.py
@pytest.fixture(scope='session')
def test1():
sex = '男'
print('获取到%s' % sex)
return sex
import pytest
# test_fixture.py
def test3(test1):
name = '男'
print('找到name')
assert test1 == name
if __name__ == '__main__':
pytest.main(['-s', 'test_fixture.py'])
import pytest
# test_fixture1.py
class TestCase:
def test4(self, test1):
sex = '男'
print('找到sex')
assert test1 == sex
if __name__ == '__main__':
pytest.main(['-s', 'test_fixture1.py'])
如果需要同时执行两个py文件,可以在cmd中在文件py文件所在目录下执行命令:pytest -s test_fixture.py test_fixture1.py
执行结果为:
================================================= test session starts =================================================
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:
collected 2 itemstest_fixture.py 获取到男
找到name
.
test_fixture1.py 找到sex
.============================================== 2 passed in 0.05 seconds ===============================================
调用fixture的三种方法
1.函数或类里面方法直接传fixture的函数参数名称
import pytest
# test_fixture1.py
@pytest.fixture()
def test1():
print('\n开始执行function')
def test_a(test1):
print('---用例a执行---')
class TestCase:
def test_b(self, test1):
print('---用例b执行')
输出结果:
test_fixture1.py
开始执行function
.---用例a执行---
开始执行function
.---用例b执行
[100%]
========================== 2 passed in 0.05 seconds ===========================
Process finished with exit code 0
2.使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例
import pytest
# test_fixture1.py
@pytest.fixture()
def test1():
print('\n开始执行function')
@pytest.mark.usefixtures('test1')
def test_a():
print('---用例a执行---')
@pytest.mark.usefixtures('test1')
class TestCase:
def test_b(self):
print('---用例b执行---')
def test_c(self):
print('---用例c执行---')
if __name__ == '__main__':
pytest.main(['-s', 'test_fixture1.py'])
输出结果:
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 3 items
test_fixture1.py
开始执行function
.---用例a执行---
开始执行function
.---用例b执行---
开始执行function
.---用例c执行---
[100%]
========================== 3 passed in 0.06 seconds ===========================
Process finished with exit code 0
叠加usefixtures
如果一个方法或者一个class用例想要同时调用多个fixture,可以使用@pytest.mark.usefixture()进行叠加。注意叠加顺序,先执行的放底层,后执行的放上层。
import pytest
# test_fixture1.py
@pytest.fixture()
def test1():
print('\n开始执行function1')
@pytest.fixture()
def test2():
print('\n开始执行function2')
@pytest.mark.usefixtures('test1')
@pytest.mark.usefixtures('test2')
def test_a():
print('---用例a执行---')
@pytest.mark.usefixtures('test2')
@pytest.mark.usefixtures('test1')
class TestCase:
def test_b(self):
print('---用例b执行---')
def test_c(self):
print('---用例c执行---')
if __name__ == '__main__':
pytest.main(['-s', 'test_fixture1.py'])
输出结果:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 3 items
test_fixture1.py
开始执行function2
开始执行function1
.---用例a执行---
开始执行function1
开始执行function2
.---用例b执行---
开始执行function1
开始执行function2
.---用例c执行---
[100%]
========================== 3 passed in 0.03 seconds ===========================
Process finished with exit code 0
usefixtures与传fixture区别
如果fixture有返回值,那么usefixture就无法获取到返回值,这个是装饰器usefixture与用例直接传fixture参数的区别。
当fixture需要用到return出来的参数时,只能讲参数名称直接当参数传入,不需要用到return出来的参数时,两种方式都可以。
fixture自动使用autouse=True
当用例很多的时候,每次都传这个参数,会很麻烦。fixture里面有个参数autouse,默认是False没开启的,可以设置为True开启自动使用fixture功能,这样用例就不用每次都去传参了
autouse设置为True,自动调用fixture功能
import pytest
# test_fixture1.py
@pytest.fixture(scope='module', autouse=True)
def test1():
print('\n开始执行module')
@pytest.fixture(scope='class', autouse=True)
def test2():
print('\n开始执行class')
@pytest.fixture(scope='function', autouse=True)
def test3():
print('\n开始执行function')
def test_a():
print('---用例a执行---')
def test_d():
print('---用例d执行---')
class TestCase:
def test_b(self):
print('---用例b执行---')
def test_c(self):
print('---用例c执行---')
if __name__ == '__main__':
pytest.main(['-s', 'test_fixture1.py'])
输出结果:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\exercise, inifile:collected 4 items
test_fixture1.py
开始执行module
开始执行class
开始执行function
.---用例a执行---
开始执行class
开始执行function
.---用例d执行---
开始执行class
开始执行function
.---用例b执行---
开始执行function
.---用例c执行---
[100%]
conftest.py的作用范围
一个工程下可以建多个conftest.py的文件,一般在工程根目录下设置的conftest文件起到全局作用。在不同子目录下也可以放conftest.py的文件,作用范围只能在改层级以及以下目录生效。
项目实例:
目录结构:
1.conftest在不同的层级间的作用域不一样
# conftest层级展示/conftest.py
import pytest
@pytest.fixture(scope='session', autouse=True)
def login():
print('----准备登录----')
# conftest层级展示/sougou_login/conftest
import pytest
@pytest.fixture(scope='session', autouse=True)
def bai_du():
print('-----登录百度页面-----')
# conftest层级展示/sougou_login/login_website
import pytest
class TestCase:
def test_login(self):
print('hhh,成功登录百度')
if __name__ == '__main__':
pytest.main(['-s', 'login_website.py'])
输出结果:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\conftest层级演示\sougou_login, inifile:collected 1 item
login_website.py ----准备登录----
-----登录百度页面-----
.hhh,成功登录百度
[100%]
========================== 1 passed in 0.03 seconds ===========================
Process finished with exit code 0
2.conftest是不能跨模块调用的(这里没有使用模块调用)
# conftest层级演示/log/contfest.py
import pytest
@pytest.fixture(scope='function', autouse=True)
def log_web():
print('打印页面日志成功')
# conftest层级演示/log/log_website.py
import pytest
def test_web():
print('hhh,成功一次打印日志')
def test_web1():
print('hhh,成功两次打印日志')
if __name__ == '__main__':
pytest.main(['-s', 'log_website.py'])
输出结果:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-4.0.2, py-1.7.0, pluggy-0.8.0
rootdir: C:\Program Files\PycharmProjects\conftest层级演示\log, inifile:
collected 2 items
log_website.py ----准备登录----
打印页面日志成功
hhh,成功一次打印日志
.打印页面日志成功
hhh,成功两次打印日志
.
========================== 2 passed in 0.02 seconds ===========================
来源:https://www.cnblogs.com/huizaia/p/10331469.html
猜你喜欢
- ASP中RegExp是什么 '名字字符检验Public Function CheckName(Str) &nbs
- 大家可能都不大熟悉关于pdb这个模块,实际上就是python中的内置模块,主要作用于命令行调试代码,下面我们将通过是哪个小结给大家详细介绍下
- <!--#include file="strcheck.asp"--> <% '笔者在写程序的
- 在目标检测中一个很重要的问题就是NMS及IOU计算,而一般所说的目标检测检测的box是规则矩形框,计算IOU也非常简单,有两种方法:1. 两
- 任何一个交互过程的操作,对于用户来说都有学习成本,谁也不能保证所有人都可以准确无误地走完一个流程。交互设计师在设计时应该考虑适时地给用户相应
- 我在使用python读取几十万行的文件中的数据,并构造字典,列表等数据结构时,再访问字典,列表时,一般都会出现内存不够的问题,然后只能循环读
- 本文实例讲述了PHP日志LOG类定义与用法。分享给大家供大家参考,具体如下:<?php/*** PHP log 类 */class C
- form无论是在网站的制作中,还是在网站的重构中,我们都会频繁地“碰面”,当“碰面”的次数多了,反而觉得他更让人迷茫,有种熟悉的“陌生”,越
- 在之前得文章中我们说过,如果使用delete对数据库中得表进行删除,那么只是把记录删除掉,并且id的值还会保持上次的状态。即删除之前如果有四
- Silverlight和Flash,到底谁更强?谁更有优势?很多初接触Silverlight和Flash的人总是会问这个问题,因为它们在表面
- 导读:分析时间序列数据的一种简单而有效的方法就是将时间序列数据可视化在一个图表上,这样我们就可以从中推断出某些假设。本文将以股价数据集为例,
- 这篇文章主要介绍了python导入不同目录下的自定义模块过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
- 我就废话不多说了,还是直接看代码吧!a = [1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1]b = ['
- 如下所示:from win32com.client import constantsimport osimport win32com.cli
- 本文实例讲述了python有证书的加密解密实现方法。分享给大家供大家参考。具体实现方法如下:最近在做python的加解密工作,同时加完密的串
- 这句代码在IE9之前曾被称为世界上最短的IE判定代码。代码虽短但确包含了不少javascript基础知识在里面。var ie&nb
- 在SQL Server 中,如果给表的一个字段设置了默认值,就会在系统表sysobjects中生成一个默认约束。如果想删除这个设置了默认值的
- 前言终于下定决心学习Python了。既然从头开始,就需要认认真真。首先需要说的是,我是初学Python,这篇文章只是用于展示global和n
- 5.0版本和之前版本的差异较大,本篇对熟悉3.2版本的用户给出了一些5.0的主要区别。URL和路由5.0的URL访问不再支持普通URL模式,
- 因一些特殊需求需要以参数的形式获取字典 * 定的值,网上搜了一下并没有特别好的实现(并没有太认真去找~),所以自己实现了一个,以供大家参考:)