码迷,mamicode.com
首页 > 其他好文 > 详细

利用 configtest.py 定制自己的运行范围 并重跑失败的case

时间:2019-03-12 18:32:54      阅读:491      评论:0      收藏:0      [点我收藏+]

标签:webdriver   get   结果   tor   diff   eid   app   并且   repo   

    先来说一下conftest.py ,这个文件放在不同的路径下,pytest 会根据层级关系来确定其作用范围,官方建议放在项目根目录下,不宜路径太深。

pytest 在启动后会加载配置文件,例如 ini 文件,和这个 conftest.py。

pytest 命令行参数会被传递给 config变量,这是一个全局对象,类似的还有 request, session,items。可以参考 pytest官网的reference。

So,如果在命令行中给出 “--lf” ,那么这个值将会在 config.option.lf 属性被设置成bool True;如果在命令行中给出了特定的测试用例function,这个值将会在 config.args 列表中出现。这两点接下来会用到。

    首先设计第一个,这个测试环境,是stage环境还是dev 环境,两者使用不用的测试数据,比如登录用户名,这个值在command line中设置

# content of project man.py
import pytest
if __name__ == ‘__main__‘:
  pytest.main([‘-v‘,‘--envopt=stage‘]) # content of conftest.py class CaseEnv(): def __init__(self): self.TestURI = None self.AdminUser = None self.AdminUserPWD = None def pytest_addoption(parser): # set testing environment to stage or devasis parser.addoption( "--envopt", action="store", default="stage", help="my option: stage or devasia" #将会在 config.option 中增加 devopt属性,默认值stage ) @pytest.fixture(scope=‘session‘) #级别session可以被class级别的fixture使用 def xyyopt(request): # request is a pytest fixture, envused = request.config.getoption("--envopt") TestData = CaseEnv() if envused == ‘stage‘: TestData.TestURI = ‘https://app.net.cn‘ TestData.AdminUser = ‘admin666‘ TestData.AdminUserPWD = ‘password123‘ else: print ‘Now is not stage environment !!! ‘ return TestData #在其他使用这个fixture的function中 通过 xyyopt.TestURI 就可以得到 webdriver初始化用的测试网址

 在command line 中增加的option,需要送给一个地方接收它,它就是config对象。所以用到了 pytest_addoption(parser)这个hook。随后根据它来确定自定义fixture的返回值,以作后续使用。

 

    接下来,又希望自定义哪些测试用例被执行。用到了 nodeid,它是一个测试function在pytest中的呈现方式,结构类似于这样 “Test_module.py::Test_class::test_function_1”;还用到了另一个hook,pytest_configure(config)。将这些用例 按照pytest的格式列在某一个配置文件中,修改这个列表就可以实现只跑这些用例。

    同样,修改conftest.py 文件:

def pytest_configure(config):

      casesids = MyCaseList.get(‘caseids‘)   # A list of cases ids I want to run  MyCaseList 是我的case 列表文件
      config.args = casesids       # add cases to config just like doing in commond line

 这样,不需要修改command line,就可以划定case的范围,并且,会根据这个case 列表的索引顺序从【0】来执行。

 

  最后,还想要把fail的case再跑一遍,但是为来避免产生的 --junit-xml 结果会覆盖先前的记过,so,对于 --lf 的结果单独写到另一个 xml 中,目前的简单方式是

#content of project main
import pytest
if __name__ == ‘__main__‘:
    pytest.main([‘-v‘,‘--envopt=stage‘])
    pytest.main([‘-v‘,‘--lf‘])     #use it to run previous failed case


#modify conftest.py/pytest_configure(config)

def pytest_configure(config):
    ...
    ...
    ...
    if config.getoption(‘lf‘) is True:             # if command line have --lf option, save run last fail report to different xml file
    config.option.xmlpath = ‘./report-stage-lf.xml‘ else: config.option.xmlpath = ‘./report-stage.xml‘ # specify --junite-xml report file path .. .. ..

 这样在对应路径下将会有两个测试结果xml文件:report-stage.xml,第一次运行自定义好的用例的结果;report-stage-lf.xml,存放了上一次fail的case第二次运行后的结果。写在不同xml文件中的原因是,第二次的--lf 运行会在原来的xml中覆盖结果。

config.option 的属性值在另一个地方记录了下来 https://www.cnblogs.com/xiaoyanxuzi/p/10510722.html

 

利用 configtest.py 定制自己的运行范围 并重跑失败的case

标签:webdriver   get   结果   tor   diff   eid   app   并且   repo   

原文地址:https://www.cnblogs.com/xiaoyanxuzi/p/10518279.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!