美文网首页
pytest学习二

pytest学习二

作者: 0981b16f19c7 | 来源:发表于2019-08-14 14:40 被阅读0次

万能的标签

标记和分类用例: @pytest.mark.level1

1)在用例上增加: @pytest.mark.runtest
2)在运行时增加,-m标签:pytest.main(['-v', '--html=./report/{}'.format(report_name), '--self-contained-html', '-m', 'runtest','--timeout=300','--reruns=4','--reruns-delay=1'])

标记用例执行顺顺序pytest.mark.run(order=1) (需安装pytest-ordering)

标记用例在指定条件下跳过或直接失败 @pytest.mark.skipif()/xfail()

标记使用指定fixture(测试准备及清理方法) @pytest.mark.usefixtures()

详见:https://www.jianshu.com/p/1761c1c2b807

参数化 @pytest.mark.parametrize

#单个参数化
@pytest.mark.parametrize('x',[0,1])

def test_foo(x):
    print("测试数据组合:x->%s"%(x))

'''
结果:
Test_demo.py .测试数据组合:x->0
.测试数据组合:x->1
'''

#同时声明多个配对参数
@pytest.mark.parametrize('x,y',[(0,1),(1,2)])

def test_foo(x,y):
    print("测试数据组合:x->%s,y->%s"%(x,y))

'''
结果:
Test_demo.py .测试数据组合:x->0,y->1
.测试数据组合:x->1,y->2
'''
#堆叠参数化装饰器
@pytest.mark.parametrize('x',[0,1])
@pytest.mark.parametrize('y',[2,3])

def test_foo(x,y):
    print("测试数据组合:x->%s,y->%s"%(x,y))

'''
结果:
Test_demo.py .测试数据组合:x->0,y->2
.测试数据组合:x->1,y->2
.测试数据组合:x->0,y->3
.测试数据组合:x->1,y->3
'''

标记超时时间 @pytest.mark.timeout(60) (需安装pytest-timeout)

1)在用例上增加: @pytest.mark.timeout(60)
2)在运行时增加,--timeout:pytest.main(['-v', '--html=./report/{}'.format(report_name), '--self-contained-html', '-m', 'runtest','--timeout=300','--reruns=4','--reruns-delay=1'])

标记失败重跑次数为5次,延迟1秒后重跑@pytest.mark.flaky(reruns=5, reruns_delay=1) (需安装pytest-rerunfailures)

1)在执行用例上增加: @pytest.mark.flaky(reruns=2, reruns_delay=1)
2)在运行时增加,--reruns,--reruns-delay:pytest.main(['-v', '--html=./report/{}'.format(report_name), '--self-contained-html', '-m', 'runtest','--timeout=300','--reruns=4','--reruns-delay=1'])
3)用例上的设置会覆盖运行时的设置。

相关文章

  • pytest学习二

    万能的标签 标记和分类用例: @pytest.mark.level1 1)在用例上增加: @pytest.mar...

  • pytest学习博客列表

    本来打算自己写pytest的一些内容,但是在学习pytest过程中,发现了几个不错的pytest学习系列,所以就不...

  • Pytest学习-通过hooks函数(pytest_runtes

    Pytest中提供了很多钩子函数,可以方便我们基于此进行二次开发,另外通过对Pytest钩子函数的学习,我们也能够...

  • pytest学习笔记--(一)

    这两天在学习pytest,之前有小用到pytest,觉得这个测试框架很灵巧,用在实现接口自动化(pytest+re...

  • pytest的fixture学习二

    测试函数可以通过将fixture对象命名为输入参数来接受。对于每个参数名称,具有该名称的fixture函数将提供f...

  • Pytest和Allure测试框架-超详细版+实战2

    二:Pytest -断言、跳过及运行 1,Pytest -断言、跳过及运行 2,mark中的skip(跳过) 3,...

  • pytest学习

    一.环境安装 二.pytest 三.allure

  • pytest学习

    使用环境准备 python==3.6.7 pycharm pytest 5.0.1 pytest用例规范 测试文件...

  • pytest.main()应用介绍

    执行pytest架构代码,两种方式 一、pytest test_xxx.py 二、python xxx.py, 其...

  • Pytest学习2 -断言

    一、前言 学习pytest总会习惯性的和unittest对比使用,自然就断言pytest和unittest也是有些...

网友评论

      本文标题:pytest学习二

      本文链接:https://www.haomeiwen.com/subject/zpcgjctx.html