创建一个文件,命名为
pytest.ini
,这个是固定写法不能更改用法一:使用ini配置运行的用例的路径
[pytest] # 配置运行的用例的路径 testpaths = ./testcases
执行用例时,会默认使用配置里的用例路径
用法二:配置用例标签
pytest -m (tag)
[pytest] markers= # 用例标签 p1:important test:test env
# 用例加上标记 @pytest.mark.test def test_three(): assert 1 ==1 .... assert 2 > 1
# 指定执行的用例 (venv) E:\UI-AutoTest>pytest -m test
用法三:配置指定运行参数
# 配置指定运行参数 addopts: -m p1
# 用例加上标记 @pytest.mark.p1 def test_three(): assert 1 ==1 .... assert 2 > 1
# 默认执行指定的运行参数 (venv) E:\UI-AutoTest>pytest ============================================= test session starts ============================================== platform win32 -- Python 3.11.8, pytest-8.2.0, pluggy-1.5.0 rootdir: E:\UI-AutoTest configfile: pytest.ini testpaths: ./testcases plugins: assume-2.4.3 collected 3 items / 1 deselected / 2 selected testcases\test_baidu.py DevTools listening on ws://127.0.0.1:51612/devtools/browser/98a8da33-08fa-4a69-b3ef-6e7b8250335f . [ 50%] testcases\test_three.py . [100%] ======================================= 2 passed, 1 deselected in 4.42s ========================================
用法四:通过关键字执行用例
pytest -k '关键词'
def test_orderlist(): assert 1 == 1 def test_search(): assert 1 == 1
(venv) E:\UI-AutoTest>pytest -k "order" ============================================= test session starts ============================================== platform win32 -- Python 3.11.8, pytest-8.2.0, pluggy-1.5.0 rootdir: E:\UI-AutoTest configfile: pytest.ini testpaths: ./testcases plugins: assume-2.4.3 collected 2 items / 1 deselected / 1 selected testcases\test_one.py . [100%] ======================================= 1 passed, 1 deselected in 0.09s ========================================
用法五:对输出结果简化
pytest -q
(venv) E:\UI-AutoTest>pytest -q DevTools listening on ws://127.0.0.1:53028/devtools/browser/06bdbfb7-2306-49a1-8a0b-4ec9c8795c61 ..... [100%] 5 passed in 5.92s
用法六:输出详细的测试结果
pytest -v
(venv) E:\UI-AutoTest>pytest -v
============================================= test session starts ==============================================
platform win32 -- Python 3.11.8, pytest-8.2.0, pluggy-1.5.0 -- E:\UI-AutoTest\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: E:\UI-AutoTest
configfile: pytest.ini
testpaths: ./testcases
plugins: assume-2.4.3
collected 5 items
testcases/test_baidu.py::test_baidu
DevTools listening on ws://127.0.0.1:53227/devtools/browser/a6ecd80e-64d2-4009-b995-228db170ad89
[5060:3708:0430/161721.799:ERROR:ssl_client_socket_impl.cc(879)] handshake failed; returned -1, SSL error code 1,
net_error -100
[5060:3708:0430/161721.801:ERROR:ssl_client_socket_impl.cc(879)] handshake failed; returned -1, SSL error code 1,
net_error -100
PASSED [ 20%]
testcases/test_one.py::test_orderlist PASSED [ 40%]
testcases/test_one.py::test_search PASSED [ 60%]
testcases/test_three.py::test_three PASSED [ 80%]
testcases/test_three.py::test_assume PASSED [100%]
========================================= 5 passed in 65.84s (0:01:05) =========================================
- 用法七:输出用例的调式信息
pytest -s
(venv) E:\UI-AutoTest>pytest -s
============================================= test session starts ==============================================
platform win32 -- Python 3.11.8, pytest-8.2.0, pluggy-1.5.0
rootdir: E:\UI-AutoTest
configfile: pytest.ini
testpaths: ./testcases
plugins: assume-2.4.3
collected 5 items
testcases\test_baidu.py
DevTools listening on ws://127.0.0.1:53389/devtools/browser/664c2674-afb4-43ad-9dbc-dbb84827e49a
.
testcases\test_one.py 我是测试订单列表
.我是测试搜索
.
testcases\test_three.py ..
============================================== 5 passed in 5.18s ===============================================
- 用法七:两种输出结果结合
pytest -vs
(venv) E:\UI-AutoTest>pytest -vs ============================================= test session starts ============================================== platform win32 -- Python 3.11.8, pytest-8.2.0, pluggy-1.5.0 -- E:\UI-AutoTest\venv\Scripts\python.exe cachedir: .pytest_cache rootdir: E:\UI-AutoTest configfile: pytest.ini testpaths: ./testcases plugins: assume-2.4.3 collected 5 items testcases/test_baidu.py::test_baidu DevTools listening on ws://127.0.0.1:53647/devtools/browser/b1eab448-e3e8-46c4-923a-800c65fae15d PASSED testcases/test_one.py::test_orderlist 我是测试订单列表 PASSED testcases/test_one.py::test_search 我是测试搜索 PASSED testcases/test_three.py::test_three PASSED testcases/test_three.py::test_assume PASSED ============================================== 5 passed in 4.92s ===============================================