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_deletetest_order.py  # 订单相关class TestOrder:def test_order_create:def test_order_list:def test_order_deletetest_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_deletetest_order.py  # 订单相关class TestOrder:def test_order_create:def test_order_list:def test_order_deletetest_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_deletetest_order.py  # 订单相关class TestOrder:def test_order_create:def test_order_list:def test_order_deletetest_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_deletetest_2_order.py  # 订单相关class TestOrder:def test_order_create:def test_order_list:def test_order_deletetest_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_deletetest_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_deletetest_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.pydef 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中即可,简单灵活

pytest多文件执行顺序控制相关推荐

  1. Pytest用例执行顺序控制

    有些场景用例是需要指定顺序执行的,或者直接跳过,或者根据条件跳过 一.默认顺序 二.指定顺序 三不执行 四.条件不执行 一.默认顺序:ASCII码的顺序自动排序 二.order控制 @pytest.m ...

  2. Pytest如何重复执行N次脚本

    [原文链接]Pytest如何重复执行N次脚本 在有些场景下,希望将自动化脚本重复执行多次,比如想看看自动化脚本的稳定性,还比如想利用功能测试用例直接进行压力测试,即对某一些脚本反复执行从而对应用产生压 ...

  3. roslaunch多文件及顺序控制启动

    roslaunch多文件及顺序控制启动 方法一:编写shell文件控制启动顺序 说明 在catkin workspace中,比如src文件夹下有三个pkg,分别为rb_gazebo,rb_descri ...

  4. python 利用pyinstaller 编译.exe文件过程中编写完的.exe文件执行过程中闪退

    问题描述: python 利用pyinstaller 编译.exe文件过程中编写完的.exe文件执行过程中闪退,并提示no module named 'pyproj.datadir' 解决方法: 闪退 ...

  5. boost::log模块实现如何同时对多个文件执行日志记录的测试程序

    boost::log模块实现如何同时对多个文件执行日志记录的测试程序 实现功能 C++实现代码 实现功能 boost::log模块实现如何同时对多个文件执行日志记录的测试程序 C++实现代码 #inc ...

  6. 搞定使用MySQL导入外部的SQL文件执行

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 搞定使用 ...

  7. 将m文件通过matlab 的编译器转换为可执行文件,【转】 Matlab中提高m文件执行效率的小技巧...

    在使用数组或矩阵之前先定义维数 MATLAB中的变量在使用之前不需要明确地定义和指定维数.但当未预定义数组或矩阵的维数时,当需赋值的元素下标超出现有的维数时,MATLAB 就为该数组或矩阵扩维一次,这 ...

  8. Testng执行顺序控制

    1.class执行顺序控制-testng.xml之preserve-order preserve-order:用来控制<test>里面所有<classes>的执行顺序.< ...

  9. Pytest如何并发执行自动化脚本

    [原文链接]Pytest如何并发执行自动化脚本 当自动化脚本数量非常多的时候,全量脚本的执行耗时很长,此时就希望自动化脚本能够并发执行,第三方插件pytest-xdist 能很好的支持此功能,pyte ...

最新文章

  1. 【算法总结】数学问题-最大公约数和最小公倍数
  2. 邮箱如何秘密发送多个人邮件_如何发送秘密消息
  3. Phoenix 关联查询异常 , MaxServerCacheSizeExceededException phoenix.query.maxServerCacheBytes
  4. Confluence 6 理解你许可证的用户数
  5. 【STM32】HAL库 STM32CubeMX教程六----定时器中断
  6. python中的main函数可以被其他文件调用么_Python中在脚本中引用其他文件函数的实现方法...
  7. x264 x264_slicetype_analyse 函数分析
  8. Mapping Text to Knowledge Graph Entities using Multi-Sense LSTMs
  9. exe 反编译 java_.exe文件怎么反编译为java代码(有木有造的)
  10. 1.4.1用空间向量研究直线、平面的位置关系教学设计
  11. 组策略设置计算机计划任务,windows 2008 server 域环境通过组策略下发计划任务
  12. VS2022怎么取消背景或者删除主题
  13. 0-博客笔记导读目录(全部)-20220506backup
  14. Arduino 串口读写函数快慢/时间花费
  15. UNI-APP APP版本更新方法
  16. python发送短信接口_python 调用接口发短信
  17. 哈曼卡顿连接电脑后,突然变的声音好小的解决办法
  18. TensorFlow-similarity 学习笔记13
  19. 为什么计算机屏幕分辨率会,为什么计算机屏幕变窄
  20. Webdw1.0版本已经整合完毕发布到github上了

热门文章

  1. 为树莓派3B+编译 64位UEFI 固件
  2. Windows 10 已禁用输入法
  3. 【工具-DVWA】DVWA的安装和使用
  4. 基于SpringBoot框架图片销售网站的设计与实现
  5. 110配线架打法图解_【布线经验】110语音配线架详细安装教程(图文)
  6. android清单文件的作用,Android 清单文件
  7. 我遇到了bug,请问该如何解决
  8. 【nginx】4xx,5xx 保持自定义header
  9. 最适合跑步用的耳机有哪些、精选五款最优秀的跑步耳机推荐
  10. dpdk内存管理分析