Pytest插件pytest-order指定用例顺序

安装

 pip install pytest-order
  • 注意不是pytest-ordering

  • 说起来这里有个故事

关于pytest-ordering和pytest-order

https://github.com/ftobia/pytest-ordering

https://github.com/pytest-dev/pytest-order

  • 在写这个文章之前,我用的一直是pytest-ordering

  • 但我在安装pytest-ordering的时候一直看到有pytest-order

  • 今天特意看了下,好家伙

    • GITHUB上写了这么一句:pytest-ordering is no longer maintained, please use https://pypi.org/project/pytest-order/

    • pytest-order is a fork of pytest-ordering that provides additional features like ordering relative to other tests.

    • pytest-order works with Python 3.6 - 3.10, with pytest versions >= 5.0.0 for all versions except Python 3.10, and for pytest >= 6.2.4 for Python 3.10. pytest-order runs on Linux, macOS and Windows.

  • 所以pytest-ordering在当前的pytest及python版本中可能会出现问题,而pytest-order是同步更新的,你可以放心食用

  • 那么问题来了,pytest-order怎么用呢?看官方的示例跟pytest-ordering还是有区别的(如果没有区别,装了2个的话你可能会分不清哪个调用的,当然我们不推荐你用2个)

    • 虽然用起来很简单,但的确蛮细节的,网上你看到的基本都是pytest-ordering的用法,然它在3年前就停止更新了

    • 我把它的git@github.com:pytest-dev/pytest-order.git,下下来,它写了很多的example,嗯~

根据索引排序

  • 其实就一个数字

  • 也可以是与之对应的特定字符,如first等

    字符 数字index
    first 0
    second 1
    last -1
    second_to_last -2
    eighth_to_last -8
  • 示例1

     import pytest
     ​
     ​
     @pytest.mark.order(index=2)
     def test_three():
         print('three')
         assert 3 == 3
     ​
     ​
     @pytest.mark.order('second')
     def test_two():
         print('two')
         assert 2 == 2
     ​
     @pytest.mark.order(-1)
     def test_last():
         print('last')
         assert 'last' == 'last'
     ​
     @pytest.mark.order(0)
     def test_one():
         print('one')
         assert 1 == 1
     ​
     ​
     if __name__ == '__main__':
         pytest.main(['-sv', __file__])
     ​
     test_order_v1.py::test_one one
     PASSED
     test_order_v1.py::test_two two
     PASSED
     test_order_v1.py::test_three three
     PASSED
     test_order_v1.py::test_last last
     PASSED
     ​
     ============================== 4 passed in 0.06s ==============================
     ​
     进程已结束,退出代码为 0
     ​
  • index是从0开始的;切记index=2其实是第三个,first其实是0.所以我们不建议混用

  • -1是最后一个没有问题,索引体系跟list的类似,还是比较好理解的。建议用数字,学习成本就比较低。

  • 这个装饰器可以用到类上

     import pytest
     ​
     ​
     @pytest.mark.order(2)
     class TestA:
         def test_one(self):
             assert 1 == 1
         def test_two(self):
             assert 1 == 1
     ​
     @pytest.mark.order(1)
     class TestB:
         def test_one(self):
             assert 1 == 1
         def test_two(self):
             assert 1 == 1
     ​
     ​
     if __name__ == '__main__':
         pytest.main(['-sv', __file__])
     ​
     test_order_v1.py::TestB::test_one PASSED
     test_order_v1.py::TestB::test_two PASSED
     test_order_v1.py::TestA::test_one PASSED
     test_order_v1.py::TestA::test_two PASSED

排在指定用例后面

  • 直接看示例

     import pytest
     ​
     ​
     @pytest.mark.order(after='test_hallo')
     def test_hello():
         assert 1 == 1
     ​
     ​
     def test_hallo():
         assert 1 == 1
     ​
     ​
     if __name__ == '__main__':
         pytest.main(['-sv', __file__])
     ​
  • 还可以这样写,指定类

     @pytest.mark.order(after="TestB::test_c")
     @pytest.mark.order(after="Test2")
     ​
  • 可以指定文件夹/文件::类::测试用例(即测试函数名),也可以用before

     @pytest.mark.order(before="test_module_c/test_submodule.py::test_2")
     ​
  • 组合也可以

     @pytest.mark.order(index=0, after="test_second")
     @pytest.mark.order(after=["test_second", "other_module.py::test_other"])
     ​
  • 如果是参数化,那就直接用测试函数名

     import pytest
     ​
     @pytest.mark.order(after=["test_second"])
     def test_first():
         assert True
     ​
     @pytest.parametrize(param, [1, 2, 3])
     def test_second(param):
         assert True

说在最后

  • 关于用例的顺序相关的插件是不少的,比如

    • pytest-randomly:随机顺序

    • pytest-reverse:反转(通过一个hook亦可实现)

    • pytest-random-order :随机顺序

    • pytest-depends:依赖

    • pytest-find-dependencies:寻找依赖

  • 写完发现他有个doc,白整了~

     https://pytest-order.readthedocs.io/en/latest/
  •  

Pytest插件pytest-order指定用例顺序的更多相关文章

  1. Pytest(7)自定义用例顺序pytest-ordering

    前言 测试用例在设计的时候,我们一般要求不要有先后顺序,用例是可以打乱了执行的,这样才能达到测试的效果. 有些同学在写用例的时候,用例写了先后顺序, 有先后顺序后,后面还会有新的问题(如:上个用例返回 ...

  2. pytest 运行指定用例

    pytest运行指定用例 随着软件功能的增加,模块越来越多,也意味用例越来越多,为了节约执行时间,快速得到测试报告与结果,在工作中可以通过运行指定用例,达到快速执行用例 例子目录 spec_sub1_ ...

  3. pytest系列(二):筛选用例新姿势,mark 一下,你就知道。

    pytest系列(一)中给大家介绍了pytest的特性,以及它的编写用例的简单至极. 那么在实际工作当中呢,我们要写的自动化用例会比较多,不会都放在一个py文件里. 如下图所示,我们编写的用例存放在不 ...

  4. 查询语句中select from where group by having order by的执行顺序

    查询语句中select from where group by having order by的执行顺序   1.查询中用到的关键词主要包含六个,并且他们的顺序依次为  select--from--w ...

  5. tabindex 带有指定 tab 键顺序 或焦点 focus

    登录注册时,文本框输入焦点 TAB 键时,自定义下一个焦点的顺序 <input type=" /> <input type=" /> 带有指定 tab 键顺 ...

  6. Jquery 插件PrintArea 打印指定的网页区域

    Jquery 插件PrintArea 打印指定的网页区域 需要下载jquery 和printarea.js插件 PrintArea.Js插件,可以打印整个网页中某个指定的区域. $("打印区 ...

  7. rownum和order by的执行顺序问题

    SQL中rownum和order by的执行顺序的问题 : 在一个SQL中,如果同时使用rownum和order by,会有一个先后顺序的问题. 比如select id1,id2 from t_tab ...

  8. unittest 运行slenium(四)---通过指定用例的形式运行用例

    一: 说明 跟数据驱动唯一的区别为用例数据获取时,及运行方式不同. 其它都基本相同,可参考https://www.cnblogs.com/xiaodingdong/p/11753220.html 二: ...

  9. pytest之收集用例规则与运行指定用例

    前言 上篇文章相信大家已经了解了pytest在cmd下结合各种命令行参数如何运行测试用例,并输出我们想要看到的信息.那么今天会讲解一下pytest是如何收集我们写好的用例?我们又有哪些方式来运行单个用 ...

  10. pytest启动浏览器,失败用例截图

    1.conftest.py # coding:utf- from selenium import webdriver import pytest driver = None @pytest.mark. ...

随机推荐

  1. CF240F (26颗线段树计数)

    题目链接:Topcoder----洛谷 题目大意: 给定一个长为n的由a到z组成的字符串,有m次操作,每次操作将[l,r]这些位置的字符进行重排,得到字典序最小的回文字符串,如果无法操作就不进行. 思 ...

  2. I Love Big Numbers !(高精度)

    题目链接 题意: 多组数据输入也就是C++中的: int n; while (cin >> n) { 代码块 } 对于每个数据输出其阶乘的各位上的数字之和.大眼一看,没有思路,那就百度把. ...

  3. golang内置包管理工具go mod简明教程

    go mod go buildin package manager. go mod是go语言内置的包管理工具,集成在go tool中,安装好go就可以使用. 要求: go version >= ...

  4. 读 Clean Code,关于变量命名和可维护代码

    原文见 http://mindprod.com/jgloss/unmain.html 如何写出不能维护的代码 如何程序命名 容易输入的名字.比如:Fred,asdf 单字母的变量名.比如:a,b,c, ...

  5. 机器学习模型评价指标之ROC 曲线、 ROC 的 AUC 和 投资回报率

    前文回顾: 机器学习模型评价指标之混淆矩阵 机器学习模型评价指标之Accuracy.Precision.Recall.F-Score.P-R Curve.AUC.AP 和 mAP 1. 基本指标 1. ...

  6. Fastjsonfan反序列化(1)

    前言 之前只是对FastJson漏洞有简单的一个认知,虽然由于网上fastjson漏洞调试的文章很多,但是真正有着自己的理解并能清楚的讲述出来的文章少之又少.大多文章都是对已知的漏洞调用流程做了大量分 ...

  7. Leetcode-SQL学习计划-SQL入门-584:寻找用户推荐人

    建表语句: Create table If Not Exists Customer (id int, name varchar(25), referee_id int) Truncate table ...

  8. 【大数据-课程】高途-天翼云侯圣文-Day2:离线数仓搭建分解

    一.内容介绍 昨日福利:大数据反杀熟 今日:数据看板 离线分析及DW数据仓库 明日:实时计算框架及全流程 一.数仓定义及演进史 1.概念 生活中解答 2.数据仓库的理解 对比商品仓库 3.数仓分层内容 ...

  9. 解决SpringMVC重定向参数无法携带问题

    解决SpringMVC重定向参数无法携带问题 场景 重定向时请求参数会丢失,我们往往需要重新携带请求参数,我们可以进⾏⼿动参数拼接如下: return "redirect:handle01? ...

  10. ORM执行sql语句 双下划线 外键字段创建 ORM跨表查询

    目录 模型层之ORM执行SQL语句 方式1一 方式二 方式三 神奇的双下划线查询 ORM外键字段的创建 1.创建基础表 2.确定外键关系 3.表的查看 数据的录入 外键字段相关操作 针对一对多 ''' ...