利用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 ==========================================================================================================
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值