pytest命令行参数

1.-v:可以输出用例更加详细的执行信息,如下图

C:\Users\cale\checkapi\test_cc>pytest test_m1.py -v
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:\users\ipharmacare\python37\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.3', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '5.2.1', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'allure-pytest': '2.8.13', 'html': '2.0.0', 'metadata': '1.8.0', 'rerunfailures': '7.0'}}
rootdir: C:\Users\cale\checkapi\test_cc
plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items

test_m1.py::TestClass::test_one PASSED                                                                                                                                                                                                [ 50%]
test_m1.py::TestClass::test_two PASSED                                                                                                                                                                                                [100%]

============================================================================================================ 2 passed in 0.14s =============================================================================================================

C:\Users\cale\checkapi\test_cc>

2.-s:输出用例中的调试信息,比如print等打印信息

C:\Users\cale\checkapi\test_cc>pytest test_m1.py -s
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\cale\checkapi\test_cc
plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items

test_m1.py test_one方法执行
.test_two方法执行
.

============================================================================================================ 2 passed in 0.16s =============================================================================================================

3.-m:根据标记执行特定的测试用例

C:\Users\cale\checkapi\test_cc>pytest test_m1.py -m smoke -s
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\cale\checkapi\test_cc
plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items / 1 deselected / 1 selected

test_m1.py test_one方法执行
.

4.-k:执行用例中包含“关键字”的用例

C:\Users\cale\checkapi\test_cc>pytest test_m1.py -k test_one -s        #test_one是用例名称,还可以是标签名或其它
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\cale\checkapi\test_cc
plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items / 1 deselected / 1 selected

test_m1.py test_one方法执行
.

===================================================================================================== 1 passed, 1 deselected in 0.10s ======================================================================================================

5.-q:简化控制台的输出

C:\Users\cale\checkapi\test_cc>pytest test_m1.py -q
..                                                                                                                                                                                                                                    [100%]
2 passed in 0.08s

 6. --collect-only:列出当前目录下所有的测试模块,测试类及测试函数

C:\Users\cale\checkapi>pytest test_cc --collect-only
========================================================================= test session starts =========================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\cale\checkapi
plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 4 items
<Package C:\Users\cale\checkapi\test_cc>
  <Module test_m1.py>
    <Class TestClass>
        <Function test_one>
        <Function test_two>
  <Module test_m2.py>
    <Function test_three>
    <Function test_four>

======================================================================== no tests ran in 0.24s ========================================================================

7.--tb=style(style可以是:no,line,short) 屏蔽测试用例执行输出的回溯信息,可以简化用例失败时的输出信息

C:\Users\cale\checkapi\test_cc>pytest test_m1.py --tb=line
========================================================================= test session starts =========================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\cale\checkapi\test_cc
plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items

test_m1.py F.                                                                                                                                                    [100%]

============================================================================== FAILURES ===============================================================================
C:\Users\cale\checkapi\test_cc\test_m1.py:80: assert 1 == 2
===================================================================== 1 failed, 1 passed in 0.38s =====================================================================

C:\Users\cale\checkapi\test_cc>pytest test_m1.py --tb=no
========================================================================= test session starts =========================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\cale\checkapi\test_cc
plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items

test_m1.py F.                                                                                                                                                    [100%]

===================================================================== 1 failed, 1 passed in 0.34s =====================================================================

C:\Users\cale\checkapi\test_cc>pytest test_m1.py --tb=short
========================================================================= test session starts =========================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\cale\checkapi\test_cc
plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items

test_m1.py F.                                                                                                                                                    [100%]

============================================================================== FAILURES ===============================================================================
_________________________________________________________________________ TestClass.test_one __________________________________________________________________________
test_m1.py:80: in test_one
    assert  1==2
E   assert 1 == 2
------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------
test_one方法执行
===================================================================== 1 failed, 1 passed in 0.33s =====================================================================

 8.运行指定的测试类:pytest test_cc/m1_test.py::TestClass -v

C:\Users\cale\checkapi>pytest test_cc/m1_test.py::TestClass -v
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:\users\ipharmacare\python37\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.3', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '5.2.1', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'allure-pytest': '2.8.13', 'html': '2.0.0', 'metadata': '1.8.0', 'rerunfailures': '7.0'}}
rootdir: C:\Users\cale\checkapi
plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items

test_cc/m1_test.py::TestClass::test1 FAILED                                                                                                                                                                                           [ 50%]
test_cc/m1_test.py::TestClass::test2 PASSED

9.运行指定的测试方法:pytest test_cc/test_m2.py::test_three -v

C:\Users\cale\checkapi>pytest test_cc/test_m2.py::test_three -v
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:\users\ipharmacare\python37\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.3', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '5.2.1', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'allure-pytest': '2.8.13', 'html': '2.0.0', 'metadata': '1.8.0', 'rerunfailures': '7.0'}}
rootdir: C:\Users\cale\checkapi
plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 1 item

test_cc/test_m2.py::test_three PASSED                                                                                                                                                                                                 [100%]

 

posted @ 2020-04-14 10:58  pple  阅读(292)  评论(0编辑  收藏  举报
以终为始,你期待的那天不会太遥远。