2022/05/12&13学习内容
整理时间:2022/05/14
参考资料:https://www.bilibili.com/video/BV1NM4y1K73T?p=1&spm_id_from=333.851.header_right.history_list.click
进入新阶段-pytest
pytest介绍与安装
1.pytest测试工具:测试函数、类、方法能否正常运行完成
2.pytest特点
3.pytest安装
pytest基本使用
1.运行方式
pytest配置文件
test_py1.py
1.pytest默认规则
2.配置pytest.ini文件
# test_py1.py
# 测试 函数/类/方法 能不能正常运行完成
# pytest 运行方式一
# import pytest
def test_a():
print("test_a")
return 1 * 0
def test_b():
print("test_b")
return 1 / 0
def demo_a():
print("demo_a")
return 1 * 0
def demo_b():
print("demo_b")
return 1 / 0
# if __name__ == '__main__':
# pytest.main(["-s"])
# pytest.ini
[pytest]
addopts = -s
testpaths = ./
python_files = test_* *test.py
python_classes = Test_*
python_functions = test_*
pytest断言
*test_py3.py
1.作用:*测试执行结果是否符合预期
# test_py3.py
# 测试 函数/类/方法 能不能正常运行完成
# 执行的结果是否符合预期
# pytest 运行方式一
import pytest
def test_a():
print("test_a")
assert 1 == 0
def test_b():
print("test_b")
assert "h" in "hello"
if __name__ == '__main__':
pytest.main(["-s", "test_py3.py"])
pytest标记功能mark
test_py4.py
1.标记跳过
2.标记失败:预期会异常的测试
# 测试 函数/类/方法 能不能正常运行完成
# 执行的结果是否符合预期
# pytest 运行方式一
import pytest
def test_a():
print("test_a+++++++++++")
return 1 + 10
# 标记跳过测试
@pytest.mark.skip(reason="想跳过")
def test_b():
print("test_b+++++")
return 1 / 0
# 标记失败
@pytest.mark.xfail(raises=ZeroDivisionError)
def test_c():
print("test_c+++++")
return 1 / 0
if __name__ == '__main__':
pytest.main(["-s", "test_py4.py"])
pytest参数化
test_py5.py
1.添加collections
2.runner
3.参数分析
4.运行结果
# 测试 函数/类/方法 能不能正常运行完成
# 执行的结果是否符合预期
# pytest 运行方式一
import pytest
# 参数化
@pytest.mark.parametrize(["a", "b"], [(1, 2), (90, 100), (10, 200)])
def test_a(a, b):
print("test_a+++++++++++")
assert a + b > 100
if __name__ == '__main__':
pytest.main(["-s", "test_py5.py"])
pytest夹具fixture
1.夹具fixture作用
2.python示例–test_py6.py
# test_py6.py
import pytest
# 模块级别--夹具
def setup_module(args):
print("setup_moudle", args)
def teardown_module(args):
print("teardown_module", args)
# 夹具
# 函数级别--夹具
def setup_function(args):
print("setup_function", args)
def teardown_function(args):
print("teardown_function", args)
# 夹具
def test_func_a():
print('-----', "test_func_a")
def test_func_b():
print('-----', "test_func_b")
class Test_A():
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_methods", args)
# 夹具
# 没写级别则与方法同级--夹具test_py6.py
def setup(self):
print(" ", self.TAG + "setup")
def teardown(self):
print(" ", self.TAG + "teardown")
# 夹具
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_py6.py"])
pytest装饰器夹具
test_py7.py
1.自定义夹具
2.使用夹具
3.参数化使用夹具
# test_py7.py
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)
# 参数化使用夹具
# 执行顺序:
# 1)因为被测方法有 参数名为init_data,所以pytest会找到同名fixture函数
# 2)装饰器上有参数params,长度为3,将进行三次测试函数的调用
# 3)测试前会先执行fixture函数
# 4)在执行fixture函数前组装好request对象,里面有scope/param/ids/name等属性
@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_py7.py"])
pytest插件使用
1.安装插件
2.生成测试报告
3.指定执行顺序
4.失败重试
pytest介绍与安装/基本使用/配置文件
pytest配置文件/断言/标记功能mark/参数化
pytest夹具fixture/装饰器夹具
pytest插件使用