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 =============================================== 
    

相关推荐

  1. postgresql_conf中配置

    2024-05-01 17:46:01       45 阅读
  2. 4、Kafka 核心配置

    2024-05-01 17:46:01       48 阅读
  3. redis.conf配置文件配置详解

    2024-05-01 17:46:01       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-01 17:46:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-01 17:46:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-01 17:46:01       87 阅读
  4. Python语言-面向对象

    2024-05-01 17:46:01       96 阅读

热门阅读

  1. k8s pod 镜像拉取策略

    2024-05-01 17:46:01       27 阅读
  2. Python解释器:编程界的“翻译官”

    2024-05-01 17:46:01       30 阅读
  3. ps基础学习笔记-颜色模式

    2024-05-01 17:46:01       29 阅读
  4. 排序试题(一)

    2024-05-01 17:46:01       34 阅读
  5. “IP地址的未来:IPv6的机遇与挑战“

    2024-05-01 17:46:01       34 阅读
  6. CSS背景图像显示方式

    2024-05-01 17:46:01       35 阅读
  7. Element-UI快速入门

    2024-05-01 17:46:01       37 阅读
  8. 以AI技术与街景影像重塑城市研究

    2024-05-01 17:46:01       31 阅读