吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1362|回复: 0
收起左侧

[Python 转载] 【记录】Unitest测试用例执行顺序的自定义

[复制链接]
Test_dada 发表于 2020-11-30 16:17
本帖最后由 Test_dada 于 2020-11-30 16:19 编辑

问题:在Unitest框架执行用例时,会遇到用例执行顺序不是由上至下的,而是根据字母、数字排序来决定执行顺序的
解决方法1:TestSuite套件添加用例,会根据添加顺序执行
       方法2:修改用例名,test_01_xxx、test_02_xxx
       方法3:先pip安装pytest、pytest-ordering库;
                          pytest库下的mark方法,语法如下;
                          @pytest.mark.run(order=x)


例子1:函数执行顺序定义
[Python] 纯文本查看 复制代码
import pytest

@pytest.mark.run(order=3)
def test_01():
print("--------test01")
assert 1

@pytest.mark.run(order=-1)
def test02():
    print("---------test02")
    assert 0

@pytest.mark.run(order=1)
def test_03():
    print("----------test03")

@pytest.mark.run(order=2)
def test04():
    print("------------test04")

if __name__ == '__main__':
    pytest.main(['-s', 'test1.py'])
微信图片_20201130161625.png


例子2:Unitest框架用例执行顺序
[Python] 纯文本查看 复制代码
import pytest
import unittest

class Add_D(unittest.TestCase):
    def setUp(self):
        print('========setUp')
    def tearDown(self):
        print('========tearDown')

    @pytest.mark.run(order=3)
    def test_01(self):
        print("--------test01")
        assert 1

    @pytest.mark.run(order=-1)
    def test02(self):
        print("---------test02")
        assert 0

    @pytest.mark.run(order=1)
    def test_03(self):
        print("----------test03")

    @pytest.mark.run(order=2)
    def test04(self):
        print("------------test04")

if __name__ == '__main__':
    # '-s'显示程序中的print/logging输出
    # ./test1.py 当前文件路径
    # ./test1.py::Add_D 指定要运行的类
    # ./test1.py::Add_D::test04 指定要运行的类下的用例
    pytest.main(['-s','./test1.py::Add_D::test04'])

微信图片_20201130161606.png

参考资料:https://blog.csdn.net/weixin_44006041/article/details/107934174

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
嘚瑟挨顿揍 + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-15 00:25

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表