Pytest的安装和基础使用
一、pytest的安装
- pytest 安装
pip install pytest
,使用5.2.1
二、pytest常用的运行模式介绍
-
test_01.py
import pytest def test_b(): print("测试b函数") assert 1 def test_a(): print("测试a函数") assert 0 if __name__ == '__main__': pytest.main()
2.1 默认参数模式(pytest test_01.py、pytest.main())
-
默认运行方式—> 终端模式:pytest test_01.py 测试类主函数模式: pytest.main()
================================================================================ test session starts ================================================================================ platform win32 -- Python 3.6.8, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 rootdir: C:\Users\LGY\Desktop\pytest-learn collected 2 items test_01.py .F [100%] ===================================================================================== FAILURES ====================================================================================== ______________________________________________________________________________________ test_a _______________________________________________________________________________________ def test_a(): print("测试a函数") > assert 0 E assert 0 test_01.py:9: AssertionError ------------------------------------------------------------------------------- Captured stdout call -------------------------------------------------------------------------------- 测试a函数 ============================================================================ 1 failed, 1 passed in 0.03s ============================================================================
2.1 -s参数运行模式(pytest -s test_01.py、pytest.main(["-s"])):关闭捕捉,输出打印信息
================================================================================ test session starts ================================================================================
platform win32 -- Python 3.6.8, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\LGY\Desktop\pytest-learn
collected 2 items
test_01.py 测试b函数
.测试a函数
F
===================================================================================== FAILURES ======================================================================================
______________________________________________________________________________________ test_a _______________________________________________________________________________________
def test_a():
print("测试a函数")
> assert 0
E assert 0
test_01.py:9: AssertionError
============================================================================ 1 failed, 1 passed in 0.03s ============================================================================
结果分析:跟默认运行相比函数里面的打印信息输出到终端,而默认的运行方式只有错误的测试用例输出在Captured stdout call
2.2 -q参数运行模式(pytest -q test_01.py、pytest.main(["-q"])):减少测试的运行冗长,简化输出
.F [100%]
===================================================================================== FAILURES ======================================================================================
______________________________________________________________________________________ test_a _______________________________________________________________________________________
def test_a():
print("测试a函数")
> assert 0
E assert 0
test_01.py:9: AssertionError
------------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------------
测试a函数
1 failed, 1 passed in 0.04s
2.3 -v参数运行模式(pytest -v test_01.py、pytest.main(["-v"])):增加测试的运行冗长,详细输出
================================================================================ test session starts ================================================================================
platform win32 -- Python 3.6.8, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:\users\lgy\pytest-env\scripts\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\LGY\Desktop\pytest-learn
collected 2 items
test_01.py::test_b PASSED [ 50%]
test_01.py::test_a FAILED [100%]
===================================================================================== FAILURES ======================================================================================
______________________________________________________________________________________ test_a _______________________________________________________________________________________
def test_a():
print("测试a函数")
> assert 0
E assert 0
test_01.py:9: AssertionError
------------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------------
测试a函数
============================================================================ 1 failed, 1 passed in 0.05s ============================================================================
2. 4-k参数运行模式(pytest -k test_01.py、pytest.main(["-k"])):指定执行的测试用例。
- pytest -k “test_a” :执行当前目前所有包含
test_a
py(比如test_01.py 、test_02.py 中的所有test_a函数)文件中的测试函数 - pytest -k “test_a” test_01.py :执行
test_01.py
中test_a
函数
2.5 pytest dir:执行当前目录下的用例
比如pytest -v ./abc
:执行abc目录下的执行用例