1.Pytest框架简介
Pytest是python的第三方单元测试框架,比自带unittest更简洁和高效;
支持315种以上的插件,同时兼容unittest框架;
在unittest框架迁移到pytest框架的时候不需要重写代码
2.1Pytest环境搭建
见链接:
https://blog.csdn.net/weixin_38567600/article/details/124039016?spm=1001.2014.3001.5502
搭建好:pip show pytest
2.2Pytest执行测试用例
使用pytest执行测试需要遵循的规则
- .py测试文件必须以test开头(或者以_test结尾)
- 测试类必须以Test开头,并且不能有init方法
- 测试方法必须以test_开头
- 断言必须使用assert
Pytest中有四种setup和teardown:
- setup_module和teardown_module在整个测试用例所在的文件中所有的方法运行前和运行后运行,只会运行一次;
- setup_class和teardown_class 则在整个文件中的一个class中所有用例的前后运行;
- setup_method和teardown_method在class内的每个方法运行前后运行;
- setup_function、teardown_function则是在非class下属的每个测试方法的前后运行。
3.1Pytest初体验
import pytest
# 1.定义函数类型---测试用例
#成功断言
def test_tc01():
assert 1+1 ==2 # 断言
# 失败断言
def test_tc02():
assert 1 + 1 == 3 # 断言
if __name__ == '__main__':
pytest.main(['test_func01.py'])
3.2Pytest封装测试类
import pytest
# 2.封装测试类
class TestLogin:#登录模块
def test_login01(self):
assert 1 + 1 == 2 # 断言
def test_login2(self):
assert 1 + 1 == 3 # 断言
if __name__ == '__main__':
pytest.main(['test_func01.py'])
稍稍复杂一点数据驱动
import pytest
# [(1,2),(3,4),(5,6)] [1,2,3]
# 2.封装测试类
class TestLogin:#登录模块
#该测试类---前置操作---初始化
def setup_class(self):# 看业务本身--可选项
print('执行测试类之前,我需要执行操作---------')
@pytest.mark.parametrize('a,b',[(1,2),(3,4),(5,6)]) #('变量名',[1,2,3])
def test_login01(self,a,b):
print('--------test_login01------')
assert a + 1 == b # 断言
def teardown_class(self):# 看业务本身--可选项
print('------该测试类的环境清除-----')
if __name__ == '__main__':
# 需要打印对应的信息 -s
pytest.main(['test_func01.py', '-s'])
#示例代码存放
# 01
import pytest
def test_tc01():
assert 1 + 1 == 2 # 断言
def test_tc02():
assert 1 + 1 == 3 # 断言
if __name__ == '__main__':
pytest.main(['test_func01.py'])
# 02
# 封装测试类
class TestLogin:
def test_login01(self):
assert 1 + 1 == 2
def test_login02(self):
assert 1 + 1 == 3
if __name__ == '__main__':
pytest.main(['test_func01.py'])
# 03
# 显示print里面的信息 +s
class TestLogin:
def test_login01(self):
print('---------test_login01-----------')
assert 1 + 1 == 2
def test_login02(self):
print('---------test_login02-----------')
assert 1 + 1 == 3
if __name__ == '__main__':
# 打印对应的信息
pytest.main(['test_func01.py', '-s'])
# 04 关于teardown的用法
# 封装测试类
class TestLogin:
# 初始化
def setup_class(self):
print('-------执行测试类,我需要执行操作------')
def test_login01(self):
print('---------test_login01-----------')
assert 1 + 1 == 2
def test_login02(self):
print('---------test_login02-----------')
assert 1 + 1 == 3
def teardown_class(self): # 可选项
print('----------该测试类的环境清除------------')
if __name__ == '__main__':
# 打印对应的信息
pytest.main(['test_func01.py', '-s'])
# 05 数据驱动
# [(1,2),(3,4),(5,6)]
# 2封装测试类
class TestLogin:
# 初始化
def setup_class(self):
print('-------执行测试类,我需要执行操作------')
# 数据驱动
# 就是参数化
@pytest.mark.parametrize('a',[1,2,3])
def test_login01(self,a):
print('---------test_login01-----------')
assert 1 + 1 == a # 断言
# def test_login02(self):
# print('---------test_login02-----------')
# assert 1 + 1 == 3
def teardown_class(self): # 可选项
print('----------该测试类的环境清除------------')
if __name__ == '__main__':
# 打印对应的信息
pytest.main(['test_func01.py', '-s'])
# 06 数据驱动【稍微复杂一点的展示】
# [(1,2),(3,4),(5,6)]
# 2封装测试类
class TestLogin:
# 初始化
def setup_class(self):
print('-------执行测试类,我需要执行操作------')
# 数据驱动
# 就是参数化
@pytest.mark.parametrize('a,b',[(1,2),(3,4),(5,6)])
def test_login01(self,a,b):
print('---------test_login01-----------')
assert a + 1 == b # 断言 有几个对的,对的结果不显示,错的显示
# def test_login02(self):
# print('---------test_login02-----------')
# assert 1 + 1 == 3
def teardown_class(self): # 可选项
print('----------该测试类的环境清除------------')
if __name__ == '__main__':
# 打印对应的信息
pytest.main(['test_func01.py', '-s'])