Pytest
1.安裝
- 首先使用pip3 install pytest安裝pytest
- pytest --version查看版本
1.編寫規(guī)則
-
測試文件以
test_
開頭或以_test結(jié)尾也可以 -
測試函數(shù)以
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
中安裝,使用的時候?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
- 多個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語句,當(dāng)出現(xiàn)錯誤的時候,不會繼續(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)出錯誤時,會重復(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)外觀,增加了一個進(jìn)度條,并立即顯示失敗的測試
2.6 pytest-cov
pytest-cov增加了對pytest的覆蓋支持,以顯示哪些代碼行已經(jīng)測試,哪些沒有。它還將包括項目的測試覆蓋率。
3.pytest高級用法
測試用例的執(zhí)行之前而初始化一些數(shù)據(jù)及方法,相當(dāng)于Unittest中的setUp,tearDown
3.1 fixture參數(shù)
fixture默認(rèn)參數(shù)為function級別的,function:每一個函數(shù)或方法都會調(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級別,即:每個函數(shù)或方法執(zhí)行一次
3.2 fixture的scope用法
使用scope設(shè)置級別,scope有以下級別:
-
function:每一個函數(shù)或方法都會調(diào)用
-
class:每一個類調(diào)用一次,一個類可以有多個方法
-
module:每一個.py文件調(diào)用一次,該文件內(nèi)又有多個function和class
-
session:是多個文件調(diào)用一次,可以跨.py文件調(diào)用,每個.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"])
以上結(jié)果,打印了兩個conftest,原因是兩個方法設(shè)置了fixture,默認(rèn)為function
test_confest.py conftest
.conftest1
conftest
.conftest2
3.2.2 class級別
如果一個class里面有多個用例,都調(diào)用了此fixture,那么此fixture只在該class里所有用例開始前執(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,而其中只有一個class,故只有一個confest
3.2.3 module級別
當(dāng)前.py腳本里面所有用例開始前只執(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"])
測試結(jié)果:
test_confest.py conftest
.conftest1
.conftest2
3.2.4 session級別
fixture為session級別是可以跨.py模塊調(diào)用的,也就是當(dāng)我們有多個.py文件的用例時候,如果多個用例只需調(diào)用一次fixture,那就可以設(shè)置為scope=“session”,并且寫到conftest.py文件里
conftest.py
import pytest
@pytest.fixture(scope="session")
def first():
print("session級別,每一個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級別,每一個py文件執(zhí)行一次
.test_conftest1=2
test_conftest2.py session級別,每一個py文件執(zhí)行一次
.test_conftest_demo1=2
.test_conftest2_demo2=2
3.2.5 conftest
conftest.py文件名稱是固定的,pytest會自動識別該文件。放到工程的根目錄下,就可以全局調(diào)用了,如果放到某個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è)置是否自動使用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號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
