【原文链接】Pytest----插件pytest-attrib即根据属性挑选用例的用法
pytest-attrib 插件是提供根据用例的属性进行挑选用例执行的。类似与mark的功能。Pytest-attrib用在测试类中挑选用例是非常方便的,而且不用使用装饰器,根据测试类的属性进行挑选,不下面首先使用如下命令安装pytest-attrib 插件。
pip install pytest-attrib
为了更好的显示pytest-attrib插件的用处,这里准备了如下三个测试类,TestDemo类中有smoke属性,TestDemo2中有feature属性,而TestDemo3类中既有smoke又有feature属性。
class TestDemo(object):
smoke=True
def test_01(self):
print("in TestDemo.test_01 ...")
def test_02(self):
print("in TestDemo.test_02 ...")
class TestDemo2(object):
feature = True
def test_03(self):
print("in TestDemo2.test_03 ...")
def test_04(self):
print("in TestDemo2.test_04 ...")
class TestDemo3(object):
feature = True
smoke = True
def test_05(self):
print("in TestDemo3.test_05 ...")
def test_06(self):
print("in TestDemo3.test_06 ...")
比如想执行带有smoke属性的用例,只需使用 –a 参数指定smoke即可,如下所示。可以看出此时,因为TestDemo类和TestDemo3类中均有smoke属性,因此此两个类中的测试方法均执行。
(demo-HCIhX0Hq) E:\demo>pytest -s -a smoke
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo, configfile: pytest.ini
plugins: assume-2.4.3, attrib-0.1.3, rerunfailures-10.2
collected 6 items / 2 deselected / 4 selected
test_demo.py in TestDemo.test_01 ...
.in TestDemo.test_02 ...
.in TestDemo3.test_05 ...
.in TestDemo3.test_06 ...
.
============= 4 passed, 2 deselected in 0.03s =============
(demo-HCIhX0Hq) E:\demo>
而如果想执行既有smoke属性又有feature属性的测试类的用例,只需要使用and逻辑词连接即可。如下所示,这里同时拥有smoke和feature属性的类即TestDemo3,因此这里将TestDemo3类中的所有测试方法均执行了。
(demo-HCIhX0Hq) E:\demo>pytest -s -a "smoke and feature"
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo, configfile: pytest.ini
plugins: assume-2.4.3, attrib-0.1.3, rerunfailures-10.2
collected 6 items / 4 deselected / 2 selected
test_demo.py in TestDemo3.test_05 ...
.in TestDemo3.test_06 ...
.
============= 2 passed, 4 deselected in 0.03s =============
(demo-HCIhX0Hq) E:\demo>
而如果想执行带有smoke属性的或者带有feature属性的测试类的脚本,只需要使用or连接词连接即可,如下所示,这里带有smoke属性或者带有feature属性的测试类有TestDemo、TestDemo2、TestDemo3,因此这里将此三个类中的所有测试方法均执行了。
(demo-HCIhX0Hq) E:\demo>pytest -s -a "smoke or feature"
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo, configfile: pytest.ini
plugins: assume-2.4.3, attrib-0.1.3, rerunfailures-10.2
collected 6 items
test_demo.py in TestDemo.test_01 ...
.in TestDemo.test_02 ...
.in TestDemo2.test_03 ...
.in TestDemo2.test_04 ...
.in TestDemo3.test_05 ...
.in TestDemo3.test_06 ...
.
==================== 6 passed in 0.03s ====================
(demo-HCIhX0Hq) E:\demo>
而如果想执行不带smoke属性的用例,则需要使用逻辑关系词not来处理,如下所示,可以看出,不带有smoke属性的测试类只有TestDemo2,因此这里只执行了TestDemo2中的测试方法。
(demo-HCIhX0Hq) E:\demo>pytest -s -a "not smoke"
=================== test session starts ===================
platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
rootdir: E:\demo, configfile: pytest.ini
plugins: assume-2.4.3, attrib-0.1.3, rerunfailures-10.2
collected 6 items / 4 deselected / 2 selected
test_demo.py in TestDemo2.test_03 ...
.in TestDemo2.test_04 ...
.
============= 2 passed, 4 deselected in 0.02s =============
(demo-HCIhX0Hq) E:\demo>