宜昌营销网站建设邯郸制作小程序的公司

当前位置: 首页 > news >正文

宜昌营销网站建设,邯郸制作小程序的公司,移动网站性能,高要网站建设这里写目录标题一、pytest的命名规则二、界面化配置符合命名规则的方法前面会有运行标记三、pytest的用例结构三部分组成四、pytest的用例断言断言写法#xff1a;五、pytest测试框架结构六、pytest参数化用例1、pytest参数化实现方式2、单参数#xff1a;每一条测试数据都会… 这里写目录标题一、pytest的命名规则二、界面化配置符合命名规则的方法前面会有运行标记三、pytest的用例结构三部分组成四、pytest的用例断言断言写法五、pytest测试框架结构六、pytest参数化用例1、pytest参数化实现方式2、单参数每一条测试数据都会生成一条测试用例3、多参数ids:为测试用例起名字ids有中文的情况4、笛卡儿积七、标记测试用例八、pytest设置跳过、预期失败用例1、skip的使用场景2、skipif的使用场景3、xfail的使用场景九、pytest运行测试用例1、运行多个测试包2、运行多个测试模块3、运行多个测试类4、运行多个测试方法5、运行单个测试方法6、运行结果分析十、pytest命令行常用参数1、-x2、–maxfail2用例允许失败1条第2条失败时stop3、-k执行测试用例中包含“tup”的用例采用双引号4、-k执行测试用例中除了“tup”之外的用例采用双引号5、–collect-only只收集不运行十一、python执行pytest1、使用main函数a、pytest.main() 执行当前目录下符合规则的所有测试用例b、运行某一条用例c、运行某个标签2、使用python -m pytest调用pytest十二、pytest异常处理try…exceptpytest.raises()1、捕获异常意料之内的异常不会报错2、捕获异常意料之外的异常会报错3、捕获多个异常4、捕获异常后获取异常值和异常类型一、pytest的命名规则 文件 test开头或者_test结尾 类名 Test开头 方法名 test_开头 特别注意测试类中不能定义init()方法 二、界面化配置 符合命名规则的方法前面会有运行标记 三、pytest的用例结构 三部分组成 用例名称 用例步骤 用例断言 class TestXXX:def setup(self):#资源准备passdef teardown(self):#资源销毁passdef test_xxx(self):#测试步骤1#测试步骤2#断言 实际结果 对比 期望结果logging.info(这是xxx测试用例)assert 11 四、pytest的用例断言 断言(assert)是一种在程序中的一阶逻辑(如:一个结果为真或假的逻辑判断式)目的为了表示与验证软件开发者预期的结果。当程序执行到断言的位置时对应的断言应该为真。若断言不为真时程序会中止执行并给出错误信息。 断言写法 assert 表达式 assert 表达式描述当断言成功时描述语句不会执行当断言失败时描述语句会执行。 class TestDemo:def test_c(self):assert 11def test_c1(self):assert abc1 in abcdef , abc不在abcdef中五、pytest测试框架结构 setup_module/teardown_module全局模块级只运行一次 setup_class/teardown_class类级只在类中前后运行一次 setup_function/teardown_function函数级别在类外 setup_method/teardown_method方法级别在类中。类中每个方法执行前后 setup/teardown在类中在每个测试方法前后执行 六、pytest参数化用例 通过参数的方式传递数据从而实现数据和脚本的分离 并且可以实现用例的重复生成和执行 1、pytest参数化实现方式 装饰器pytest.mark.parametrize 2、单参数每一条测试数据都会生成一条测试用例 #单参数 import pytest search_list[appium,selenium,pytest]# 两组测试数据两条测试用例 pytest.mark.parametrize(search_key,[java,appium]) def test_search_params(search_key):assert search_key in search_list3、多参数 ids:为测试用例起名字 注意ids 列表参数的个数要与参数值的个数一致 import pytest#测试数据有2个参数 pytest.mark.parametrize(username,password,[[kobe,666],[kd,111],[ ,888]],ids[success,fail,username is None]) def test_login1(username,password):print(f登录的用户名:{username},密码{password})ids有中文的情况 import pytestpytest.mark.parametrize(username,password,[[kobe,666],[kd,111],[ ,888]],ids[成功,失败,用户名为空]) def test_login(username,password):print(f登录的用户名:{username},密码{password})需要在conftest.py中定义pytest_collection_modifyitems方法 def pytest_collection_modifyitems(items):测试用例收集完成时将收集到的用例名name和用例标识nodeid的中文信息显示在控制台上for i in items:i.namei.name.encode(utf-8).decode(unicode-escape)i._nodeidi.nodeid.encode(utf-8).decode(unicode-escape)4、笛卡儿积 两组数据a[1,2,3],b[a,b,c] 对应的有几组组合形式 (1,a)(1,b)(1,c) (2,a)(2,b)(2,c) (3,a)(3,b)(3,c) import pytestpytest.mark.parametrize(a,[1,2,3]) pytest.mark.parametrize(b,[aa,bb,cc]) def test_params1(a,b):print(f笛卡儿积形式参数化中,a{a},b{b})七、标记测试用例 场景只执行符合要求的某一部分用例可以把一个web项目划分为多个模块然后指定模块名执行 解决在测试用例方法上加pytest.mark.标签名 执行-m 执行自定义标记的相关用例 import pytest #测试数据整型 pytest.mark.int def test_int():assert isinstance(2,int)#测试数据字符串 pytest.mark.str def test_str():assert isinstance(str,str)#测试数据浮点型 pytest.mark.float def test_float():assert isinstance(2.7,float)执行测试用例 D:\pytest_project\test_paramspytest test_mark.py -vs -m int测试结果 执行用例会出现警告解决方法在pytest.ini中将标签注册 再次执行没有警告了
八、pytest设置跳过、预期失败用例 这是pytest的内置标签可以处理一些特殊的测试用例 skip始终跳过该测试用例 skipif遇到特定情况跳过该测试用例 xfail遇到特定情况产生一个”预期失败“输出 1、skip的使用场景 1、调试时不想运行这个测试用例 2、标记无法在某些平台运行的测试用例 3、在某些版本中执行其他版本中跳过 import pytestpytest.mark.skip def test_aaa():print(代码未开发完)assert Truepytest.mark.skip(reason代码没有实现) def test_bbb():assert False执行测试用例测试用例跳过图标默认置灰 案例做判断 def check_login():return Falsedef test_function():print(start)#如果未登录则跳过后续步骤if not check_login():pytest.skip(unsupported configuration)print(end)2、skipif的使用场景 import sys print(sys.platform)pytest.mark.skipif(sys.platform darwin, reasondoes not run on mac) def test_case1():assert True pytest.mark.skipif(sys.platform win, reasondoes not run on windows) def test_case2():assert True pytest.mark.skipif(sys.version_info (3, 6), reasonrequires python3.6 or higher) def test_case3():assert True3、xfail的使用场景 import pytestpytest.mark.xfail def test_aaa():print(test_xfail1 方法执行)assert 2 2xfail pytest.mark.xfailxfail(reasonbug) def test_hello4():assert 0 九、pytest运行测试用例 1、运行多个测试包 目录结构
运行测试用例 D:\pytest_projectpytest demo_plugin2、运行多个测试模块 运行测试用例 D:\pytest_projectpytest demo_plugin/demo13、运行多个测试类 运行测试用例 D:\pytest_projectpytest demo_plugin/demo2/test_demo2.py4、运行多个测试方法 运行测试用例 D:\pytest_projectpytest demo_plugin/demo2/test_demo2.py::TestB5、运行单个测试方法 运行测试用例 D:\pytest_projectpytest demo_plugin/demo2/test_demo2.py::TestB::test_d6、运行结果分析 常用的pass通过、fail失败、error代码错误 特殊的warning警告例如pytest.ini中没有注册标签名、deselect跳过加标签skip、skipif 十、pytest命令行常用参数 -m执行加标签的测试用例 -k执行测试用例中包含某个关键字的测试用例windows中关键字使用双引号 -x用例一旦运行失败立刻停止运行冒烟测试 – maxfailnum测试用例失败达到num个时立刻停止运行 -v打印详细信息 -s打印输出日志 -n并发执行测试用例 -lf只执行上次用例失败的测试用例 -ff先执行上次用例失败的测试用例再执行其他的测试用例 –collect-only测试平台pytest自动导入功能 #!/usr/bin/env python

-- coding: utf-8 --

Time : 2023/2/15 20:10

Author : 杜兰特

File : test_mark.py

import pytest #测试数据整型 pytest.mark.int def test_int():assert isinstance(2,int)#测试数据字符串 pytest.mark.str def test_str():assert isinstance(str,str)#测试数据浮点型 pytest.mark.float def test_float():assert isinstance(2.7,float)#测试数据列表 pytest.mark.list def test_list():assert isinstance([1,2,3],list)#测试数据元组 pytest.mark.tuple def test_tuple():assert isinstance((1,),tuple)1、-x D:\pytest_project\test_paramspytest test_mark.py -x2、–maxfail2用例允许失败1条第2条失败时stop D:\pytest_project\test_paramspytest test_mark.py –maxfail23、-k执行测试用例中包含“tup”的用例采用双引号 D:\pytest_project\test_paramspytest test_mark.py -k tup4、-k执行测试用例中除了“tup”之外的用例采用双引号 D:\pytest_project\test_paramspytest test_mark.py -k not tup5、–collect-only只收集不运行 十一、python执行pytest 1、使用main函数 a、pytest.main() 执行当前目录下符合规则的所有测试用例 test_first.py文件 import pytestdef test_a():assert 11def test_b():assert 12class TestDemo:def test_c(self):assert 11def test_c1(self):assert abc1 in abcdef , abc不在abcdef中class TestXXX:def setup(self):#资源准备passdef teardown(self):#资源销毁passdef test_xxx(self):#测试步骤1#测试步骤2#断言 实际结果 对比 期望结果assert 11if name main:pass#todo 1、运行当前目录下所有的用例pytest.main() #相当于pytest.main([./])执行测试用例 D:\pytest_projectpython test_first.pyb、运行某一条用例 import pytestif name main:#todo 2、运行test_parameters.py::test_login中的某一条用例pytest.main([test_params/test_parameters.py::test_login,-vs])执行测试用例 D:\pytest_projectpython test_first.pyc、运行某个标签 import pytestif name main:#todo 3、运行某个标签pytest.main([test_params/test_mark.py,-vs,-m,int])执行测试用例 D:\pytest_projectpython test_first.py2、使用python -m pytest调用pytest D:\pytest_projectpython -m pytest test_first.py十二、pytest异常处理 try…except pytest.raises() 可以捕获特定的异常 获取捕获的异常的细节异常类型、异常信息 发生异常后面的代码将不会被执行 1、捕获异常意料之内的异常不会报错 import pytest def test_raise():with pytest.raises(expected_exceptionValueError, matchmust be 0 or None):raise ValueError(value must be 0 or None)2、捕获异常意料之外的异常会报错 def test_raise1():with pytest.raises(expected_exceptionValueError, matchmust be 0 or None):raise ZeroDivisionError(除数为0的)3、捕获多个异常 def test_raise2():with pytest.raises(expected_exception(ValueError,ZeroDivisionError), matchmust be 0 or None):raise ZeroDivisionError(value must be 0 or None)4、捕获异常后获取异常值和异常类型 def test_raise2():with pytest.raises(ValueError) as exc_info:raise ValueError(value must be 42)assert exc_info.type is ValueErrorassert exc_info.value.args[0] value must be 42