pytest学习实践(一)

pytest学习实践(一)

pytest是python中一个很好用的框架,主要特点:

  1. 支持参数化(不用unittest一样接ddt)
  2. 支持简单单元测试及复杂功能测试,可以用来做selenium和appium,以及接口自动化(pytest+request)
  3. 众多第三方插件:pytest-html(报告)、pytest-selenium(集成selenium)、pytest-rerunfailures(失败重跑)等
  4. 测试用来skip和xfail处理
  5. CI方便

pytest插件大全

安装

1
pip install 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表示运行失败

文章目录
  1. 安装
  2. 测试
|