pytest是一个很是成熟的全功能的Python测试框架,适合从简单的单元到复杂的功能测试,主要特色有如下几点:html
一、环境搭建推荐版本匹配:pip install pytest==3.6.3python
二、查看版本:pytest --versionapi
C:\Users\Administrator>pytest --version This is pytest version 三、六、3, imported from d:\path_python\lib\site-packages\pytest、py
三、pytest 命名规则:session
四、pytest 直接写用例,写完 cmd 运行,不须要导入其余模块。框架
G:\python_study\study\pytest_demo\study>pytest -s test_demo1.py ================================================= test session starts ================================================= platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0 rootdir: G:\python_study\study\pytest_demo\study, inifile: collected 2 items
test_demo1.py 我是用例:a . 我是用例:b .
============================================== 2 passed in 0.02 seconds ===============================================
一、运行规则:pytest py文件路径函数
C:\Users\Administrator>pytest G:\python_study\study\pytest_demo\study\test_demo.py ============================= test session starts ============================= platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0 rootdir: C:\Users\Administrator, inifile: collected 4 items test_demo.py .... [100%] ========================== 4 passed in 0.03 seconds ===========================
二、显示打印信息(否则不会看到打印内容):pytest -s xxx单元测试
G:\python_study\study\pytest_demo\study>pytest -s test_demo1.py ================================================= test session starts ================================================= platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0 rootdir: G:\python_study\study\pytest_demo\study, inifile: collected 2 items test_demo1.py 我是用例:a . 我是用例:b . ============================================== 2 passed in 0.02 seconds ===============================================
三、显示详细信息:pytest -v xxx测试
G:\python_study\study\pytest_demo\study>pytest test_demo1.py -v ============================= test session starts ============================= platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0 -- d:\path_python\python.exe cachedir: .pytest_cache rootdir: G:\python_study\study\pytest_demo\study, inifile: collected 2 items test_demo1.py::Test_api::test_a PASSED [ 50%] test_demo1.py::Test_api::test_b PASSED [100%] ========================== 2 passed in 0.02 seconds ===========================
四、简洁显示信息:pytest -q xxxspa
G:\python_study\study\pytest_demo\study>pytest test_demo1.py -q .. [100%] 2 passed in 0.02 seconds
五、运行指定用例:pytest -k case_name (case_name可类可函数,模糊匹配关键字),以下匹配 demo插件
G:\python_study\study\pytest_demo\study>pytest -k demo -v ============================= test session starts ============================= platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0 -- d:\path_python\python.exe cachedir: .pytest_cache rootdir: G:\python_study\study\pytest_demo\study, inifile: collected 11 items / 5 deselected test_demo.py::test_ab PASSED [ 16%] test_demo.py::test_aba PASSED [ 33%] test_demo.py::Test_api::test_aa PASSED [ 50%] test_demo.py::Test_api::test_b PASSED [ 66%] test_demo1.py::Test_api::test_a PASSED [ 83%] test_demo1.py::Test_api::test_b PASSED [100%] =================== 6 passed, 5 deselected in 0.06 seconds ====================
六、命令行参数不分顺序,还有其余命令行参数,不一一细说:
七、pycharm 设置 pytest 运行用例:
更多请查看 pytest -h 或者找度娘,咱们通常用以上的参数够平常使用了。欢迎来QQ交流群:482713805