Pytest----插件pytest-attrib即根据属性挑选用例的用法
作者:redrose2100   类别:    日期:2022-12-06 08:31:15    阅读:981 次   消耗积分:0 分

    pytest-attrib 插件是提供根据用例的属性进行挑选用例执行的。类似与mark的功能。Pytest-attrib用在测试类中挑选用例是非常方便的,而且不用使用装饰器,根据测试类的属性进行挑选,不下面首先使用如下命令安装pytest-attrib 插件。

    1. pip install pytest-attrib

    为了更好的显示pytest-attrib插件的用处,这里准备了如下三个测试类,TestDemo类中有smoke属性,TestDemo2中有feature属性,而TestDemo3类中既有smoke又有feature属性。

    1. class TestDemo(object):
    2. smoke=True
    3. def test_01(self):
    4. print("in TestDemo.test_01 ...")
    5. def test_02(self):
    6. print("in TestDemo.test_02 ...")
    7. class TestDemo2(object):
    8. feature = True
    9. def test_03(self):
    10. print("in TestDemo2.test_03 ...")
    11. def test_04(self):
    12. print("in TestDemo2.test_04 ...")
    13. class TestDemo3(object):
    14. feature = True
    15. smoke = True
    16. def test_05(self):
    17. print("in TestDemo3.test_05 ...")
    18. def test_06(self):
    19. print("in TestDemo3.test_06 ...")

    比如想执行带有smoke属性的用例,只需使用 –a 参数指定smoke即可,如下所示。可以看出此时,因为TestDemo类和TestDemo3类中均有smoke属性,因此此两个类中的测试方法均执行。

    1. (demo-HCIhX0Hq) E:\demo>pytest -s -a smoke
    2. =================== test session starts ===================
    3. platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
    4. rootdir: E:\demo, configfile: pytest.ini
    5. plugins: assume-2.4.3, attrib-0.1.3, rerunfailures-10.2
    6. collected 6 items / 2 deselected / 4 selected
    7. test_demo.py in TestDemo.test_01 ...
    8. .in TestDemo.test_02 ...
    9. .in TestDemo3.test_05 ...
    10. .in TestDemo3.test_06 ...
    11. .
    12. ============= 4 passed, 2 deselected in 0.03s =============
    13. (demo-HCIhX0Hq) E:\demo>

    而如果想执行既有smoke属性又有feature属性的测试类的用例,只需要使用and逻辑词连接即可。如下所示,这里同时拥有smoke和feature属性的类即TestDemo3,因此这里将TestDemo3类中的所有测试方法均执行了。

    1. (demo-HCIhX0Hq) E:\demo>pytest -s -a "smoke and feature"
    2. =================== test session starts ===================
    3. platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
    4. rootdir: E:\demo, configfile: pytest.ini
    5. plugins: assume-2.4.3, attrib-0.1.3, rerunfailures-10.2
    6. collected 6 items / 4 deselected / 2 selected
    7. test_demo.py in TestDemo3.test_05 ...
    8. .in TestDemo3.test_06 ...
    9. .
    10. ============= 2 passed, 4 deselected in 0.03s =============
    11. (demo-HCIhX0Hq) E:\demo>

    而如果想执行带有smoke属性的或者带有feature属性的测试类的脚本,只需要使用or连接词连接即可,如下所示,这里带有smoke属性或者带有feature属性的测试类有TestDemo、TestDemo2、TestDemo3,因此这里将此三个类中的所有测试方法均执行了。

    1. (demo-HCIhX0Hq) E:\demo>pytest -s -a "smoke or feature"
    2. =================== test session starts ===================
    3. platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
    4. rootdir: E:\demo, configfile: pytest.ini
    5. plugins: assume-2.4.3, attrib-0.1.3, rerunfailures-10.2
    6. collected 6 items
    7. test_demo.py in TestDemo.test_01 ...
    8. .in TestDemo.test_02 ...
    9. .in TestDemo2.test_03 ...
    10. .in TestDemo2.test_04 ...
    11. .in TestDemo3.test_05 ...
    12. .in TestDemo3.test_06 ...
    13. .
    14. ==================== 6 passed in 0.03s ====================
    15. (demo-HCIhX0Hq) E:\demo>

    而如果想执行不带smoke属性的用例,则需要使用逻辑关系词not来处理,如下所示,可以看出,不带有smoke属性的测试类只有TestDemo2,因此这里只执行了TestDemo2中的测试方法。

    1. (demo-HCIhX0Hq) E:\demo>pytest -s -a "not smoke"
    2. =================== test session starts ===================
    3. platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
    4. rootdir: E:\demo, configfile: pytest.ini
    5. plugins: assume-2.4.3, attrib-0.1.3, rerunfailures-10.2
    6. collected 6 items / 4 deselected / 2 selected
    7. test_demo.py in TestDemo2.test_03 ...
    8. .in TestDemo2.test_04 ...
    9. .
    10. ============= 2 passed, 4 deselected in 0.02s =============
    11. (demo-HCIhX0Hq) E:\demo>
    始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
    版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/483
    个人成就
    • 2022年 : 371 篇 
    • 2023年 : 211 篇 
    • 2024年 : 31 篇 
    • 2025年 : 0 篇 
    • 博客总数: 613 
    • 阅读总量: 638624 
    测试开发技术全栈公众号
    DevOps技术交流微信群