pytest配置项pytest.ini用法,以及常用控制台运行参数使用方法

  1. 创建一个文件,命名为 pytest.ini,这个是固定写法不能更改

  2. 用法一:使用ini配置运行的用例的路径

    [pytest]
    # 配置运行的用例的路径
    testpaths = ./testcases 
    

    执行用例时,会默认使用配置里的用例路径
    执行

  3. 用法二:配置用例标签 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
    
  4. 用法三:配置指定运行参数

    # 配置指定运行参数
    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 ======================================== 
    
  5. 用法四:通过关键字执行用例 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 ======================================== 
    
  6. 用法五:对输出结果简化 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
    
    
  7. 用法六:输出详细的测试结果 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) ========================================= 
  1. 用法七:输出用例的调式信息 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 =============================================== 
  1. 用法七:两种输出结果结合 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 =============================================== 
    
  • 24
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值