pytest是 一个第三方单元测试框架。
pytest比unittest更加简单、灵活,而且提供了更加丰富的扩展。
- 不用创建测试类
- 断言使用python 的 assert
- 安装 pip install pytest
使用规则
- 测试文件、测试函数必须以 test 开头
其他
- 如果使用测试类,该类必须以 Test 开头
- 可以通过 pytest 命令直接执行
- 可以通过pytest.main()方法执行
官方示例
def inc(x):
return x+1
def test_answer():
assert inc(3) == 5
执行:
进入文件所在目录,输入pytest 执行。
PS E:\selenium\Test\pyttest> pytest
==================================================================== test session starts ====================================================================
platform win32 -- Python 3.9.0, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: E:\selenium\Test\pyttest
collected 1 item
test_py.py F [100%]
========================