• pytest命令行方式运行用例


    背景

    用命令行方式调用用例是我们最常用的方式,这方面确实比java的TestNG框架要好用许多,至少不用写xml文件,为了提供定制化运行用例的方式,pytest提供了许多运行命令以供定制化运行某一类测试用例或者某个测试用例等

    pycharm里命令行运行用例

    在pycharm里写好了测试用例后如何运行呢?pycharm里好像并没有像eclipse里提供TestNG用的插件一样可以一键执行的方式,那么我们可以使用命令行的方式来进行,如下图所示为一个用例文件:

     代码如下:

     1 #-*- coding: utf-8 -*-
     2 import pytest
     3 
     4 
     5 class Test_simple():
     6 
     7     @pytest.mark.test
     8     def test_case1(self):
     9         print("testCase1")
    10         tof = True
    11         assert tof
    12 
    13     @pytest.mark.normal
    14     @pytest.mark.test
    15     def test_case2(self):
    16         print("testCase2")
    17         tof = False
    18         assert tof
    19 
    20     def test_case3(self):
    21         print("testCase3")
    22         assert True
    23 
    24     @pytest.mark.test
    25     def setup_class(self):
    26         print("用于test组")
    27 
    28     @pytest.mark.normal
    29     def setup_class(self):
    30         print("用于normal组")

    如上所示tests目录下包含了多个模块,在res_platform目录下添加了一些测试用例即test_Simple和test_getRecity
    想要运行用例时可以打开下方的Terminal窗口:

     会自动切换到当前工程目录下,而后即可使用pytest的命令了

    终端中使用pytest

    在终端中使用pytest也是和在pycharm中类似,如下以windows系统为例:
    先切换到用例所在工程或者目录而后运行pytest即可,如下:

     linux系统中也是同样的使用方法,只是如果没有为pytest添加软连接,则需要在pytest前面加上python命令;

    用例全部运行

    全部运行时不需要添加任何后缀,只需要添加命令pytest即可,此时打印的信息比较简单:

    打印详情-v

    如上图所示,只显示了用例时成功还是失败,至于里边的log则没有打印,那么如果我们想要看运行详细信息怎么办呢?可以加上-v标签,如下:

     1 (venv) D:devgemini	ests
    es_platform>pytest -v
     2 ======================================================================================================= test session starts =======================================================================================================
     3 platform win32 -- Python 3.7.1, pytest-4.3.1, py-1.10.0, pluggy-0.13.1 -- d:devvenvscriptspython.exe
     4 cachedir: .pytest_cache
     5 rootdir: D:devgemini	ests
    es_platform, inifile:
     6 plugins: allure-pytest-2.8.40, json-0.4.0
     7 collected 6 items                                                                                                                                                                                                                  
     8 
     9 test_Simple.py::Test_simple::test_case1 PASSED                                                                                                                                                                               [ 16%]
    10 test_Simple.py::Test_simple::test_case2 FAILED                                                                                                                                                                               [ 33%]
    11 test_Simple.py::Test_simple::test_case3 PASSED                                                                                                                                                                               [ 50%]
    12 test_getRecCity.py::test_reccity[4-E2237250698--] PASSED                                                                                                                                                                     [ 66%]
    13 test_getRecCity.py::test_reccity[4--1623811147711.43stfy-] PASSED                                                                                                                                                            [ 83%]
    14 test_getRecCity.py::test_reccity[4---50772109410758277886] PASSED                                                                                                                                                            [100%]
    15 
    16 ============================================================================================================ FAILURES =============================================================================================================
    17 _____________________________________________________________________________________________________ Test_simple.test_case2 ______________________________________________________________________________________________________
    18 
    19 self = <tests.res_platform.test_Simple.Test_simple object at 0x000001EFB493C048>
    20 
    21     @pytest.mark.normal
    22     @pytest.mark.test
    23     def test_case2(self):
    24         print("testCase2")
    25         tof = False
    26 >       assert tof
    27 E       assert False
    28 
    29 test_Simple.py:18: AssertionError
    30 ------------------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------------------
    31 testCase2
    32 =============================================================================================== 1 failed, 5 passed in 0.39 seconds ================================================================================================

    如上图会把详细信息都打印出来

    指定组别

    如果用例中包含多个分组,想要只运行其中一个组,则使用-m "组名"的方式,依然使用如上代码,运行命令和结果如下:

     1 (venv) D:devgemini	ests
    es_platform>pytest -s -m "normal"
     2 ======================================================================================================= test session starts =======================================================================================================
     3 platform win32 -- Python 3.7.1, pytest-4.3.1, py-1.10.0, pluggy-0.13.1
     4 rootdir: D:devgemini	ests
    es_platform, inifile:
     5 plugins: allure-pytest-2.8.40, json-0.4.0
     6 collected 6 items / 5 deselected / 1 selected                                                                                                                                                                                      
     7 
     8 test_Simple.py 用于normal组
     9 testCase2
    10 F
    11 
    12 ============================================================================================================ FAILURES =============================================================================================================
    13 _____________________________________________________________________________________________________ Test_simple.test_case2 ______________________________________________________________________________________________________
    14 
    15 self = <tests.res_platform.test_Simple.Test_simple object at 0x000001B57B05E908>
    16 
    17     @pytest.mark.normal
    18     @pytest.mark.test
    19     def test_case2(self):
    20         print("testCase2")
    21         tof = False
    22 >       assert tof
    23 E       assert False
    24 
    25 test_Simple.py:18: AssertionError
    26 ============================================================================================= 1 failed, 5 deselected in 0.16 seconds ==============================================================================================

    使用表达式指定某些用例-k

    -k选项允许我们设置表达式来运行某些用例,如下传参就只运行了test_case1test_case2

    (venv) D:devgemini	ests
    es_platform>pytest -v -k "case1 or case2"
    ======================================================================================================= test session starts =======================================================================================================
    platform win32 -- Python 3.7.1, pytest-4.3.1, py-1.10.0, pluggy-0.13.1 -- d:devvenvscriptspython.exe
    cachedir: .pytest_cache
    rootdir: D:devgemini	ests
    es_platform, inifile:
    plugins: allure-pytest-2.8.40, json-0.4.0
    collected 6 items / 4 deselected / 2 selected                                                                                                                                                                                      
    
    test_Simple.py::Test_simple::test_case1 PASSED                                                                                                                                                                               [ 50%]
    test_Simple.py::Test_simple::test_case2 FAILED                                                                                                                                                                               [100%]
    
    ============================================================================================================ FAILURES =============================================================================================================
    _____________________________________________________________________________________________________ Test_simple.test_case2 ______________________________________________________________________________________________________
    
    self = <tests.res_platform.test_Simple.Test_simple object at 0x0000026C2ECCE0B8>
    
        @pytest.mark.normal
        @pytest.mark.test
        def test_case2(self):
            print("testCase2")
            tof = False
    >       assert tof
    E       assert False
    
    test_Simple.py:18: AssertionError
    ------------------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------------------
    testCase2
    ======================================================================================== 1 failed, 1 passed, 4 deselected in 0.21 seconds =========================================================================================

    表达式的写法有许多,可以用全称如test_case1这样也可以去掉test_,除了or外也可以使用not来指定那些用例不跑;

    遇到失败即停止运行-x

    pytest的原本运行规则是每条用例均执行,不管是否有失败,如果我们想在用例运行时遇到失败即停止,则可以使用-x,如下所示,第二条用例失败后则不再运行第三条用例:

    (venv) D:devgemini	ests
    es_platform>pytest -v -x
    ======================================================================================================= test session starts =======================================================================================================
    platform win32 -- Python 3.7.1, pytest-4.3.1, py-1.10.0, pluggy-0.13.1 -- d:devvenvscriptspython.exe
    cachedir: .pytest_cache
    rootdir: D:devgemini	ests
    es_platform, inifile:
    plugins: allure-pytest-2.8.40, json-0.4.0
    collected 6 items                                                                                                                                                                                                                  
    
    test_Simple.py::Test_simple::test_case1 PASSED                                                                                                                                                                               [ 16%]
    test_Simple.py::Test_simple::test_case2 FAILED                                                                                                                                                                               [ 33%]
    
    ============================================================================================================ FAILURES =============================================================================================================
    _____________________________________________________________________________________________________ Test_simple.test_case2 ______________________________________________________________________________________________________
    
    self = <tests.res_platform.test_Simple.Test_simple object at 0x000001BAD12ECCC0>
    
        @pytest.mark.normal
        @pytest.mark.test
        def test_case2(self):
            print("testCase2")
            tof = False
    >       assert tof
    E       assert False
    
    test_Simple.py:18: AssertionError
    ------------------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------------------
    testCase2
    =============================================================================================== 1 failed, 1 passed in 0.16 seconds ================================================================================================

    指定运行某个测试py文件

    指定运行某个py文件,只需要接上文件相对路径即可:

     1 (venv) D:devgemini	ests
    es_platform>pytest -v res_platform/test_getRecCity.py
     2 ======================================================================================================= test session starts =======================================================================================================
     3 platform win32 -- Python 3.7.1, pytest-4.3.1, py-1.10.0, pluggy-0.13.1 -- d:devvenvscriptspython.exe
     4 cachedir: .pytest_cache
     5 rootdir: D:devgemini	ests
    es_platform, inifile:
     6 plugins: allure-pytest-2.8.40, json-0.4.0
     7 collecting ... 
     8 ================================================================================================== no tests ran in 0.01 seconds ===================================================================================================
     9 ERROR: file not found: res_platform/test_getRecCity.py
    10 
    11 
    12 (venv) D:devgemini	ests
    es_platform>pytest -v test_Simple.py
    13 ======================================================================================================= test session starts =======================================================================================================
    14 platform win32 -- Python 3.7.1, pytest-4.3.1, py-1.10.0, pluggy-0.13.1 -- d:devvenvscriptspython.exe
    15 cachedir: .pytest_cache
    16 rootdir: D:devgemini	ests
    es_platform, inifile:
    17 plugins: allure-pytest-2.8.40, json-0.4.0
    18 collected 3 items                                                                                                                                                                                                                  
    19 
    20 test_Simple.py::Test_simple::test_case1 PASSED                                                                                                                                                                               [ 33%]
    21 test_Simple.py::Test_simple::test_case2 FAILED                                                                                                                                                                               [ 66%]
    22 test_Simple.py::Test_simple::test_case3 PASSED                                                                                                                                                                               [100%]
    23 
    24 ============================================================================================================ FAILURES =============================================================================================================

    指定运行某个class

    写法为:py文件路径::class名称,范例如下:

     1 (venv) D:devgemini	ests
    es_platform>pytest -v test_Simple.py
     2 ======================================================================================================= test session starts =======================================================================================================
     3 platform win32 -- Python 3.7.1, pytest-4.3.1, py-1.10.0, pluggy-0.13.1 -- d:devvenvscriptspython.exe
     4 cachedir: .pytest_cache
     5 rootdir: D:devgemini	ests
    es_platform, inifile:
     6 plugins: allure-pytest-2.8.40, json-0.4.0
     7 collected 3 items                                                                                                                                                                                                                  
     8 
     9 test_Simple.py::Test_simple::test_case1 PASSED                                                                                                                                                                               [ 33%]
    10 test_Simple.py::Test_simple::test_case2 FAILED                                                                                                                                                                               [ 66%]
    11 test_Simple.py::Test_simple::test_case3 PASSED                                                                                                                                                                               [100%]
    12 
    13 ============================================================================================================ FAILURES =============================================================================================================
    14 _____________________________________________________________________________________________________ Test_simple.test_case2 ______________________________________________________________________________________________________
    15 
    16 self = <tests.res_platform.test_Simple.Test_simple object at 0x0000024213191E80>
    17 
    18     @pytest.mark.normal
    19     @pytest.mark.test
    20     def test_case2(self):
    21         print("testCase2")
    22         tof = False
    23 >       assert tof
    24 E       assert False
    25 
    26 test_Simple.py:18: AssertionError
    27 ------------------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------------------

    指定运行某个方法:

    写法为:py文件路径::class名称::method名称,范例如下:

     1 (venv) D:devgemini	ests>pytest -v res_platform/test_Simple.py::Test_simple
     2 ======================================================================================================= test session starts =======================================================================================================
     3 platform win32 -- Python 3.7.1, pytest-4.3.1, py-1.10.0, pluggy-0.13.1 -- d:devvenvscriptspython.exe
     4 cachedir: .pytest_cache
     5 rootdir: D:devgemini	ests, inifile:
     6 plugins: allure-pytest-2.8.40, json-0.4.0
     7 collected 3 items                                                                                                                                                                                                                  
     8 
     9 res_platform/test_Simple.py::Test_simple::test_case1 PASSED                                                                                                                                                                  [ 33%]
    10 res_platform/test_Simple.py::Test_simple::test_case2 FAILED                                                                                                                                                                  [ 66%]
    11 res_platform/test_Simple.py::Test_simple::test_case3 PASSED                                                                                                                                                                  [100%]
    12 
    13 ============================================================================================================ FAILURES =============================================================================================================
    14 _____________________________________________________________________________________________________ Test_simple.test_case2 ______________________________________________________________________________________________________
    15 
    16 self = <tests.res_platform.test_Simple.Test_simple object at 0x0000014460FC2EB8>
    17 
    18     @pytest.mark.normal
    19     @pytest.mark.test
    20     def test_case2(self):
    21         print("testCase2")
    22         tof = False
    23 >       assert tof
    24 E       assert False
    25 
    26 res_platform	est_Simple.py:18: AssertionError
    27 ------------------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------------------
    28 testCase2
    29 =============================================================================================== 1 failed, 2 passed in 0.11 seconds ================================================================================================

    其他

    pytest还包含许多其他用法,具体用法可以使用pytest --help来查看,如下:

      

  • 相关阅读:
    python中读取文件数据时要注意文件路径
    sklearn.model_selection 的 train_test_split作用
    matplotlib中subplot的各参数的作用
    用梯度下降算法求最值
    AfxMessageBox与MessageBox用法与区别
    MFC、API、C++三者的区别
    2、CString与string借助char *互转
    1、创建MFC应用程序——单个文档
    1、Mat类的属性、方法
    CMake编译OpenCV
  • 原文地址:https://www.cnblogs.com/sunnydev/p/15472749.html
Copyright © 2020-2023  润新知