IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> 利用pytest_collection_modifyitems调整pytest运行顺序的坑 -> 正文阅读

[开发测试]利用pytest_collection_modifyitems调整pytest运行顺序的坑

对pytest的测试用例运行顺序想做一下调整,在使用pytest_collection_modifyitems的过程中掉到一个坑里,特此记录一下。

需求

  • 简化后的需求就是,希望pytest按照原来的反序执行测试用例
  • 通过pytest_collection_modifyitems来实现
  • 不用pytest-ordering,是因为不想把这个顺序写死在case里
  • 脚本
$ cat test.py 

import pytest
 
class TestDemoA:
 
    def test_A_003(self):
        print "### 003"
 
    def test_A_001(self):
        print "### 001"
 
    def test_A_004(self):
        print "### 004"

    def test_A_002(self):
        print "### 002"
  • 运行结果,可以看到是按照文件写入测试用例的顺序执行的
$ pytest test.py -s 
============================================================================================================ test session starts ============================================================================================================
platform linux2 -- Python 2.7.17, pytest-4.6.11, py-1.10.0, pluggy-0.13.1
rootdir: /home/centec/test
collected 4 items                                                                                                                                                                                                                           

test.py ### 003
.### 001
.### 004
.### 002
.

========================================================================================================= 4 passed in 0.01 seconds ==========================================================================================================

尝试

  • 网上搜了几篇讲pytest_collection_modifyitems的文章,都是说改一下items就行了
  • 于是添加了conftest.py
$ cat conftest.py 

import pytest

def pytest_collection_modifyitems(session, config, items):
    items = items[::-1]
    print items
  • 运行发现items的顺序确实反过来了,但是执行顺序并没有改变!
$ pytest test.py -s 
============================================================================================================ test session starts ============================================================================================================
platform linux2 -- Python 2.7.17, pytest-4.6.11, py-1.10.0, pluggy-0.13.1
rootdir: /home/centec/test
collecting ... [<Function test_A_002>, <Function test_A_004>, <Function test_A_001>, <Function test_A_003>]
collected 4 items                                                                                                                                                                                                                           

test.py ### 003
.### 001
.### 004
.### 002
.

========================================================================================================= 4 passed in 0.01 seconds ==========================================================================================================

问题定位

  • 给一小段代码,品一品
>>> a=[3,1,4,2]
>>> id(a)
139800452051904
>>> 
>>> a=a[::-1]
>>> a
[2, 4, 1, 3]
>>> id(a)
139800452053344
>>> 

>>> b=[3,1,4,2]
>>> id(b)
140105279067072
>>> 
>>> b[:]=b[::-1]
>>> b
[2, 4, 1, 3]
>>> id(b)
140105279067072
>>> 
  • 结论就是不能改变items本身(其实就是指针),只能改变items列表中的值

修改

  • 修改后的conftest.py
$ cat conftest.py 

import pytest

def pytest_collection_modifyitems(session, config, items):
    items[:] = items[::-1]
    print items
  • 运行后,确认按照逆序执行
$ pytest test.py -s 
============================================================================================================ test session starts ============================================================================================================
platform linux2 -- Python 2.7.17, pytest-4.6.11, py-1.10.0, pluggy-0.13.1
rootdir: /home/centec/test
collecting ... [<Function test_A_002>, <Function test_A_004>, <Function test_A_001>, <Function test_A_003>]
collected 4 items                                                                                                                                                                                                                           

test.py ### 002
.### 004
.### 001
.### 003
.

========================================================================================================= 4 passed in 0.01 seconds ==========================================================================================================
  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-07-22 23:05:08  更:2021-07-22 23:05:12 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/2 3:50:35-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码