0三、pytest查找測試策略

默認狀況下,pytest會遞歸查找當前目錄下全部以test開始或結尾的Python腳本,並執行文件內的全部以test開始或結束的函數和方法.html


etc
├── robocop.toml
├── test_data.py
└── test_demo.py

1、執行某文件夾下全部test打頭的py文件中test打頭的函數(測試用例)

╰ pytest -v etc
=================================================== test session starts ====================================================
platform darwin -- Python 3.7.4, pytest-4.4.0, py-1.8.0, pluggy-0.13.0 -- /Users/zhouwanghua/Code/Leyan/python/robocop/bin/python
cachedir: .pytest_cache
metadata: {'Python': '3.7.4', 'Platform': 'Darwin-18.6.0-x86_64-i386-64bit', 'Packages': {'pytest': '4.4.0', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'bdd': '3.1.0', 'html': '1.20.0', 'metadata': '1.8.0'}}
rootdir: /Users/zhouwanghua/Code/Leyan/robocop, inifile: pytest.ini
plugins: bdd-3.1.0, html-1.20.0, metadata-1.8.0
collected 4 items                                                                                                          

etc/test_data.py::test_data1 PASSED                                                                                  [ 25%]
etc/test_data.py::test_data2 PASSED                                                                                  [ 50%]
etc/test_demo.py::test_demo PASSED                                                                                   [ 75%]
etc/test_demo.py::test_demo2 PASSED    
================================================= 4 passed in 0.01 seconds =================================================

2、執行某個py文件下全部的測試用例:

╰ pytest -v etc/test_demo.py            
=================================================== test session starts ====================================================
platform darwin -- Python 3.7.4, pytest-4.4.0, py-1.8.0, pluggy-0.13.0 -- /Users/zhouwanghua/Code/Leyan/python/robocop/bin/python
cachedir: .pytest_cache
metadata: {'Python': '3.7.4', 'Platform': 'Darwin-18.6.0-x86_64-i386-64bit', 'Packages': {'pytest': '4.4.0', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'bdd': '3.1.0', 'html': '1.20.0', 'metadata': '1.8.0'}}
rootdir: /Users/zhouwanghua/Code/Leyan/robocop, inifile: pytest.ini
plugins: bdd-3.1.0, html-1.20.0, metadata-1.8.0
collected 2 items                                                                                                          

etc/test_demo.py::test_demo PASSED                                                                                   [ 50%]
etc/test_demo.py::test_demo2 PASSED   
================================================= 2 passed in 0.01 seconds =================================================

3、測試某個文件中的某1個測試用例

╰ pytest -v etc/test_demo.py::test_demo2
=================================================== test session starts ====================================================
platform darwin -- Python 3.7.4, pytest-4.4.0, py-1.8.0, pluggy-0.13.0 -- /Users/zhouwanghua/Code/Leyan/python/robocop/bin/python
cachedir: .pytest_cache
metadata: {'Python': '3.7.4', 'Platform': 'Darwin-18.6.0-x86_64-i386-64bit', 'Packages': {'pytest': '4.4.0', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'bdd': '3.1.0', 'html': '1.20.0', 'metadata': '1.8.0'}}
rootdir: /Users/zhouwanghua/Code/Leyan/robocop, inifile: pytest.ini
plugins: bdd-3.1.0, html-1.20.0, metadata-1.8.0
collected 1 item                                                                                                           

etc/test_demo.py::test_demo2 PASSED                                                                                  [100%]

================================================= 1 passed in 0.01 seconds =================================================
相關文章
相關標籤/搜索