pytest自动化测试实战,搭建pytest测试框架

  pytest自动化测试实战,搭建pytest测试框架

  问题列表是如何使用pytest运行pycharm中的用例(默认为unittest)来满足pytest:错误:无法识别的参数:xxxparametrize使用ids进行参数化时,控制台中文无法正常显示。显示unicode编码风格(非乱码)pytest-html报告中的中文标题为乱码,解决pytest.ini配置日志文件每次覆盖最后一个日志的问题。pytest.ini的配置每天生成一个日志文件。问题1:如何使用pytest运行pycharm中的一个用例(默认为unittest)。当脚本文件被命名为test_xx.py并且用例以test开始时,运行的代码pycharm将自动识别它作为unittest运行。

  如果要作为pytest运行,需要修改设置默认运行器:文件-设置-工具-python集成工具-测试-默认测试运行器-选择pytest。

  修改后,删除所有单元测试运行配置。

  右键再次执行,查看会显示pytest runner。

  问题2:满足pytest:错误:无法识别的参数:-count=2这种情况有两种情况:

  检查参数拼写是否正确,使用的插件是否安装。您可以使用pip install pytest-repeat来安装相应的插件。问题3:使用ids进行parametrize参数化时,控制台中文无法正常显示,显示为unicode编码样式(非乱码)

现象:参数用例描述有中文时,在控制台输出会显示unicode编码,中文不能正常显示

解决办法:在conftest.py文件使用pytest_collection_modifyitems 钩子函数,对输出的 item.name 和 item.nodeid 重新编码

  py test _ collection _ modify items(items): 测试用例收集完成后,在控制台上显示收集到的item的名称和nodeid的中文:返回: for items:item . name=item . name . encode( UTF-8 )。解码( Unicode _ Escape )项。_ nodeid=item . nodeid . encode( UTF-8 )。再次执行Decode (Unicode _ Escape ),并检查结果:

  问题4:pytest-html报告中的中文标题显示乱码

现象:HTML报告里面用例标题中文显示乱码

解决方案:修改pytest-html目录下的plugin.py里面test_id编码方式,修改路径为:../site-packages/pytest_html/plugin.py

  将代码修改为:

  set self . test _ id=report . nodeid . encode( UTF-8 )。解码(“Unicode _ Escape”)

  修改为:self . test _ id=re sub(r ( u[ s s]{ 4 }),lambda x: x.group (1)。编码(“UTF-8”)。解码( Unicode-Escape ),报告

  plugin.py文件大约有140行长。

  类TestResult: def __init__(self,outcome,report,logfile,config):# self . test _ id=report . nodeid . encode( utf-8 )。decode( unicode _ escape )self . test _ id=re sub(r ( u[ S S]{ 4 }),lambda x: x.group(1)。编码( utf-8 )。decode(unicode-escape ),report.nodeid) if getattr(report, when , call )!=call: self.test_id=:。join([report.nodeid,report . when])self . time=getattr(report, duration ,0.0)self . outcome=outcome self . additional _ html=[]self . links _ html=[]self . self _ contained=config . getoption( self _ contained _ html )self . log file=log files self . config=config self . row _ table=self . row _ extra=none。再次从命令行生成HTML报告:py test-v-s-HTML=reports . HTML-self-contained-HTML test _ params . py。

  问题5:解决pytest.ini配置日志文件每次都会覆盖最后一个日志

现象:每次运行脚本,都会将上一次日志覆盖

的问题。

  

解决方案:修改日志写入模式,mode="a"

  您可以通过修改源代码的编写方式来保存所有的执行日志,并将其更改为模式A来修改文件位置./lib/site-packages/_ pytest/logging . py .将552行self . log _ file _ handler=_ file handler(log _ file,mode= w ,Encoding=UTF-8 )修改为:self . log _ file _ handler=_ file handler(log _ file,mode= a ,encoding= utf-8 )

  问题6: pytest.ini被配置为每天生成一个日志文件

解决方案:修改 pytest下的 logging 模块,主要是给文件名拼接一个日期信息

  修改文件位置./lib/site-packages/_ pytest/logging . py

  Pytest.ini配置:

  logging.py修改:

  密码

  注意日志文件导入时间模块导入时间log _ file _ s=log _ file.partition( . )log _ file=log _ file _ s[0]time . strftime( _ % y _ % m _ % d ,time . local time(time . time())log _ file _ s[1]log _ file _ s[2]问题pytest.ini文件的存储路径

注意:pytest.ini 特别建议放到项目根目录,如果不放在根目录,注意里面 testpaths、norecursedirs 和log_file 与路径相关的参数配置

  路径搜索方法是在pytest.ini中配置的,pytest.ini文件所在的路径是根路径,继续搜索。

  以上内容纯属个人理解。如有不足之处,请指正。转载请注明出处!

  

如果觉得文章不错,欢迎关注微信公众号,微信公众号每天推送相关测试技术文章

pytest自动化测试实战,搭建pytest测试框架