pytest总结pytest非常好的一点就是可以支持很多扩展。在安装python工具包的时候,遇到无法访问仓库的情况时,使用-i命令指定仓库地址。通常我选择这个地址:https://mirrors.ustc.edu.cn/p...pytest插件如下,功能介绍:安装pytest:pip3installpytest-ihttps://mirrors。ustc.edu.cn/p...由于要在项目test中重复执行,使用pytest-repeat插件--count=2,指定测试次数。默认是函数级测试--repeat-scope,指定重复执行的域。补充说明:pytest有一个domain的概念,即scope,分为function、class、module、session,其中function是指一个测试函数的开头,class是指以Test开头的类,module是指一个test开头的文件,session是指本次执行的所有用例。所以如果--repeat-scope指定了session,就表示按照session重复。注意:经测试,--xdist命令无法按照预期的repeat-scope执行。示例:pytesttest1.py-s--count=5--repeat-scope=session通过在函数中添加@pytest.mark.repeat(count)修饰来指定函数执行次数。@pytest.mark.repeat(5)deftest_02(start,open_baidu):print("testcasetest_02")pytest-xdist,并发执行多个不依赖的用例说明,--xdist选项的目的是保存testcases执行时间,使用多核执行测试用例,但是有一个前提是这些用例没有依赖关系,因为不能保证依赖关系,及时安装pip3installpytest-xdist-ihttps后面提到的依赖插件://mirrors.ustc.edu.cn/p...用法:pytesttest.py-nauto表示根据当前cpu信息自动分配合理数量的core来运行测试用例,也可以使用pytesttest.py-n3来指定具体的运行核数和并发进程数pytest比较简单,可以保证项目用例的顺序。插件pytest-ordering安装:pip3installpytest-ordering-ihttps://mirrors.ustc.edu.cn/p...使用方法:@pytest.mark.run(order=7)deftest_delete_port(self,host_ip,module_dict,cfg):if"test_create_port"notinmodule_dict:#skippytest.skip("donothavetest_create_portresult")self.delete_port(host_ip,module_dict)@pytest.mark.run(order=8)def复制代码test_delete_subnet(self,host_ip,module_dict,cfg,db_session):如果"test_create_vpc_subnet"不在module_dict:#skippytest.skip("donothavetest_create_portresult")self.delete_subnet(host_ip,module_dict,db_session)两段代码会先执行7再执行8,如果不指定则按照name的顺序执行自定义参数有时候我们需要根据不同的场景传入参数,可以参考下面的代码:#pytest_addoption是pytest已经定义好可以直接使用的fixture(firmware),指定需要解析的字段,以及action表示存储格式defpytest_addoption(parser):parser.addoption("--cmdopt",action="store",default="dev.ini",help="myoption:type1ortype2")parser.addoption("--concurrency",action="store",default='False',help="myoption:concurrency")#定义自己的固件,即其他测试用例使用哪个名称来获取这个定义的--cmdopt参数@pytest.fixture(scope="module")defcmdopt(request):returnrequest.config.getoption("--cmdopt")参考https://blog.csdn.net/waitan2...:生成报告:使用简单的pytest-html插件只需要安装pip3installpytest-html运行命令pytesttest.py--html=./report.html,指定输出文件为report.html报告使用allure工具,界面美观,功能齐全,安装复杂,需要安装allure插件和工具安装allurebrewinstallallureinstall插件pip3installallure-pytest-ihttps://mirrors.ustc.edu.cn/p...执行命令:pytest-vtest_vpc_floatingip.py-nauto--count=5-q--alluredir。/reportalluregenerate--cleanreport/-oallure-results/#生成html格式文件allureopen./allure-results/打开文件备注:allure使用两种方式渲染页面表面是诱惑开放和诱惑服务。前者用于在本地渲染并查看结果,后者用于展示本地渲染后的结果。因此,如果要查看带有数据的html内容,需要使用allureopen。运行命令allureopenallure-report自动打开浏览器(耐心等待)显示渲染结果,其中allure-report为alluregenerate生成的结果所在目录。有人会用pip3installpytest-allure-adaptor-ihttps://mirrors.ustc.edu.cn/p...会报错:INTERNALERROR>AttributeError:module'pytest'hasnoattribute'allure',uninstall并安装lallure-pytest,不过我这里会报错,所以不是用来重复多次执行,allure会多次记录结果,但是用例总数不变,只是a的重复次数某些用例会增加重点:我的项目测试用例之间存在依赖关系,我不想将它们集成在一起丢失很多test_case细节,我想并发执行这个过程多次。这个时候单纯使用repeatorderingxdist插件终究不能满足我的需求。最后,我只能使用如下:mainfunction:defrun(i):file_name='--html=./report/report_{}.html'.format(i[0])pytest.main(["-v","-s",'--log-format="%(asctime)s%(levelname)s%(message)s"','--log-date-format="%Y-%m-%d%H:%M:%S"',file_name,'','--alluredir','./report/result',i[1]])#pytest.main(['-v','--alluredir','report/result'])if__name__=="__main__":importsysiflen(sys.argv)>1:p1='--cmdopt={}'.format(sys.argv[1])num=int(sys.argv[2])else:p1='--cmdopt=dev.ini'num=1importmultiprocessingpool=multiprocessing.Pool(4)_list=[(x,p1)forxinrange(num)]pool.map(run,_list)pool.close()pool.join()关于日志输出:可以指定--log-format="%(asctime)s%(levelname)s%(message)s"和--log-date-format="%Y-%m-%d%H:%M:%S"分别指定日志的格式和日期格式通过pytest.ini的日志指定了pytest运行时参数。pytest的执行可以分为几种,一种是最常见的pytesttest.py形式,第二种是直接执行pytest,第三种是执行pythontest.py但是此时的test。py内容应该是第7节run中pytest.main的运行形式,值得注意的是不管是什么形式pytest都会先找到pytest.ini加载,如果有被覆盖的信息就覆盖,否则pytest会使用.ini内容开启日志功能uselog_cli=true[pytest];addopts=-s-v--html=report/report.html--alluredir./report/result--count=1-nautotestpaths=./#python_files=test_*.py#python_classes=Test*log_cli=truelog_cli_date_format=%Y-%m-%d%H:%M:%Slog_cli_format=%(asctime)s%(levelname)s%(message)slog_cli_level=INFO#log_level=NOTSETlog_file=./logs/pytest-logs.txtpython_functions=测试*参考:https://www.jianshu.com/p/ddb...https://www.cnblogs.com/yoyok...使用skip用例https://blog.csdn.net/qq_42610167/article/细节/101204066?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-任务
