资讯专栏INFORMATION COLUMN

pytest多文件执行顺序控制详解

89542767 / 645人阅读

  默认情况下pytest测试用例的执行顺序是先按照外层后内层(目录下的文件),同层级的包或文件、根据名称、按照ascii码升序执行,文件内的用例根据先后顺序执行,这篇文章主要给大家介绍了关于pytest多文件执行顺序控制的相关资料,需要的朋友可以参考下


  1.只有一个py文件


  1.使用pytest做接口测试,如果测试case只存在于单个.py文件,那么测试case默认从上到下执行,如果使用了pytest-order插件


  2.如果存在多个py文件


  1.使用pytest做接口测试,如果测试case存在于多个.py文件中,那么默认是按照文件名的ascii码顺序执行,进入文件后,默认按照从上到下顺序执行每个单元测试接口。


  test_user.py#用户相关
  class TestUser:
  def test_user_create:
  def test_user_login:
  def test_user_delete
  test_order.py#订单相关
  class TestOrder:
  def test_order_create:
  def test_order_list:
  def test_order_delete
  test_stock.py#库存相关
  class TestStock:
  def test_stock_add:
  def test_stock_list:
  def test_stock_reduce

  1.按照文件名ascii排序:test_order>test_stock>test_user


  2.test_order_create>test_order_list>test_order_delete>test_stock_add>test_stock_list>…


  2.如果单个.py测试文件中使用了pytest-order插件,那么该文件中添加了order的测试用例将会最先执行,没添加的将会按照1的顺序执行,这样就会出现单元测试的顺序在多文件中交叉执行的现象。(所以单个.py文件在使用pytest-order插件的情况下,建议每个case都带上order=x,且x不要相同)


  test_user.py#用户相关
  class TestUser:
  pytest.mark.run(order=1)
  def test_user_create:
  def test_user_login:
  pytest.mark.run(order=2)
  def test_user_delete
  test_order.py#订单相关
  class TestOrder:
  def test_order_create:
  def test_order_list:
  def test_order_delete
  test_stock.py#库存相关
  class TestStock:
  def test_stock_add:
  def test_stock_list:
  def test_stock_reduce

  1.由于test_user文件中的case使用了pytest-order插件,所以优先执行使用了order排序的case


  2.test_user_create>test_user_delete>test_order_create>…>test_stock_add>…>test_user_delete


  3.如果多个.py文件使用了pytest-order插件,如果每个order指定的顺序不冲突,就按照order指定的顺序执行,如果有冲突,那就会出现在多个.py文件中交叉执行,可能不符合我们预期。


  test_user.py#用户相关
  class TestUser:
  pytest.mark.run(order=1)
  def test_user_create:
  def test_user_login:
  pytest.mark.run(order=2)
  def test_user_delete
  test_order.py#订单相关
  class TestOrder:
  def test_order_create:
  def test_order_list:
  def test_order_delete
  test_stock.py#库存相关
  class TestStock:
  pytest.mark.run(order=1)
  def test_stock_add:
  pytest.mark.run(order=2)
  def test_stock_list:
  def test_stock_reduce
  1.test_stock和test_user存在order冲突,所以按照文件名ascii顺序排序
  2.test_stock_add>test_user_create>test_stock_list>test_user_delete>order相关>test_stock_reduce>test_user_login


  4.多个py文件修改按照文件名ascii码排序方式


  需求:不要再多个文件中来回执行case,保证测试用例顺序为:用户模块-->订单模块-->库存模块


  方式一:通过修改文件名,使得文件名ascii码排序,和我们测试case执行顺序一致,确保case中没有pytest-order插件


  test_1_user.py#用户相关
  class TestUser:
  def test_user_create:
  def test_user_login:
  def test_user_delete
  test_2_order.py#订单相关
  class TestOrder:
  def test_order_create:
  def test_order_list:
  def test_order_delete
  test_3_stock.py#库存相关
  class TestStock:
  def test_stock_add:
  def test_stock_list:
  def test_stock_reduce

  但通常情况下,我们.py文件是根据模块去命名的,所以通过修改文件名实现我们预期的执行顺序,并不是很友好


  方式二:如果使用pytest-order插件来控制,必须保证每个文件的order值是不能重复的,后一个.py文件order最小值必须大于前一个.py文件最大值,这样就可以确保文件执行顺序


  这样在增加测试用例后,就可能需要修改很多order顺序


  test_user.py#用户相关
  class TestUser:
  pytest.mark.run(order=1)
  def test_user_create:
  pytest.mark.run(order=3)
  def test_user_login:
  pytest.mark.run(order=2)
  def test_user_delete
  test_order.py#订单相关
  class TestOrder:
  pytest.mark.run(order=4)
  def test_order_create:
  pytest.mark.run(order=5)
  def test_order_list:
  pytest.mark.run(order=6)
  def test_order_delete
  test_stock.py#库存相关
  class TestStock:
  pytest.mark.run(order=7)
  def test_stock_add:
  pytest.mark.run(order=8)
  def test_stock_list:
  pytest.mark.run(order=9)
  def test_stock_reduce


  方式三:通过pytest提供的勾子方法pytest_collection_modifyitems,对case执行顺序进行修改


  #conftest.py
  def pytest_collection_modifyitems(config,items)


  #期望用例顺序按照.py文件执行


  appoint_classes={"TestUser":[],"TestOrder":[],"TestStock":[]}
  for item in items:
  for cls_name in appoint_classes:
  if item.parent.name==cls_name:
  appoint_classes[cls_name].append(item)
  items.clear()
  for cases in appoint_classes.values():
  items.extend(cases)


  用户只需要将其新增的测试模块class按照预期的顺序添加到appoint_classes中即可,简单灵活


  总结


  综上所述,这篇文章就给大家介绍到这里了,希望可以给各位读者带来帮助。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/128334.html

相关文章

  • 利用xdist实现自动化测试用例并行执行

    摘要:在测试行业,如果利用作为脚本语言开发自动化测试用例,可用的框架有等主流可供选择,个人感觉较之和应该算是现阶段最灵活,功能最全面,扩展最丰富的框架了。不知道各位在做自动化的时候有没有遇到过用例数过多,单机执行效率不高的困扰。 在测试行业,如果利用python作为脚本语言开发自动化测试用例,可用...

    tabalt 评论0 收藏0
  • 2021-09-03-接口自动化-python+requests+pytest+csv+yaml+a

    摘要:本套代码和逻辑是本人的劳动成果,如果有转载需要标注,非常适合公司做项目的同学小白也可以学哦接口自动化项目目录公共方法的封装如果不用配置文件可以使用这个方法进行封装但是有一定的缺陷可以不使用字典。这是在正常的命令行解析之前发生的。 ...

    李昌杰 评论0 收藏0
  • python单元测试卷架构pytest详细介绍

      此篇文章详细介绍了python的单元测试卷架构pytest,原文中根据实例编码推荐的十分详尽。对大家学习培训和工作具有很强的参照参考意义,需要的小伙伴可以必须  pytest是python语言表达中一个强悍的单元测试卷架构,用于管理方法和管理功能测试,可运用在单元测试卷、功能测试工作上。  unittest也是python语言表达中一个单元测试卷架构,可是作用比较有限,没有pytest灵便。 ...

    89542767 评论0 收藏0
  • pytest插件探索——hook开发

    摘要:所有的函数都使用的命名规则,以便于查找并且同其他函数区分开来。用来每个,保证被正确的定义。里还有一个选项,用来表示这个函数是个函数。自动注册插件除了常规的方法注册插件,同时提供了方法,允许通过自动注册插件。 前言 参考官方的这篇文章,我尝试翻译其中一些重点部分,并且拓展了相关的pluggy部分的知识。由于pytest是在pluggy基础上构建的,强烈建议先阅读一下pluggy的官方文档...

    shiguibiao 评论0 收藏0
  • 带你深入理解自动化测试框架Pytest的配置文件!

    摘要:其中用到编程等,还需要花更多的精力去深入学习,当每项技能都能掌握到一定深度,才能称为一个完整的知识体系。 都有哪些种类的配置文件 pytest.ini:pytes...

    wayneli 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<