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文件
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
[pytest]
addopts = -s
testpaths = ./
python_files = test_* *test.py
python_classes = Test_*
python_functions = test_*
pytest断言
test_py3.py
1.作用:测试执行结果是否符合预期
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.标记失败:预期会异常的测试
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.运行结果
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
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)
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.参数化使用夹具
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_py7.py"])
pytest插件使用
1.安装插件
2.生成测试报告
3.指定执行顺序
4.失败重试
Original: https://blog.csdn.net/weixin_49079904/article/details/124764152
Author: Joy_ou悠
Title: 软测05/12&13|记录一下学习过程|pytest
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/776359/
转载文章受原作者版权保护。转载请注明原作者出处!