hua_pytest

添加mark标记

#运行命令 pytest -m slow
import pytest
class TestClass(object):
    @pytest.mark.slow
    def test_one(self):
        x = 'this'
        assert 'h' in x
    def test_two(self):
        x = 'hello'
        assert hasattr(x,'hello')
去除警告解决方案: Unknown pytest.mark.slow - is this a typo? 
#工作目录下新建conftest.py
def pytest_configure(config):
    config.addinivalue_line(
    "markers", "slow" # 是标签名
    )

使用autos调用fixture

fixture decorator一个optional的参数是autouse, 默认设置为False。
当默认为False,就可以选择用上面方式来调用fixture标识的函数。
当设置为True时,在一个session内的所有的test都会自动调用这个fixture。

作为函数入参fixture

#测试函数可以通过接受一个已经命名的fixture对象来使用他们。对于每个参数名,如果fixture已经声明定义,会自动创建一个实例并传入该测试函数。fixture函数通过装饰器标志@pytest.fixture来注册。
#命令pytest -s
# -s 支持命令行显示打印输出
import pytest
@pytest.fixture()
def run():
    print('111111')
def test_run(run):
    pass

setup和teardown及参数化

#conftest.py
@pytest.fixture(scope='class')
def run():
    print('开始')
    yield
    print('结束')
@pytest.fixture()
def func():
    print('函数开始')
    yield
    print('函数结束')
#test_class.py
import pytest
@pytest.mark.usefixtures('run','func')
class Test_run():
    def test_run(self):
        print('testrun')
    def test_run1(self):
        print('testrun1')
    @pytest.mark.parametrize('a,b',[(1,2),(3,4)])
    def test_parms(self,a,b):
        print(a,b)

skip/skipif

@pytest.mark.skip()
   def test_skip():
       print('跳过')
@pytest.mark.skipif('1>2')
   def test_skipif():
       print('跳过')
#共享跳过
boo = pytest.mark.skipif('1>2', reason="1>2")
@boo
   def test_skipif():
       print('跳过')

XFail

#标记期望失败测试用例

@pytest.mark.xfail()
    def test_function():
        raise
#XFAIL和XPASS的测试用例都不会导致整个测试套失败,除非指定了strict参数为True
# reason
#raises:如果该测试函数并不是因为raises中指定的异常导致的失败,那么该测试函数在测试结果中就会被标 记为正常的失败,如果是raises中的异常导致的失败,那么就会被标记为预期的失败。
#run:如果一个测试应该被标记为xfail并且预期是失败的,但是你又暂时不想运行这个测试用例,那么你可 以将run参数设置为False
#pytest ‐‐runxfail:可以强制让xfail和pytest.xfail失效

生成报告

pytest.main(['--html=./1.html','--alluredir=./allure-results/'])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值