【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
默认情况下,pytest会递归查找当前目录下所有以
test
开始或结尾的Python
脚本,并执行文件内的所有以test
开始或结束的函数和方法.
etc
├── robocop.toml
├── test_data.py
└── test_demo.py
一、执行某文件夹下所有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 =================================================
二、执行某个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 =================================================
三、测试某个文件中的某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 =================================================
来源:oschina
链接:https://my.oschina.net/u/2474096/blog/3111763