亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Python學習筆記|python之pytest

系統 1920 0

Pytest

1.安裝

  • 首先使用pip3 install pytest安裝pytest
  • pytest --version查看版本

1.編寫規則

  • 測試文件以 test_ 開頭或以_test結尾也可以
  • 測試函數以 test_ 開頭
  • 測試類以 Test 開頭,并不能有 __init__ 方法

例如: test_pydemo.py 文件

            
              def  test_add():
    print("I am 1")
    assert add.add_test(1,3)==4
    print("I am 2")
    assert add.add_test(1, 2) == 6
    print("I am 3")
    assert add.add_test(1, 5) == 6


            
          

2.pytest用法

安裝第三方插件,可以使用 pip install 安裝或者 pycharm 中安裝,使用的時候導入 import pytest

在pycharm中,files-》settings-》tools=》python integrated tools=》設定default test runner

2.1 pytest-參數化

使用 pip install pytest-parametrize 安裝

  • pytest.mark.parametrize (參數名,參數化元組的數組)

代碼如下:

            
              import pytest

@pytest.mark.parametrize("x,y",[
    (3+5, 8),
    (2+4, 6),
    (6*9, 42),
])

def test_add_by_para(x,y):
    assert add.add_test(x,y)==x+y

            
          

2.2 pytest-assume

  • 多個assert
            
              import pytest
def  test_add():
    print("I am 1")
    assert add.add_test(1,3)==4
    print("I am 2")
    assert add.add_test(1, 2) == 6
    print("I am 3")
    assert add.add_test(1, 5) == 6


            
          
  • pytest-assume

多個assert語句,當出現錯誤的時候,不會繼續往下執行,如果繼續執行,使用pytest-assume插件

            
              import pytest

def  test_add():
    print("I am 1")
    pytest.assume(add.add_test(1,3)==4)
    print("I am 2")
    pytest.assume(add.add_test(1, 2) == 5)
    print("I am 3")
    pytest.assume(add.add_test(1, 7) == 8)


            
          

2.3 pytest-rerunfails

當出錯誤時,會重復執行,run次數可以設置,pytest --reruns 5

2.4 pytest-ordering

按照order順序執行

            
              import pytest

@pytest.mark.run(order=2)
def test_add_order():
    print("i am 2")
    assert add.add_test(4,4)==8

@pytest.mark.run(order=1)
def test_add_order1():
    print("i am 1")
    assert add.add_test(6,4)==10


            
          

2.5 pytest-sugar

pytest-sugar改變了pytest的默認外觀,增加了一個進度條,并立即顯示失敗的測試

2.6 pytest-cov

pytest-cov增加了對pytest的覆蓋支持,以顯示哪些代碼行已經測試,哪些沒有。它還將包括項目的測試覆蓋率。

3.pytest高級用法

測試用例的執行之前而初始化一些數據及方法,相當于Unittest中的setUp,tearDown

3.1 fixture參數

fixture默認參數為function級別的,function:每一個函數或方法都會調用

            
              import pytest

@pytest.fixture()
def loginlogout():
    print("Before")
    yield
    print("After")

class TestFixture():
    def test_fixture(self,loginlogout):
        assert add.add_test(2,3)==5
    def test_fixture2(self,loginlogout):
        assert add.add_test(5,3)==8

class TestFixture1():
    def test_fixture3(self,loginlogout):
        assert add.add_test(2,3)==5
    def test_fixture4(self,loginlogout):
        assert add.add_test(5,3)==8
        

            
          

以上結果執行4次loginlogout,默認為function級別,即:每個函數或方法執行一次

3.2 fixture的scope用法

使用scope設置級別,scope有以下級別:

  • function:每一個函數或方法都會調用

  • class:每一個類調用一次,一個類可以有多個方法

  • module:每一個.py文件調用一次,該文件內又有多個function和class

  • session:是多個文件調用一次,可以跨.py文件調用,每個.py文件就是module

3.2.1 funtion級別

作用范圍是每個測試用例來之前運行一次

            
              test_confest.py

@pytest.fixture()
def conftest():
    print("conftest")

def test_confest1(conftest):
    print("conftest1")
    assert 1==1

def test_confest2(conftest):
    print("conftest2")
    assert 2==2

def test_confest3():
    assert 2 == 2

if __name__ == "__main__":
    pytest.main(["-s", "test_confest.py"])


            
          

以上結果,打印了兩個conftest,原因是兩個方法設置了fixture,默認為function

            
              test_confest.py conftest
.conftest1
conftest
.conftest2

            
          

3.2.2 class級別

如果一個class里面有多個用例,都調用了此fixture,那么此fixture只在該class里所有用例開始前執行一次

            
              test_confest.py

@pytest.fixture(scope="class")
def conftest():
    print("conftest")

class TestConftest():
    def test_confest1(self,conftest):
        print("conftest1")
        assert 1==1

    def test_confest2(self,conftest):
        print("conftest2")
        assert 2==2

    def test_confest3(self,conftest):
        assert 2==2

if __name__ == "__main__":
    pytest.main(["-s", "test_confest.py"])

            
          

以上結果為執行一次confest,原因是scope設置為class,而其中只有一個class,故只有一個confest

3.2.3 module級別

當前.py腳本里面所有用例開始前只執行一次

            
              @pytest.fixture(scope="module")
def conftest():
    print("conftest")

def test_confest1(conftest):
    print("conftest1")
    assert 1==1

class TestConftest():
    def test_confest2(self,conftest):
        print("conftest2")
        assert 1==1

if __name__ == "__main__":
    pytest.main(["-s", "test_confest.py"])


            
          

測試結果:

            
              test_confest.py conftest
.conftest1
.conftest2

            
          

3.2.4 session級別

fixture為session級別是可以跨.py模塊調用的,也就是當我們有多個.py文件的用例時候,如果多個用例只需調用一次fixture,那就可以設置為scope=“session”,并且寫到conftest.py文件里

            
              conftest.py

import pytest

@pytest.fixture(scope="session")
def first():
    print("session級別,每一個py文件執行一次")
    return 2



            
          
            
              test_conftest1.py

import pytest
def test_conftest1(first):
    '''用例傳fixture'''
    print("test_conftest1=%d" % first)
    assert 1 == 1

if __name__ == "__main__":
    pytest.main(["-s", "test_conftest1.py"])


            
          
            
              test_conftest2.py

import pytest
def test_conftest_demo1(first):
    '''用例傳fixture'''
    print("test_conftest_demo1=%d" % first)
    assert 1 == 1

def test_conftest_demo2(first):
    '''用例傳fixture'''
    print("test_conftest2_demo2=%d" % first)
    assert 1 == 1

if __name__ == "__main__":
    pytest.main(["-s", "test_conftest2.py"])

            
          

結果如下:

            
              test_conftest1.py session級別,每一個py文件執行一次
.test_conftest1=2

            
          
            
              test_conftest2.py session級別,每一個py文件執行一次
.test_conftest_demo1=2
.test_conftest2_demo2=2

            
          

3.2.5 conftest

conftest.py文件名稱是固定的,pytest會自動識別該文件。放到工程的根目錄下,就可以全局調用了,如果放到某個package包下,那就只在該package內有效

3.3 fixture的usefixtures用法

將定義在函數里面的放在外面,使用usefixtures

            
              import pytest

@pytest.fixture(scope="class")
def loginlogout():
    print("Before")
    yield
    print("After")

class TestFixture():
    @pytest.mark.usefixtures("loginlogout")
    def test_fixture(self):
        assert add.add_test(2,3)==5

    @pytest.mark.usefixtures("loginlogout")
    def test_fixture2(self):
        assert add.add_test(5,3)==8

            
          

3.4 fixture的autouse用法

使用autouse設置是否自動使用fixtures

            
              import pytest

pytest.fixture(scope="class",autouse=True)
def loginlWogout():
    print("Before")
    yield
    print("After")

class TestFixture():
    def test_fixture(self):
        assert add.add_test(2,3)==5

    def test_fixture2(self):
        assert add.add_test(5,3)==8


            
          

更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲一区二区三区免费看 | 亚洲图片综合区 | 国产精品视频第一区二区三区 | 特黄a大片免费视频 | 伊人在综合 | 久久99视频精品 | 亚洲精品一二三四区 | 国产精品亚洲精品影院 | 五月久久亚洲七七综合中文网 | 久草在线中文视频 | 国产福利视精品永久免费 | 色偷偷88888欧美精品久久久 | 欧美一区二区手机在线观看视频 | 久草青青 | 久久99精品视免费看 | 免费国产精品视频 | sihu影院永久在线影院 | 欧美日韩成人午夜免费 | 国内精品久久久久久影院老狼 | 欧美日韩一区二区三区自拍 | 伊人久久成人 | 欧美视频在线不卡 | 国产精品香蕉在线一区二区 | 日本色图在线 | 久久国产精品永久免费网站 | 欧美日韩中文亚洲v在线综合 | 国产男女爽爽爽免费视频 | 99热在线这里只有精品 | 天天婷婷| 狠狠色婷婷狠狠狠亚洲综合 | 黄色录像网址 | 亚洲一区二区三区在线播放 | 一级寡妇乱色毛片全18 | 亚州毛片| 久草在线久草在线 | 国内在线播放 | 欧美日韩在线观看区一二 | 国产日韩欧美亚洲综合在线 | 男人女人真曰批的视频动态 | 老子影院午夜理伦手机不卡 | 天天操天天擦 |