pytest学习实践(一)
pytest是python中一个很好用的框架,主要特点:
- 支持参数化(不用unittest一样接ddt)
- 支持简单单元测试及复杂功能测试,可以用来做selenium和appium,以及接口自动化(pytest+request)
- 众多第三方插件:pytest-html(报告)、pytest-selenium(集成selenium)、pytest-rerunfailures(失败重跑)等
- 测试用来skip和xfail处理
- CI方便
pytest插件大全
安装
测试
写一个简单的测试函数:
1 2 3 4 5 6 7
| import os
def test_func1(): assert 1 == 1
if __name__ == '__main__': os.system('pytest')
|
运行结果:
1 2 3 4 5 6 7
| collected 1 item
test_1.py . [100%]
========================== 1 passed in 0.06 seconds ===========================
Process finished with exit code 0
|
pytest以.
表示测试成功,可以看一下测试失败:
1 2 3 4 5 6 7
| import os
def test_func1(): assert 1 == 2
if __name__ == '__main__': os.system('pytest')
|
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13
| collected 1 item
test_1.py F [100%]
================================== FAILURES =================================== _________________________________ test_func1 __________________________________
def test_func1(): > assert 1 == 2 E assert 1 == 2
test_1.py:5: AssertionError ========================== 1 failed in 0.11 seconds ===========================
|
F
表示运行失败