日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python學(xué)習(xí)筆記|python之pytest

系統(tǒng) 1835 0

Pytest

1.安裝

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

1.編寫(xiě)規(guī)則

  • 測(cè)試文件以 test_ 開(kāi)頭或以_test結(jié)尾也可以
  • 測(cè)試函數(shù)以 test_ 開(kāi)頭
  • 測(cè)試類(lèi)以 Test 開(kāi)頭,并不能有 __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 中安裝,使用的時(shí)候?qū)? import pytest

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

2.1 pytest-參數(shù)化

使用 pip install pytest-parametrize 安裝

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

代碼如下:

            
              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

  • 多個(gè)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

多個(gè)assert語(yǔ)句,當(dāng)出現(xiàn)錯(cuò)誤的時(shí)候,不會(huì)繼續(xù)往下執(zhí)行,如果繼續(xù)執(zhí)行,使用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

當(dāng)出錯(cuò)誤時(shí),會(huì)重復(fù)執(zhí)行,run次數(shù)可以設(shè)置,pytest --reruns 5

2.4 pytest-ordering

按照order順序執(zhí)行

            
              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的默認(rèn)外觀,增加了一個(gè)進(jìn)度條,并立即顯示失敗的測(cè)試

2.6 pytest-cov

pytest-cov增加了對(duì)pytest的覆蓋支持,以顯示哪些代碼行已經(jīng)測(cè)試,哪些沒(méi)有。它還將包括項(xiàng)目的測(cè)試覆蓋率。

3.pytest高級(jí)用法

測(cè)試用例的執(zhí)行之前而初始化一些數(shù)據(jù)及方法,相當(dāng)于Unittest中的setUp,tearDown

3.1 fixture參數(shù)

fixture默認(rèn)參數(shù)為function級(jí)別的,function:每一個(gè)函數(shù)或方法都會(huì)調(diào)用

            
              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
        

            
          

以上結(jié)果執(zhí)行4次loginlogout,默認(rèn)為function級(jí)別,即:每個(gè)函數(shù)或方法執(zhí)行一次

3.2 fixture的scope用法

使用scope設(shè)置級(jí)別,scope有以下級(jí)別:

  • function:每一個(gè)函數(shù)或方法都會(huì)調(diào)用

  • class:每一個(gè)類(lèi)調(diào)用一次,一個(gè)類(lèi)可以有多個(gè)方法

  • module:每一個(gè).py文件調(diào)用一次,該文件內(nèi)又有多個(gè)function和class

  • session:是多個(gè)文件調(diào)用一次,可以跨.py文件調(diào)用,每個(gè).py文件就是module

3.2.1 funtion級(jí)別

作用范圍是每個(gè)測(cè)試用例來(lái)之前運(yùn)行一次

            
              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"])


            
          

以上結(jié)果,打印了兩個(gè)conftest,原因是兩個(gè)方法設(shè)置了fixture,默認(rèn)為function

            
              test_confest.py conftest
.conftest1
conftest
.conftest2

            
          

3.2.2 class級(jí)別

如果一個(gè)class里面有多個(gè)用例,都調(diào)用了此fixture,那么此fixture只在該class里所有用例開(kāi)始前執(zhí)行一次

            
              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"])

            
          

以上結(jié)果為執(zhí)行一次confest,原因是scope設(shè)置為class,而其中只有一個(gè)class,故只有一個(gè)confest

3.2.3 module級(jí)別

當(dāng)前.py腳本里面所有用例開(kāi)始前只執(zhí)行一次

            
              @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"])


            
          

測(cè)試結(jié)果:

            
              test_confest.py conftest
.conftest1
.conftest2

            
          

3.2.4 session級(jí)別

fixture為session級(jí)別是可以跨.py模塊調(diào)用的,也就是當(dāng)我們有多個(gè).py文件的用例時(shí)候,如果多個(gè)用例只需調(diào)用一次fixture,那就可以設(shè)置為scope=“session”,并且寫(xiě)到conftest.py文件里

            
              conftest.py

import pytest

@pytest.fixture(scope="session")
def first():
    print("session級(jí)別,每一個(gè)py文件執(zhí)行一次")
    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"])

            
          

結(jié)果如下:

            
              test_conftest1.py session級(jí)別,每一個(gè)py文件執(zhí)行一次
.test_conftest1=2

            
          
            
              test_conftest2.py session級(jí)別,每一個(gè)py文件執(zhí)行一次
.test_conftest_demo1=2
.test_conftest2_demo2=2

            
          

3.2.5 conftest

conftest.py文件名稱(chēng)是固定的,pytest會(huì)自動(dòng)識(shí)別該文件。放到工程的根目錄下,就可以全局調(diào)用了,如果放到某個(gè)package包下,那就只在該package內(nèi)有效

3.3 fixture的usefixtures用法

將定義在函數(shù)里面的放在外面,使用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設(shè)置是否自動(dòng)使用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


            
          

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。。?/p>

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 阳新县| 宜兰县| 泽州县| 沛县| 阿拉善左旗| 革吉县| 安塞县| 文水县| 茌平县| 荔波县| 开江县| 利辛县| 河东区| 永春县| 关岭| 咸宁市| 枣强县| 上思县| 伊吾县| 淳安县| 苗栗市| 化隆| 浙江省| 漯河市| 泗洪县| 仪征市| 宜黄县| 雅安市| 微山县| 思南县| 泸溪县| 洞口县| 济宁市| 双峰县| 阳新县| 察哈| 永川市| 永春县| 昌吉市| 大埔县| 泊头市|