- 入门案例
- pytest配置文件
- 断言
- 参数化
- 夹具
- 模块级
- 类级
- 方法级
- 函数级
- fixture装饰器
- 插件
unittest:python自带的单元测试框架,兼容性好
pytest:单元测试框架,比自带框架更简洁高效
安装: pip install pytest 入门案例
- 使用pycharm的terminal运行:
这里的文件名只能以test_开头,如果想运行所有的test_,建议使用命令行
import pytest
# 默认只能用test开头
def test_a():
print("test_a")
return 1 * 0
def test_b():
print("test_b")
return 1 / 0
# if __name__ == '__main__':
# pytest.main(["-s","test_py1.py"]) # 显示程序中打印的信息
pytest配置文件
[pytest] addopts = -s 打印 testpaths = ./script 测试模块所在目录 python_files = test_*.py *test.py 测试模块文件名称规则 多个内容用空格分隔 python_classes = Demo* 测试类名称规则 python_functions = demo_* 测试函数或者方法的名称规则断言
# 标记跳过
@pytest.mark.skip(reason='我先跳过')
def test_b():
print("test_b---++++---")
return 1 / 0
# 标记失败
@pytest.mark.xfail(raises=AssertionError)
def test_c():
print("test_c---++++---")
return 1 / 0
参数化
# 参数化
@pytest.mark.parametrize(["a", "b"], [(1, 2), (10, 20), (100, 20), (300, 40)])
def test_a(a, b):
print("test_a_b````````")
assert a + b > 100
夹具
setup:用于初始化环境
teardown:用于清理回收测试资源
整个py文件
import pytest
# 模块级
def setup_module(args):
print("setup_module", args)
def teardown_module(args):
print("teardown",args)
# 函数
def test_func_a():
print('-----------', 'test_func_a')
# 函数
def test_func_b():
print('-----------', 'test_func_b')
# 类
class TestOne():
TAG = "a"
def test_1(self):
print('--------', self.TAG + 'test_1')
def test_2(self):
print('--------', self.TAG + 'test_2')
# 主函数
if __name__ == '__main__':
pytest.main(["-s", "test_py7.py"])
类级
class TestOne():
TAG = "a"
# 在类中加载
def setup_class(self):
print(" "+self.TAG+" setup_class")
def teardown_class(self):
print(" "+self.TAG+" teardown_class")
def test_1(self):
print('--------', self.TAG + 'test_1')
def test_2(self):
print('--------', self.TAG + 'test_2')
方法级
class TestOne():
TAG = "a"
def setup_class(self):
print(" " + self.TAG + " setup_class")
def teardown_class(self):
print(" " + self.TAG + " teardown_class")
# 方法级
def setup_method(self, args):
print(" " +self.TAG + " setup_method", args)
def teardown_method(self, args):
print(" " + self.TAG + "teardown_method", args)
def test_1(self):
print('--------', self.TAG + 'test_1')
def test_2(self):
print('--------', self.TAG + 'test_2')
函数级
# 函数级
def setup_function(args):
print(" setupfunction", args)
def teardown_function(args):
print(" teardownfunction", args)
fixture装饰器
自定义夹具,可以有返回值
import pytest
# 自定义夹具
@pytest.fixture()
def before():
print("before")
@pytest.fixture()
def login():
print("login")
return "user"
# 调用夹具
@pytest.mark.usefixtures("before")
def test_1():
print("test_1---")
def test_2(login):
print("test_2---", login)
@pytest.fixture(params=[1, 2, 3])
def init_data(request):
print("request参数是", request.param)
return request.param
def test_Data(init_data):
assert init_data > 2
if __name__ == '__main__':
pytest.main(["-s", "test_py8.py"])
插件
- html报告
pip install pytest-html
在pytest.ini中设置:
addopts = -s --html=./report.html
- 指定执行顺序
pip install pytest-ordering
import pytest
# 标记了顺序的,正整数或0,运行优先级大于未标记的
# 0和正整数>未标记的>负整数
# 在各个阶段 数越小的运行优先级越高
@pytest.mark.run(order=0)
def test_1():
print("1")
@pytest.mark.run(order=-1)
def test_2():
print("2")
@pytest.mark.run(order=-2)
def test_3():
print("3")
def test_4():
print("4")
@pytest.mark.run(order=2)
def test_5():
print("5")
@pytest.mark.run(order=1)
def test_6():
print("6")
if __name__ == '__main__':
pytest.main(["-s", "test_py9.py"])
- 失败重试
pip install pytest-rerunfailures
在pytest.ini中添加:
addopts = -s --html=./report.html --reruns 3



