1.用例标签注册的方式
1.1 pytest.ini
[pytest]
markers=
webtest: Run the webtest case
hello: Run the hello case
webtest是标签名称,Run the webtest case是注释
1.2 conftest.py
def pytest_configure(config):
config.addinivalue_line("markers",'webtest')
config.addinivalue_line("markers", 'hello')
2.用例添加标签
test_aa.py
import pytest
@pytest.mark.hello
def test_testFunc1(init_driver):
print('\n我是一个测试用例! in test_testFunc1')
assert 1 == 1
def test_func1():
print('not test')
assert 1 == 1
注:用例打标签的方式为@pytest.mark.标签名
2.1.执行用例与结果
import pytest
pytest.main(["-s","-v","-m","hello"])
-m 用例标签名,只执行打上标签hello的用例
page/test_a.py::test_testFunc1 dakailiulanqi
我是一个测试用例! in test_testFunc1
PASSEDclose
======================= 1 passed, 5 deselected in 0.43s =======================
从执行结果可以看出,一共有6条用例,只执行了1条,其他5条未被选择
3 给测试类加标签
test_b.py
import pytest
@pytest.mark.webtest
class TestClass1(object):
def test_class_func1(self):
print('in test_class_func1')
assert 1 == 1
def test_func1(self):
print('class!')
3.1 执行用例与结果
import pytest
pytest.main(["-s","-v","-m","webtest"])
collecting ... collected 6 items / 4 deselected / 2 selected
page/test_b.py::TestClass1::test_class_func1 in test_class_func1
PASSED
page/test_b.py::TestClass1::test_func1 class!
PASSED
======================= 2 passed, 4 deselected in 0.47s =======================
4.用例同时打两个标签
import pytest
@pytest.mark.hello
@pytest.mark.webtest
def test_testFunc1(init_driver):
print('\n我是一个测试用例! in test_testFunc1')
assert 1 == 1
选中其中一个标签hello,或webtest,该用例都会被执行