pytest用例打标签

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,该用例都会被执行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值