一、Pytest 框架入门
pytest 目录名/
pytest 脚本名称.py
pytest -k "MyClass and not method"
pytest test_mod.py::test_func
pytest test_mod.py::TestClass::test_method
pytest -m slow
-x 遇到错误时停止测试
pytest -x test_class.py
--maxfail=num
pytest --maxfail=3
Pytest.main()
Pytest.main("-s", "mytest.py")
(venv) D:\PyLib\CodemaoApiTest>pytest --collect-only ============================================================================================= test session starts ============================================================================================== platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 rootdir: D:\PyLib\CodemaoApiTest plugins: allure-pytest-2.7.1, forked-1.0.2, html-1.21.1, metadata-1.8.0, xdist-1.29.0 collected 12 items <Package D:\PyLib\CodemaoApiTest\tests> <Module test_ban_and_recover_login.py> <Function test_ban_login_one> <Function test_ban_and_recover_login_two> <Module test_ban_and_recover_publish.py> <Function test_ban_one> <Function test_ban_two> <Module test_ban_comment.py> <Function test_ban_one> <Function test_ban_two> <Function test_ban_three> <Function test_ban_four> <Module test_delete_comments.py> <Function test_delete_comments_one> <Module test_delete_works.py> <Function test_delete_works_one> <Module test_recover_comment.py> <Function test_recover_one> <Function test_recover_two> ========================================================================================= no tests ran in 0.27 seconds ========================================================================================
(venv) D:\PyLib\CodemaoApiTest>pytest -k "comment or publish" --collect-only ============================================================================================= test session starts ============================================================================================== platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 rootdir: D:\PyLib\CodemaoApiTest plugins: allure-pytest-2.7.1, forked-1.0.2, html-1.21.1, metadata-1.8.0, xdist-1.29.0 collected 12 items / 3 deselected / 9 selected <Package D:\PyLib\CodemaoApiTest\tests> <Module test_ban_and_recover_publish.py> <Function test_ban_one> <Function test_ban_two> <Module test_ban_comment.py> <Function test_ban_one> <Function test_ban_two> <Function test_ban_three> <Function test_ban_four> <Module test_delete_comments.py> <Function test_delete_comments_one> <Module test_recover_comment.py> <Function test_recover_one> <Function test_recover_two> ========================================================================================= 3 deselected in 0.12 seconds =========================================================================================
(venv) D:\PyLib\CodemaoApiTest>pytest -k "one and not ban" --collect-only ============================================================================================= test session starts ============================================================================================== platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 rootdir: D:\PyLib\CodemaoApiTest plugins: allure-pytest-2.7.1, forked-1.0.2, html-1.21.1, metadata-1.8.0, xdist-1.29.0 collected 12 items / 9 deselected / 3 selected <Package D:\PyLib\CodemaoApiTest\tests> <Module test_delete_comments.py> <Function test_delete_comments_one> <Module test_delete_works.py> <Function test_delete_works_one> <Module test_recover_comment.py> <Function test_recover_one> ========================================================================================= 9 deselected in 0.12 seconds =========================================================================================
(venv) D:\PyLib\CodemaoApiTest>pytest -m smoke ============================================================================================= test session starts ============================================================================================== platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 rootdir: D:\PyLib\CodemaoApiTest, inifile: pytest.ini plugins: allure-pytest-2.7.1, forked-1.0.2, html-1.21.1, metadata-1.8.0, xdist-1.29.0 collected 12 items / 11 deselected / 1 selected tests\test_recover_comment.py . [100%] =================================================================================== 1 passed, 11 deselected in 13.47 seconds ==================================================================================
(venv) D:\PyLib\NemoDataFactory\tests>pytest -v test_add.py ============================================================================================= test session starts ============================================================================================== platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 -- d:\pylib\nemodatafactory\venv\scripts\python.exe cachedir: .pytest_cache metadata: {'Python': '3.6.5rc1', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '5.0.1', 'py': '1.8.0', 'pluggy': '0.12.0'}, 'Plugins': {'allure-pytest': '2.7.1', 'html': '1.21.1', 'metadata': '1.8.0'}, 'JAVA_HOME': 'D:\\software\\Java'} rootdir: D:\PyLib\NemoDataFactory\tests plugins: allure-pytest-2.7.1, html-1.21.1, metadata-1.8.0 collected 24 items test_add.py::test_add_bcm[{'name': 测试所有声音播放}] PASSED [ 4%] test_add.py::test_add_bcm[{'name': 重构后得声音模块}] PASSED [ 8%] test_add.py::test_add_bcm[{'name': 测试说你好}] PASSED [ 12%] test_add.py::test_add_bcm[{'name': 测试侦测颜色}] PASSED [ 16%] test_add.py::test_add_bcm[{'name': 测试侦测屏幕边缘}] PASSED [ 20%] test_add.py::test_add_bcm[{'name': 测试计时器}] PASSED # [ 25%] test_add.py::test_add_bcm[{'name': 测试手指}] PASSED [ 29%] test_add.py::test_add_bcm[{'name': 测试画笔}] PASSED [ 33%] test_add.py::test_add_bcm[{'name': 测试变量运算}] PASSED [ 37%] test_add.py::test_add_bcm[{'name': 音乐播放器嘟嘟}] PASSED [ 41%] test_add.py::test_add_bcm[{'name': 追击飞电鼠}] PASSED [ 45%] test_add.py::test_add_bcm[{'name': 5000积木数}] PASSED [ 50%] test_add.py::test_add_bcm[{'name': 测试克隆类积木}] PASSED [ 54%] test_add.py::test_add_bcm[{'name': 测试控制类积木}] PASSED [ 58%] test_add.py::test_add_bcm[{'name': 测试动作类积木}] PASSED [ 62%] test_add.py::test_add_bcm[{'name': 编程猫快跑}] PASSED [ 66%] test_add.py::test_add_bcm[{'name': 测试侦测类}] PASSED [ 70%] test_add.py::test_add_bcm[{'name': 测试水平和重力}] PASSED [ 75%] test_add.py::test_add_bcm[{'name': 外观积木汇总}] PASSED [ 79%] test_add.py::test_add_bcm[{'name': 测试当角色被点击}] PASSED [ 83%] test_add.py::test_add_bcm[{'name': 测试外观类}] PASSED [ 87%] test_add.py::test_add_bcm[{'name': 测试x分量}] PASSED [ 91%] test_add.py::test_add_bcm[{'name': 测试当积木}] PASSED [ 95%] test_add.py::test_add_bcm[{'name': (创作页内运行)测试复制积木能否克隆}] PASSED [100%] ========================================================================================== 24 passed in 32.01 seconds =========================================================================================
(venv) D:\PyLib\NemoDataFactory\tests>pytest -q test_add.py ........................ [100%] 24 passed in 31.47 seconds
(venv) D:\PyLib\NemoDataFactory\tests>pytest -l test_add.py ============================================================================================= test session starts ============================================================================================== platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 rootdir: D:\PyLib\NemoDataFactory\tests plugins: allure-pytest-2.7.1, html-1.21.1, metadata-1.8.0 collected 24 items test_add.py ........................ [100%] ========================================================================================== 24 passed in 30.74 seconds =========================================================================================
(venv) D:\PyLib\CodemaoApiTest>pytest tests --durations=0 ============================================================================================= test session starts ============================================================================================== platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 rootdir: D:\PyLib\CodemaoApiTest, inifile: pytest.ini plugins: allure-pytest-2.7.1, forked-1.0.2, html-1.21.1, metadata-1.8.0, xdist-1.29.0 collected 12 items tests\test_ban_and_recover_login.py .. [ 16%] tests\test_ban_and_recover_publish.py .. [ 33%] tests\test_ban_comment.py .... [ 66%] tests\test_delete_comments.py . [ 75%] tests\test_delete_works.py . [ 83%] tests\test_recover_comment.py .. [100%] ============================================================================================ slowest test durations ============================================================================================ 37.84s call tests/test_delete_comments.py::test_delete_comments_one 15.91s call tests/test_delete_works.py::test_delete_works_one 12.94s call tests/test_recover_comment.py::test_recover_one 9.03s call tests/test_ban_and_recover_login.py::test_ban_and_recover_login_two 8.56s call tests/test_recover_comment.py::test_recover_two 6.10s call tests/test_ban_comment.py::test_ban_two 6.05s call tests/test_ban_comment.py::test_ban_four 5.21s teardown tests/test_ban_and_recover_login.py::test_ban_and_recover_login_two 3.92s call tests/test_ban_and_recover_publish.py::test_ban_two 3.75s setup tests/test_ban_and_recover_login.py::test_ban_and_recover_login_two 3.32s setup tests/test_ban_and_recover_login.py::test_ban_login_one 2.74s setup tests/test_ban_comment.py::test_ban_two 2.71s setup tests/test_recover_comment.py::test_recover_one 2.62s setup tests/test_delete_works.py::test_delete_works_one 2.50s setup tests/test_ban_comment.py::test_ban_four 2.46s teardown tests/test_recover_comment.py::test_recover_one 2.45s setup tests/test_ban_comment.py::test_ban_three 2.44s setup tests/test_delete_comments.py::test_delete_comments_one 2.42s setup tests/test_ban_comment.py::test_ban_one 2.41s teardown tests/test_ban_comment.py::test_ban_one 2.37s setup tests/test_recover_comment.py::test_recover_two 2.31s teardown tests/test_ban_and_recover_publish.py::test_ban_two 2.30s teardown tests/test_delete_comments.py::test_delete_comments_one 2.29s teardown tests/test_ban_comment.py::test_ban_two 2.28s teardown tests/test_delete_works.py::test_delete_works_one 2.25s teardown tests/test_ban_and_recover_login.py::test_ban_login_one 2.24s teardown tests/test_recover_comment.py::test_recover_two 2.22s teardown tests/test_ban_comment.py::test_ban_three 2.20s teardown tests/test_ban_comment.py::test_ban_four 2.03s teardown tests/test_ban_and_recover_publish.py::test_ban_one 1.54s call tests/test_ban_and_recover_login.py::test_ban_login_one 1.52s setup tests/test_ban_and_recover_publish.py::test_ban_two 1.47s setup tests/test_ban_and_recover_publish.py::test_ban_one 1.31s call tests/test_ban_and_recover_publish.py::test_ban_one 0.70s call tests/test_ban_comment.py::test_ban_one 0.66s call tests/test_ban_comment.py::test_ban_three ========================================================================================= 12 passed in 165.19 seconds =========================================================================================