最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python測試框架pytest高階用法全面詳解

 更新時(shí)間:2022年05月31日 16:32:02   作者:慕城南風(fēng)  
這篇文章主要為大家介紹了Python測試框架pytest高階用法全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

前言

Python測試框架之前一直用的是unittest+HTMLTestRunner,聽到有人說pytest很好用,所以這段時(shí)間就看了看pytest文檔,在這里做個(gè)記錄。

官方文檔介紹:

Pytest is a framework that makes building simple and scalable tests easy. Tests are expressive and readable—no boilerplate code required. Get started in minutes with a small unit test or complex functional test for your application or library.

pytest是一個(gè)非常成熟的全功能的Python測試框架,主要有以下幾個(gè)特點(diǎn):

  • 簡單靈活,容易上手
  • 支持參數(shù)化
  • 能夠支持簡單的單元測試和復(fù)雜的功能測試,還可以用來做selenium/appnium等自動(dòng)化測試、接口自
  • 動(dòng)化測試(pytest+requests)
  • pytest具有很多第三方插件,并且可以自定義擴(kuò)展,比較好用的如pytest-selenium(集成
  • selenium)、pytest-html(完美html測試報(bào)告生成)、pytest-rerunfailures(失敗case重復(fù)執(zhí)
  • 行)、pytest-xdist(多CPU分發(fā))等
  • 測試用例的skip和xfail處理
  • 可以很好的和jenkins集成
  • report框架----allure 也支持了pytest

1.pytest安裝

1.1安裝

pip install -U pytest

1.2驗(yàn)證安裝

pytest --version # 會(huì)展示當(dāng)前已安裝版本

1.3pytest文檔

官方文檔:https://docs.pytest.org/en/latest/contents.html

在pytest框架中,有如下約束:

  • 所有的單測文件名都需要滿足test_*.py格式或*_test.py格式。
  • 在單測文件中,測試類以Test開頭,并且不能帶有 init
     方法(注意:定義class時(shí),需要以T開頭,不然pytest是不會(huì)去運(yùn)行該class的)
  • 在單測類中,可以包含一個(gè)或多個(gè)test_開頭的函數(shù)。
  • 在執(zhí)行pytest命令時(shí),會(huì)自動(dòng)從當(dāng)前目錄及子目錄中尋找符合上述約束的測試函數(shù)來執(zhí)行。

1.4 Pytest運(yùn)行方式

 # file_name: test_abc.py
 import pytest # 引入pytest包
 def test_a(): # test開頭的測試函數(shù)
     print("------->test_a")
     assert 1 # 斷言成功
 def test_b():
     print("------->test_b")
     assert 0 # 斷言失敗
 if __name__ == '__main__':
        pytest.main("-s  test_abc.py") # 調(diào)用pytest的main函數(shù)執(zhí)行測試

1.測試類主函數(shù)模式

pytest.main("-s test_abc.py")

2.命令行模式

  pytest 文件路徑/測試文件名
  例如:pytest ./test_abc.py

1.5 Pytest Exit Code 含義清單

Exit code 0 所有用例執(zhí)行完畢,全部通過

Exit code 1 所有用例執(zhí)行完畢,存在Failed的測試用例

Exit code 2 用戶中斷了測試的執(zhí)行

Exit code 3 測試執(zhí)行過程發(fā)生了內(nèi)部錯(cuò)誤

Exit code 4 pytest 命令行使用錯(cuò)誤

Exit code 5 未采集到可用測試用例文件

1.6 如何獲取幫助信息

查看 pytest 版本

pytest --version

顯示可用的內(nèi)置函數(shù)參數(shù)

pytest --fixtures

通過命令行查看幫助信息及配置文件選項(xiàng)

pytest --help

1.7 控制測試用例執(zhí)行

1.在第N個(gè)用例失敗后,結(jié)束測試執(zhí)行

pytest -x                    # 第01次失敗,就停止測試
pytest --maxfail=2     # 出現(xiàn)2個(gè)失敗就終止測試

2.指定測試模塊

pytest test_mod.py

3.指定測試目錄

pytest testing/

4.通過關(guān)鍵字表達(dá)式過濾執(zhí)行

pytest -k "MyClass and not method"

這條命令會(huì)匹配文件名、類名、方法名匹配表達(dá)式的用例,這里這條命令會(huì)運(yùn)行 TestMyClass.test_something, 不會(huì)執(zhí)行 TestMyClass.test_method_simple

5.通過 node id 指定測試用例

nodeid由模塊文件名、分隔符、類名、方法名、參數(shù)構(gòu)成,舉例如下:
運(yùn)行模塊中的指定用例

pytest test_mod.py::test_func

運(yùn)行模塊中的指定方法

ytest test_mod.py::TestClass::test_method

6.通過標(biāo)記表達(dá)式執(zhí)行

pytest -m slow

這條命令會(huì)執(zhí)行被裝飾器 @pytest.mark.slow 裝飾的所有測試用例

7.通過包執(zhí)行測試

pytest --pyargs pkg.testing

這條命令會(huì)自動(dòng)導(dǎo)入包 pkg.testing,并使用該包所在的目錄,執(zhí)行下面的用例。

1.8 多進(jìn)程運(yùn)行cases

當(dāng)cases量很多時(shí),運(yùn)行時(shí)間也會(huì)變的很長,如果想縮短腳本運(yùn)行的時(shí)長,就可以用多進(jìn)程來運(yùn)行。

安裝pytest-xdist:

pip install -U pytest-xdist

運(yùn)行模式:

pytest test_se.py -n NUM

其中NUM填寫并發(fā)的進(jìn)程數(shù)。

1.9 重試運(yùn)行cases

在做接口測試時(shí),有事會(huì)遇到503或短時(shí)的網(wǎng)絡(luò)波動(dòng),導(dǎo)致case運(yùn)行失敗,而這并非是我們期望的結(jié)果,此時(shí)可以就可以通過重試運(yùn)行cases的方式來解決。

安裝pytest-rerunfailures:

pip install -U pytest-rerunfailures

運(yùn)行模式:

pytest test_se.py --reruns NUM

NUM填寫重試的次數(shù)。

1.10 顯示print內(nèi)容

在運(yùn)行測試腳本時(shí),為了調(diào)試或打印一些內(nèi)容,我們會(huì)在代碼中加一些print內(nèi)容,但是在運(yùn)行pytest時(shí),這些內(nèi)容不會(huì)顯示出來。如果帶上-s,就可以顯示了。

運(yùn)行模式:

pytest test_se.py -s

另外,pytest的多種運(yùn)行模式是可以疊加執(zhí)行的,比如說,你想同時(shí)運(yùn)行4個(gè)進(jìn)程,又想打印出print的內(nèi)容。可以用:

pytest test_se.py -s -n 4

2.Pytest的setup和teardown函數(shù)

1.setup和teardown主要分為:模塊級,類級,功能級,函數(shù)級。

2.存在于測試類內(nèi)部

代碼示例:

函數(shù)級別setup()/teardown()

運(yùn)行于測試方法的始末,即:運(yùn)行一次測試函數(shù)會(huì)運(yùn)行一次setup和teardown

import pytest
class Test_ABC:
  # 函數(shù)級開始
  def setup(self):
      print("------->setup_method")
  # 函數(shù)級結(jié)束
  def teardown(self):
      print("------->teardown_method")
  def test_a(self):
      print("------->test_a")
      assert 1
  def test_b(self):
      print("------->test_b")
if __name__ == '__main__':
              pytest.main("-s  test_abc.py")
執(zhí)行結(jié)果:
  test_abc.py 
  ------->setup_method # 第一次 setup()
  ------->test_a
  .
  ------->teardown_method # 第一次 teardown()
  ------->setup_method # 第二次 setup()
  ------->test_b
  .
          ------->teardown_method # 第二次 teardown()

類級別

運(yùn)行于測試類的始末,即:在一個(gè)測試內(nèi)只運(yùn)行一次setup_class和teardown_class,不關(guān)心測試類內(nèi)有多少個(gè)測試函數(shù)。

代碼示例:

import pytest
class Test_ABC:
   # 測試類級開始
   def setup_class(self):
       print("------->setup_class")
   # 測試類級結(jié)束
   def teardown_class(self):
       print("------->teardown_class")
   def test_a(self):
       print("------->test_a")
       assert 1
   def test_b(self):
       print("------->test_b")
          if __name__ == '__main__':
              pytest.main("-s  test_abc.py")
執(zhí)行結(jié)果:
  test_abc.py 
  ------->setup_class # 第一次 setup_class()
  ------->test_a
  .
  ------->test_b
  F 
          ------->teardown_class # 第一次 teardown_class()

3.Pytest配置文件

pytest的配置文件通常放在測試目錄下,名稱為pytest.ini,命令行運(yùn)行時(shí)會(huì)使用該配置文件中的配置.

#配置pytest命令行運(yùn)行參數(shù)
   [pytest]
    addopts = -s ... # 空格分隔,可添加多個(gè)命令行參數(shù) -所有參數(shù)均為插件包的參數(shù)配置測試搜索的路徑
    testpaths = ./scripts  # 當(dāng)前目錄下的scripts文件夾 -可自定義
#配置測試搜索的文件名稱
    python_files = test*.py 
#當(dāng)前目錄下的scripts文件夾下,以test開頭,以.py結(jié)尾的所有文件 -可自定義
配置測試搜索的測試類名
    python_classes = Test_*  
   #當(dāng)前目錄下的scripts文件夾下,以test開頭,以.py結(jié)尾的所有文件中,以Test開頭的類 -可自定義
配置測試搜索的測試函數(shù)名
    python_functions = test_*
#當(dāng)前目錄下的scripts文件夾下,以test開頭,以.py結(jié)尾的所有文件中,以Test開頭的類內(nèi),以test_開頭的方法 -可自定義
 

4 Pytest常用插件

插件列表網(wǎng)址:https://plugincompat.herokuapp.com

包含很多插件包,大家可依據(jù)工作的需求選擇使用。

4.1 前置條件:

1.文件路徑:

- Test_App
- - test_abc.py
- - pytest.ini

2.pyetst.ini配置文件內(nèi)容:

  [pytest]
# 命令行參數(shù)
 addopts = -s
# 搜索文件名
 python_files = test_*.py
 # 搜索的類名
 python_classes = Test_*
 #搜索的函數(shù)名
    python_functions = test_*

4.2 Pytest測試報(bào)告

pytest-HTML是一個(gè)插件,pytest用于生成測試結(jié)果的HTML報(bào)告。兼容Python 2.7,3.6

安裝方式:pip install pytest-html

pip install pytest-html

通過命令行方式,生成xml/html格式的測試報(bào)告,存儲(chǔ)于用戶指定路徑。插件名稱:pytest-html

使用方法: 命令行格式:pytest --html=用戶路徑/report.html

示例:

import pytest
class Test_ABC:
    def setup_class(self):
        print("------->setup_class")
    def teardown_class(self):
        print("------->teardown_class")
    def test_a(self):
        print("------->test_a")
        assert 1
    def test_b(self):
            print("------->test_b")
            assert 0 # 斷言失敗```
運(yùn)行方式:
1.修改Test_App/pytest.ini文件,添加報(bào)告參數(shù),即:addopts = -s --html=./report.html 
    # -s:輸出程序運(yùn)行信息
    # --html=./report.html 在當(dāng)前目錄下生成report.html文件
    ? 若要生成xml文件,可將--html=./report.html 改成 --html=./report.xml
2.命令行進(jìn)入Test_App目錄
3.執(zhí)行命令: pytest
執(zhí)行結(jié)果:
    1.在當(dāng)前目錄會(huì)生成assets文件夾和report.html文件
 

5.pytest的高階用法(一)

前置條件:

1.文件路徑:

Test_App
    - - test_abc.py
    - - pytest.ini

2.pyetst.ini配置文件內(nèi)容:

 [pytest]
  命令行參數(shù)
 addopts = -s
 搜索文件名
 python_files = test*.py
  搜索的類名
 python_classes = Test*
搜索的函數(shù)名
 python_functions = test_*

5.1pytest之fixture

fixture修飾器來標(biāo)記固定的工廠函數(shù),在其他函數(shù),模塊,類或整個(gè)工程調(diào)用它時(shí)會(huì)被激活并優(yōu)先執(zhí)行,通常會(huì)被用于完成預(yù)置處理和重復(fù)操作。

方法:

fixture(scope="function", params=None, autouse=False, ids=None, name=None)

常用參數(shù):

 scope:被標(biāo)記方法的作用域

 function(default):作用于每個(gè)測試方法,每個(gè)test都運(yùn)行一次

class:作用于整個(gè)類,每個(gè)class的所有test只運(yùn)行一次

module:作用于整個(gè)模塊,每個(gè)module的所有test只運(yùn)行一次

 session:作用于整個(gè)session(慎用),每個(gè)session只運(yùn)行一次

 params:(list類型)提供參數(shù)數(shù)據(jù),供調(diào)用標(biāo)記方法的函數(shù)使用

 autouse:是否自動(dòng)運(yùn)行,默認(rèn)為False不運(yùn)行,設(shè)置為True自動(dòng)運(yùn)行

5.2fixture第一個(gè)例子(通過參數(shù)引用)

示例:

class Test_ABC:
    @pytest.fixture()
    def before(self):
        print("------->before")
    def test_a(self,before): # ? test_a方法傳入了被fixture標(biāo)識(shí)的函數(shù),已變量的形式
        print("------->test_a")
        assert 1
if __name__ == '__main__':
    pytest.main("-s  test_abc.py")
執(zhí)行結(jié)果:
    test_abc.py 
        ------->before # 發(fā)現(xiàn)before會(huì)優(yōu)先于測試函數(shù)運(yùn)行
        ------->test_a
         .

5.3.fixture第二個(gè)例子(通過函數(shù)引用)

示例:

import pytest
@pytest.fixture() # fixture標(biāo)記的函數(shù)可以應(yīng)用于測試類外部
def before():
    print("------->before")
@pytest.mark.usefixtures("before")
class Test_ABC:
    def setup(self):
        print("------->setup")
    def test_a(self):
        print("------->test_a")
        assert 1
if __name__ == '__main__':
          pytest.main("-s  test_abc.py")
  執(zhí)行結(jié)果:
      test_abc.py 
      ------->before # 發(fā)現(xiàn)before會(huì)優(yōu)先于測試類運(yùn)行
      ------->setup
      ------->test_a
      .

5.4.fixture第三個(gè)例子(默認(rèn)設(shè)置為運(yùn)行)

示例:

 import pytest
 @pytest.fixture(autouse=True) # 設(shè)置為默認(rèn)運(yùn)行
 def before():
     print("------->before")
 class Test_ABC:
     def setup(self):
         print("------->setup")
     def test_a(self):
         print("------->test_a")
         assert 1
 if __name__ == '__main__':
     pytest.main("-s  test_abc.py")
執(zhí)行結(jié)果:
    test_abc.py 
    ------->before # 發(fā)現(xiàn)before自動(dòng)優(yōu)先于測試類運(yùn)行
    ------->setup
    ------->test_a
        .

5.5.fixture第四個(gè)例子(設(shè)置作用域?yàn)閒unction)

示例:

    import pytest
    @pytest.fixture(scope='function',autouse=True) # 作用域設(shè)置為function,自動(dòng)運(yùn)行
    def before():
        print("------->before")
    class Test_ABC:
        def setup(self):
            print("------->setup")
        def test_a(self):
            print("------->test_a")
            assert 1
        def test_b(self):
            print("------->test_b")
            assert 1
    if __name__ == '__main__':
        pytest.main("-s  test_abc.py")
執(zhí)行結(jié)果:
    test_abc.py
        ------->before # 運(yùn)行第一次
        ------->setup
        ------->test_a
        .------->before # 運(yùn)行第二次
        ------->setup
        ------->test_b
        .

5.6.fixture第五個(gè)例子(設(shè)置作用域?yàn)閏lass)

示例:

    import pytest
    @pytest.fixture(scope='class',autouse=True) # 作用域設(shè)置為class,自動(dòng)運(yùn)行
    def before():
        print("------->before")
    class Test_ABC:
        def setup(self):
            print("------->setup")
        def test_a(self):
            print("------->test_a")
            assert 1
        def test_b(self):
            print("------->test_b")
            assert 1
    if __name__ == '__main__':
        pytest.main("-s  test_abc.py")
執(zhí)行結(jié)果:
    test_abc.py
    ------->before # 發(fā)現(xiàn)只運(yùn)行一次
    ------->setup
        ------->test_a
        .
        ------->setup
        ------->test_b
        .

5.7.fixture第六個(gè)例子(返回值)

示例一:

     import pytest
    @pytest.fixture()
    def need_data():
        return 2 # 返回?cái)?shù)字2
    class Test_ABC:
        def test_a(self,need_data):
            print("------->test_a")
            assert need_data != 3 # 拿到返回值做一次斷言
    if __name__ == '__main__':
        pytest.main("-s  test_abc.py")
執(zhí)行結(jié)果:
    test_abc.py 
    ------->test_a
    .
``
 

示例二:

import pytest
@pytest.fixture(params=[1, 2, 3])
def need_data(request): # 傳入?yún)?shù)request 系統(tǒng)封裝參數(shù)
    return request.param # 取列表中單個(gè)值,默認(rèn)的取值方式
class Test_ABC:
    def test_a(self,need_data):
        print("------->test_a")
        assert need_data != 3 # 斷言need_data不等于3
if __name__ == '__main__':
    pytest.main("-s  test_abc.py")
 執(zhí)行結(jié)果:
      # 可以發(fā)現(xiàn)結(jié)果運(yùn)行了三次
      test_abc.py 
      1
      ------->test_a
      .
      2
      ------->test_a
      .
      3
      ------->test_a
      F

6.Pytest高階用法(二)

前置條件:

1.文件路徑:

- Test_App
- - test_abc.py
- - pytest.ini

2.pyetst.ini配置文件內(nèi)容:

[pytest]
命令行參數(shù)
addopts = -s
搜索文件名
python_files = test_*.py
 搜索的類名
python_classes = Test_*
 搜索的函數(shù)名
python_functions = test_*

6.1.跳過測試函數(shù)

根據(jù)特定的條件,不執(zhí)行標(biāo)識(shí)的測試函數(shù).

 方法:

 skipif(condition, reason=None)

 參數(shù):

condition:跳過的條件,必傳參數(shù)

reason:標(biāo)注原因,必傳參數(shù)

 使用方法:

 @pytest.mark.skipif(condition, reason="xxx") 

示例:

import pytest
class Test_ABC:
    def setup_class(self):
        print("------->setup_class")
    def teardown_class(self):
        print("------->teardown_class")
    def test_a(self):
        print("------->test_a")
        assert 1
    @pytest.mark.skipif(condition=2>1,reason = "跳過該函數(shù)") # 跳過測試函數(shù)test_b
    def test_b(self):
        print("------->test_b")
            assert 0
執(zhí)行結(jié)果:
   test_abc.py 
   ------->setup_class
   ------->test_a #只執(zhí)行了函數(shù)test_a
   .
   ------->teardown_class
       s # 跳過函數(shù)```
 

6.2 標(biāo)記為預(yù)期失敗函數(shù)

標(biāo)記測試函數(shù)為失敗函數(shù)

 方法:

 xfail(condition=None, reason=None, raises=None, run=True, strict=False)

 常用參數(shù):

condition:預(yù)期失敗的條件,必傳參數(shù)

reason:失敗的原因,必傳參數(shù)

 使用方法:

     @pytest.mark.xfail(condition, reason="xx")

示例:

import pytest
class Test_ABC:
    def setup_class(self):
        print("------->setup_class")
    def teardown_class(self):
        print("------->teardown_class")
    def test_a(self):
        print("------->test_a")
        assert 1
    @pytest.mark.xfail(2 > 1, reason="標(biāo)注為預(yù)期失敗") # 標(biāo)記為預(yù)期失敗函數(shù)test_b
       def test_b(self):
           print("------->test_b")
          assert 0
   執(zhí)行結(jié)果:
       test_abc.py 
       ------->setup_class
       ------->test_a
       .
       ------->test_b
       ------->teardown_class
       x  # 失敗標(biāo)記

6.3 函數(shù)數(shù)據(jù)參數(shù)化

方便測試函數(shù)對測試屬于的獲取。

 方法:

     parametrize(argnames, argvalues, indirect=False, ids=None, scope=None)

 常用參數(shù):

argnames:參數(shù)名

argvalues:參數(shù)對應(yīng)值,類型必須為list

當(dāng)參數(shù)為一個(gè)時(shí)格式:[value]

當(dāng)參數(shù)個(gè)數(shù)大于一個(gè)時(shí),格式為:

[(param_value1,param_value2.....),(param_value1,param_value2.....)]

 使用方法:

     @pytest.mark.parametrize(argnames,argvalues)

參數(shù)值為N個(gè),測試方法就會(huì)運(yùn)行N次

單個(gè)參數(shù)示例:

import pytest
class Test_ABC:
    def setup_class(self):
        print("------->setup_class")
    def teardown_class(self):
        print("------->teardown_class")
@pytest.mark.parametrize("a",[3,6]) # a參數(shù)被賦予兩個(gè)值,函數(shù)會(huì)運(yùn)行兩遍
def test_a(self,a): # 參數(shù)必須和parametrize里面的參數(shù)一致
    print("test data:a=%d"%a)
    assert a%3 == 0
    執(zhí)行結(jié)果:
    test_abc.py 
    ------->setup_class
    test data:a=3 # 運(yùn)行第一次取值a=3
    .
    test data:a=6 # 運(yùn)行第二次取值a=6
    . 
    ------->teardown_class

多個(gè)參數(shù)示例:

import pytest
class Test_ABC:
    def setup_class(self):
        print("------->setup_class")
    def teardown_class(self):
        print("------->teardown_class")
@pytest.mark.parametrize("a,b",[(1,2),(0,3)]) # 參數(shù)a,b均被賦予兩個(gè)值,函數(shù)會(huì)運(yùn)行兩遍
def test_a(self,a,b): # 參數(shù)必須和parametrize里面的參數(shù)一致
    print("test data:a=%d,b=%d"%(a,b))
    assert a+b == 3
    執(zhí)行結(jié)果:
    test_abc.py 
    ------->setup_class
    test data:a=1,b=2 # 運(yùn)行第一次取值 a=1,b=2
    .
    test data:a=0,b=3 # 運(yùn)行第二次取值 a=0,b=3
    .
    ------->teardown_class

函數(shù)返回值類型示例:

import pytest
def return_test_data():
    return [(1,2),(0,3)]
class Test_ABC:
    def setup_class(self):
        print("------->setup_class")
    def teardown_class(self):
            print("------->teardown_class")
@pytest.mark.parametrize("a,b",return_test_data()) # 使用函數(shù)返回值的形式傳入?yún)?shù)值
def test_a(self,a,b):
    print("test data:a=%d,b=%d"%(a,b))
    assert a+b == 3
    執(zhí)行結(jié)果:
    test_abc.py 
    ------->setup_class
    test data:a=1,b=2 # 運(yùn)行第一次取值 a=1,b=2
    .
    test data:a=0,b=3 # 運(yùn)行第二次取值 a=0,b=3
    .
        ------->teardown_class

6.4 修改 Python traceback 輸出

pytest --showlocals     # show local variables in tracebacks
pytest -l               # show local variables (shortcut)
pytest --tb=auto        # (default) 'long' tracebacks for the first and last
                        # entry, but 'short' style for the other entries
pytest --tb=long        # exhaustive, informative traceback formatting
pytest --tb=short       # shorter traceback format
pytest --tb=line        # only one line per failure
pytest --tb=native      # Python standard library formatting
pytest --tb=no          # no traceback at all

--full-trace參數(shù)會(huì)打印更多的錯(cuò)誤輸出信息,比參數(shù) --tb=long 還多,即使是 Ctrl+C 觸發(fā)的錯(cuò)誤,也會(huì)打印出來

6.5 執(zhí)行失敗的時(shí)候跳轉(zhuǎn)到 PDB

執(zhí)行用例的時(shí)候,跟參數(shù) --pdb,這樣失敗的時(shí)候,每次遇到失敗,會(huì)自動(dòng)跳轉(zhuǎn)到 PDB

pytest --pdb              # 每次遇到失敗都跳轉(zhuǎn)到 PDB
pytest -x --pdb           # 第一次遇到失敗就跳轉(zhuǎn)到 PDB,結(jié)束測試執(zhí)行
pytest --pdb --maxfail=3  # 只有前三次失敗跳轉(zhuǎn)到 PDB 

6.6 設(shè)置斷點(diǎn)

在用例腳本中加入如下python代碼,pytest會(huì)自動(dòng)關(guān)閉執(zhí)行輸出的抓取,這里,其他test腳本不會(huì)受到影響,帶斷點(diǎn)的test上一個(gè)test正常輸出

import pdb; pdb.set_trace()

6.7 獲取用例執(zhí)行性能數(shù)據(jù)

獲取最慢的10個(gè)用例的執(zhí)行耗時(shí)

pytest --durations=10

6.8 生成 JUnitXML 格式的結(jié)果文件

這種格式的結(jié)果文件可以被Jenkins或其他CI工具解析

pytest --junitxml=path

6.9禁用插件 

例如,關(guān)閉 doctest 插件

pytest -p no:doctest

6.10 從Python代碼中調(diào)用pytest

pytest.main()                      # 基本用法
pytest.main(['-x', 'mytestdir'])   # 傳入配置參數(shù)
// 指定自定義的或額外的插件
# content of myinvoke.py
import pytest
class MyPlugin(object):
    def pytest_sessionfinish(self):
        print("*** test run reporting finishing")
pytest.main(["-qq"], plugins=[MyPlugin()])

6.11 測試腳本遷移后快速部署包含pytest的virtualenv

例如你從Gitlab倉庫里clone了項(xiàng)目組的刀刀同學(xué)編寫的測試腳本到你自己的電腦里,你想修改些東西,并調(diào)試,咋辦?可以通過下面的操作快速創(chuàng)建 VirtualEnv

cd <repository>
pip install -e .

This will set up a symlink to your code in site-packages, allowing you to edit your code while
your tests run against it as if it were installed.
Setting up your project in development mode lets you avoid having to reinstall every time you want to run your tests,
and is less brittle than mucking about with sys.path to point your tests at local code.
Also consider using tox

遇到的問題

pytest可以輸出覆蓋率的html報(bào)告

使用命令如下:

pytest -vv --cov=./ --cov-report=html
open htmlcov/index.html 

有可能遇到報(bào)錯(cuò):

(venv) zhangxiaofans-MacBook-Pro:mgap-mendel joe$ pytest --cov-report=html
usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --cov-report=html
  inifile: None
  rootdir: /Users/joe/workspace/platform/mgap-mendel/mgap-mendel

原因:

缺少pytest cov的包

解決方法

pip install pytest-cov

以上就是Python測試框架pytest高階用法全面詳解的詳細(xì)內(nèi)容,更多關(guān)于Python測試框架pytest的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python讀取預(yù)處理DICOM文件方式詳解

    Python讀取預(yù)處理DICOM文件方式詳解

    這篇文章主要介紹了Python讀取預(yù)處理DICOM文件方式,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • python查找第k小元素代碼分享

    python查找第k小元素代碼分享

    這篇文章分享了python查找第k小的元素程序代碼,大家參考使用吧
    2013-12-12
  • 穩(wěn)扎穩(wěn)打?qū)WPython之容器 可迭代對象 迭代器 生成器專題講解

    穩(wěn)扎穩(wěn)打?qū)WPython之容器 可迭代對象 迭代器 生成器專題講解

    在剛開始學(xué)Python的時(shí)候,是不是經(jīng)常會(huì)聽到大佬們在講容器、可迭代對象、迭代器、生成器、列表/集合/字典推導(dǎo)式等等眾多概念,其實(shí)這不是大佬們沒事就擱那扯專業(yè)術(shù)語來裝B,而是這些東西都得要明白的,光知道字符串、列表等基礎(chǔ)還是不夠的,尤其是在Python的數(shù)據(jù)結(jié)構(gòu)方面
    2021-10-10
  • python中cv2.imdecode()與cv2.imencode()的使用小結(jié)

    python中cv2.imdecode()與cv2.imencode()的使用小結(jié)

    本文介紹了cv2.imencode()和cv2.imdecode()函數(shù)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • tensorflow如何將one_hot標(biāo)簽和數(shù)字(整數(shù))標(biāo)簽進(jìn)行相互轉(zhuǎn)化

    tensorflow如何將one_hot標(biāo)簽和數(shù)字(整數(shù))標(biāo)簽進(jìn)行相互轉(zhuǎn)化

    這篇文章主要介紹了tensorflow如何將one_hot標(biāo)簽和數(shù)字(整數(shù))標(biāo)簽進(jìn)行相互轉(zhuǎn)化問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 教你用python將數(shù)據(jù)寫入Excel文件中

    教你用python將數(shù)據(jù)寫入Excel文件中

    Python作為一種腳本語言相較于shell具有更強(qiáng)大的文件處理能力,下面這篇文章主要給大家介紹了關(guān)于如何用python將數(shù)據(jù)寫入Excel文件中的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • 使用python生成目錄樹

    使用python生成目錄樹

    這篇文章主要為大家詳細(xì)介紹了使用python生成目錄樹、文件的程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • matplotlib共享坐標(biāo)軸的實(shí)現(xiàn)(X或Y坐標(biāo)軸)

    matplotlib共享坐標(biāo)軸的實(shí)現(xiàn)(X或Y坐標(biāo)軸)

    在作圖的過程中,我們經(jīng)常會(huì)遇到子圖共用坐標(biāo)軸的情況,或是共用橫軸標(biāo)軸,也可能是縱坐標(biāo)軸。本文就介紹了matplotlib共享坐標(biāo)軸,感興趣的可以了解一下
    2021-05-05
  • python庫umap有效地揭示高維數(shù)據(jù)的結(jié)構(gòu)和模式初探

    python庫umap有效地揭示高維數(shù)據(jù)的結(jié)構(gòu)和模式初探

    這篇文章主要介紹了python庫umap有效地揭示高維數(shù)據(jù)的結(jié)構(gòu)和模式初探,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • Python實(shí)現(xiàn)密碼薄文件讀寫操作

    Python實(shí)現(xiàn)密碼薄文件讀寫操作

    這篇文章主要介紹了Python實(shí)現(xiàn)密碼薄文件讀寫操作,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12

最新評論

双鸭山市| 和政县| 精河县| 武城县| 柳江县| 安义县| 民乐县| 固阳县| 青川县| 东至县| 怀柔区| 门源| 西乡县| 左云县| 玛多县| 阳高县| 岢岚县| 肃北| 红河县| 肥东县| 鲁山县| 兴国县| 土默特左旗| 双流县| 梅河口市| 天台县| 张北县| 庆安县| 柘荣县| 敦煌市| 晋宁县| 十堰市| 诸暨市| 义马市| 额尔古纳市| 彭泽县| 广南县| 昭通市| 德令哈市| 天峻县| 灵川县|