欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pytest?fixture及conftest相關(guān)詳解

 更新時(shí)間:2022年09月15日 14:31:53   作者:大剛測(cè)試開(kāi)發(fā)實(shí)戰(zhàn)???????  
這篇文章主要介紹了Pytest?fixture及conftest相關(guān)詳解,fixture是在測(cè)試函數(shù)運(yùn)行前后,由pytest執(zhí)行的外殼函數(shù),更多相關(guān)內(nèi)容需要的朋友可以參考一下

前言

fixture是在測(cè)試函數(shù)運(yùn)行前后,由pytest執(zhí)行的外殼函數(shù)。fixture中的代碼可以定制,滿(mǎn)足多變的測(cè)試需求,包括定義傳入測(cè)試中的數(shù)據(jù)集、配置測(cè)試前系統(tǒng)的初始狀態(tài)、為批量測(cè)試提供數(shù)據(jù)源等等。fixture是pytest的精髓所在,類(lèi)似unittest中setup/teardown,但是比它們要強(qiáng)大、靈活很多,它的優(yōu)勢(shì)是可以跨文件共享。

一、Pytest fixture

1.pytest fixture幾個(gè)關(guān)鍵特性

  • 有獨(dú)立的命名,并通過(guò)聲明它們從測(cè)試函數(shù)、模塊、類(lèi)或整個(gè)項(xiàng)目中的使用來(lái)激活
  • 按模塊化的方式實(shí)現(xiàn),每個(gè)fixture都可以互相調(diào)用
  • fixture可以實(shí)現(xiàn)unittest不能實(shí)現(xiàn)的功能,比如unittest中的測(cè)試用例和測(cè)試用例之間是無(wú)法傳遞參數(shù)和數(shù)據(jù)的,但是fixture卻可以解決這個(gè)問(wèn)題
  • fixture的范圍從簡(jiǎn)單的單元擴(kuò)展到復(fù)雜的功能測(cè)試,允許根據(jù)配置和組件選項(xiàng)對(duì)fixture和測(cè)試用例進(jìn)行參數(shù)化

2.Pytest fixture定義

  • 定義fixture跟定義普通函數(shù)差不多,唯一區(qū)別就是在函數(shù)上加個(gè)裝飾器@pytest.fixture(),fixture命名不要用test_開(kāi)頭,跟用例區(qū)分開(kāi)。用例才是test_開(kāi)頭的命名;
  • fixture裝飾器里的scope有四個(gè)級(jí)別的參數(shù):function(不寫(xiě)默認(rèn)這個(gè))、class、module、session;
  • fixture可以有返回值,如果沒(méi)有return,默認(rèn)會(huì)是None;用例調(diào)用fixture的返回值,就是直接把fixture的函數(shù)名稱(chēng)作為參數(shù)傳入;
  • fixture可以返回一個(gè)元組、列表或字典;
  • 測(cè)試用例可傳單個(gè)、多個(gè)fixture參數(shù);
  • fixture與fixture間可相互調(diào)用;

3.Pytest fixture用法

1)用法一:作為參數(shù)使用

fixture的名字直接作為測(cè)試用例的參數(shù),用例調(diào)用fixture的返回值,直接將fixture的函數(shù)名稱(chēng)當(dāng)做變量名稱(chēng);如果用例需要用到多個(gè)fixture的返回?cái)?shù)據(jù),fixture也可以返回一個(gè)元祖,list或字典,然后從里面取出對(duì)應(yīng)數(shù)據(jù)。

① 將fixture函數(shù)作為參數(shù)傳遞給測(cè)試用例

@pytest.fixture()
def login():
print("this is login fixture")
user = "chen"
pwd = 123456
return user, pwd

def test_login(login):
"""將fixture修飾的login函數(shù)作為參數(shù)傳遞給本用例"""
print(login)
assert login[0] == "chen"
assert login[1] == 123456
assert "chen" in str(login)

② 同一個(gè)用例中傳入多個(gè)fixture函數(shù)

@pytest.fixture()
def user():
user = "cris"
return user

@pytest.fixture()
def pwd():
pwd = "123456"
return pwd

def test_trans_fixture(user, pwd):
"""同一條用例中傳入多個(gè)fixture函數(shù)"""
print(user, pwd)
assert "cris" in str(user)
assert pwd == "123456"

③ fixture函數(shù)之間的相互傳遞

@pytest.fixture()
def user2():
user = "cris"
return user

@pytest.fixture()
def login_info(user2):
"""fixture與fixture函數(shù)之間的相互傳遞"""
pwd = "e10adc3949ba59abbe56e057f20f883e"
return user2, pwd

def test_assert_login_info(login_info):
print(login_info)
print(type(login_info))
assert login_info[0] == "cris"
assert login_info[1] == "e10adc3949ba59abbe56e057f20f883e"

2)用法二:提供靈活的類(lèi)似setup和teardown功能

Pytest的fixture另一個(gè)強(qiáng)大的功能就是在函數(shù)執(zhí)行前后增加操作,類(lèi)似setup和teardown操作,但是比setup和teardown的操作更加靈活;具體使用方式是同樣定義一個(gè)函數(shù),然后用裝飾器標(biāo)記為fixture,然后在此函數(shù)中使用一個(gè)yield語(yǔ)句,yield語(yǔ)句之前的就會(huì)在測(cè)試用例之前使用,yield之后的語(yǔ)句就會(huì)在測(cè)試用例執(zhí)行完成之后再執(zhí)行。

@pytest.fixture()
def run_function():
print("run before function...")
yield
print("run after function...")

def test_run_1(run_function):
print("case 1")

def test_run_2():
print("case 2")

def test_run_3(run_function):
print("case 3")

運(yùn)行結(jié)果如下:

常見(jiàn)的應(yīng)用場(chǎng)景:@pytest.fixture可以用在selenium中測(cè)試用例執(zhí)行前后打開(kāi)、關(guān)閉瀏覽器的操作:

@pytest.fixture()
def fixture_driver():
driver = webdriver.Chrome()
yield driver
driver.quit()
def test_baidu(fixture_driver):
driver = fixture_driver
driver.get("http://www.baidu.com")
driver.find_element_by_id('kw').send_keys("python fixture")
driver.find_element_by_id('su').click()

3)用法三:利用pytest.mark.usefixtures疊加調(diào)用多個(gè)fixture

如果一個(gè)方法或者一個(gè)class用例想要同時(shí)調(diào)用多個(gè)fixture,可以使用@pytest.mark.usefixtures()進(jìn)行疊加。注意疊加順序,先執(zhí)行的放底層,后執(zhí)行的放上層。

需注意:

  • ① 與直接傳入fixture不同的是,@pytest.mark.usefixtures無(wú)法獲取到被fixture裝飾的函數(shù)的返回值;
  • ② @pytest.mark.usefixtures的使用場(chǎng)景是:被測(cè)試函數(shù)需要多個(gè)fixture做前后置工作時(shí)使用;
@pytest.fixture
def func_1():
print("用例前置操作---1")
yield
print("用例后置操作---1")

@pytest.fixture
def func_2():
print("用例前置操作---2")
yield
print("用例后置操作---2")

@pytest.fixture
def func_3():
print("用例前置操作---3")
yield
print("用例后置操作---3")

@pytest.mark.usefixtures("func_3") # 最后執(zhí)行func_3
@pytest.mark.usefixtures("func_2") # 再執(zhí)行func_1
@pytest.mark.usefixtures("func_1") # 先執(zhí)行func_1
def test_func():
print("這是測(cè)試用例")

執(zhí)行結(jié)果:

4)用法四:fixture自動(dòng)使用autouse=True

當(dāng)用例很多的時(shí)候,每次都傳這個(gè)參數(shù),會(huì)很麻煩。fixture里面有個(gè)參數(shù)autouse,默認(rèn)是False沒(méi)開(kāi)啟的,可以設(shè)置為T(mén)rue開(kāi)啟自動(dòng)使用fixture功能,這樣用例就不用每次都去傳參了,autouse設(shè)置為T(mén)rue,自動(dòng)調(diào)用fixture功能。所有用例都會(huì)生效,包括類(lèi)中的測(cè)試用例和類(lèi)以外的測(cè)試用例

@pytest.fixture(autouse=True, scope="function")
def func_auto():
"""autouse為T(mén)rue時(shí),會(huì)作用于每一條用例"""
print("\n---用例前置操作---")
yield
print("---用例后置操作---")

# func_auto函數(shù)的autouse=True時(shí),無(wú)論是否使用usefixtures引用func_auto,都會(huì)執(zhí)行func_auto
@pytest.mark.usefixtures("func_auto")
def test_01():
print("case 1")

def test_02():
print("case 2")

class Test:
def test_03(self):
print("case 3")

執(zhí)行結(jié)果:

4.Pytest fixture四種作用域

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

fixture里面有個(gè)scope參數(shù)可以控制fixture的作用范圍:

  • 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è)級(jí)別會(huì)結(jié)合conftest.py文件使用)

1)function級(jí)別

function默認(rèn)模式為@pytest.fixture() 函數(shù)級(jí)別,即scope="function",scope可以不寫(xiě)。每一個(gè)函數(shù)或方法都會(huì)調(diào)用,每個(gè)測(cè)試用例執(zhí)行前都會(huì)執(zhí)行一次function級(jí)別的fixture。

# @pytest.fixture(scope="function")等價(jià)于@pytest.fixture()
@pytest.fixture(scope="function")
def func_auto():
"""用例級(jí)別fixture,作用域單個(gè)用例"""
print("\n---function級(jí)別的用例前置操作---")
yield
print("---function級(jí)別的用例后置操作---")

# test_01會(huì)引用func_auto函數(shù),test_02沒(méi)有用修飾器修飾,故不會(huì)引用
def test_func_auto_fixture_1(func_auto):
print("func 1 print")

def test_func_auto_fixture_2():
print("func 2 print")

2)class級(jí)別

fixture的scope值還可以是class,此時(shí)則fixture定義的動(dòng)作就會(huì)在測(cè)試類(lèi)class的所有用例之前和之后運(yùn)行,需注意:測(cè)試類(lèi)中只要有一個(gè)測(cè)試用例的參數(shù)中使用了class級(jí)別的fixture,則在整個(gè)測(cè)試類(lèi)的所有測(cè)試用例都會(huì)調(diào)用fixture函數(shù)

① 用例類(lèi)中的測(cè)試用例調(diào)用fixture

執(zhí)行fixture定義的動(dòng)作,以及此測(cè)試類(lèi)的所有用例結(jié)束后同樣要運(yùn)行fixture指定的動(dòng)作

@pytest.fixture(scope="class")
def class_auto():
"""類(lèi)級(jí)別fixture,作用域整個(gè)類(lèi)"""
print("\n---class級(jí)別的用例前置操作---")
yield
print("---class級(jí)別的用例后置操作---")

class TestClassAutoFixture:
# class級(jí)別的fixture任意一個(gè)用例引用即可
def test_class_auto_fixture_1(self, class_auto):
print("class 1 print")

def test_class_auto_fixture_2(self):
print("class 1 print")

測(cè)試類(lèi)中的第1條測(cè)試用例引用了fixture修飾的函數(shù),則整個(gè)測(cè)試類(lèi)的所有測(cè)試用例都會(huì)執(zhí)行fixture函數(shù)的前置操作,在所有用例執(zhí)行完成后,都會(huì)執(zhí)行fixture函數(shù)的后置操作。

② 用例類(lèi)外的測(cè)試用例調(diào)用fixture

如果在類(lèi)外的函數(shù)中去使用class級(jí)別的fixture,則此時(shí)在測(cè)試類(lèi)外每個(gè)測(cè)試用例中,fixture跟function級(jí)別的fixture作用是一致的,即在類(lèi)外的函數(shù)中引用了class級(jí)別的fixture,則在此函數(shù)之前和之后同樣去執(zhí)行fixture定義的對(duì)應(yīng)的操作。

def test_class_auto_fixture(class_auto):
print("class 1 print")

如下圖所示,測(cè)試類(lèi)外的函數(shù)引用了class級(jí)別的fixture,則它的作用會(huì)等同于function級(jí)別的fixture,

運(yùn)行結(jié)果如下:

3)module級(jí)別

在Python中module即.py文件,當(dāng)fixture定義為module時(shí),則此fixture將在當(dāng)前文件中起作用。這里需要特別說(shuō)明的是,當(dāng)fixture的scope定義為module時(shí),只要當(dāng)前文件中有一個(gè)測(cè)試用例使用了fixture,不管這個(gè)用例是在類(lèi)外,還是在類(lèi)中,都會(huì)在當(dāng)前文件(模塊)的所有測(cè)試用例執(zhí)行之前去執(zhí)行fixture定義的行為以及當(dāng)前文件的所有用例結(jié)束之后同樣去執(zhí)行fixture定義的對(duì)應(yīng)操作。

@pytest.fixture(scope="module")
def module_auto():
"""作用于整個(gè)py文件"""
print("\n---module級(jí)別的用例前置操作---")
yield
print("---module級(jí)別的用例后置操作---")

# 測(cè)試類(lèi)外和測(cè)試類(lèi)內(nèi)的函數(shù)方法都調(diào)用了module級(jí)別的fixture,但整個(gè)py文件只會(huì)生效一次fixture。
def test_module_scope_out_class(module_auto):
print("case scope 01")

class TestScope1:
def test_scope_01(self):
print("case scope 01")

def test_scope_02(self, module_auto):
print("case scope 02")

def test_scope_03(self):
print("case scope 03")

若類(lèi)中的方法分別調(diào)用了class級(jí)別的fixture和module級(jí)別的fixture,則會(huì)兩個(gè)fixture都生效:

# 順序在前面fixture會(huì)先執(zhí)行
def test_scope_01(self, module_auto, class_auto):
print("case scope 01")

若類(lèi)中的方法同時(shí)調(diào)用了function級(jí)別、class級(jí)別、module級(jí)別的fixture,則3種fixture會(huì)同時(shí)生效:

# 順序在前面fixture會(huì)先執(zhí)行
def test_scope_02(self, module_auto, class_auto, func_auto):
print("case scope 02")

4)session級(jí)別(使用conftest.py共享fixture)

當(dāng)fixture的scope定義為session時(shí),是指在當(dāng)前目錄下的所有用例之前和之后執(zhí)行fixture對(duì)應(yīng)的操作

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

使用方式:

  • ① 定義測(cè)試用例文件
  • ② 在指定目錄下創(chuàng)建conftest.py(固定命名,不可修改)文件,然后在conftest.py文件中定義fixture方法,將scope指定為session,此時(shí)在當(dāng)前目錄下只要有一個(gè)用例使用了此fixture,則就會(huì)在當(dāng)前目錄下所有用例之前和之后會(huì)執(zhí)行fixture定義的對(duì)應(yīng)的操作。
@pytest.fixture(scope="session", )
def session_auto():
"""session級(jí)別的fixture,針對(duì)該目錄下的所有用例都生效"""
print("\n---session級(jí)別的用例前置操作---")
yield
print("---session級(jí)別的用例后置操作---")

定義了session級(jí)別的fixture,存放于該用例文件的同一個(gè)目錄下的conftest.py文件中,該目錄下的任一用例文件中的任一測(cè)試用例,引用了這個(gè)session級(jí)別的fixture,則這個(gè)session級(jí)別的fixture會(huì)針對(duì)這整個(gè)用例文件會(huì)生效。若存放在根目錄下,則針對(duì)整個(gè)工程的所有用例都會(huì)生效。

class TestSessionAutoFixture:
# session級(jí)別的fixture任意一個(gè)用例引用即可
def test_session_auto_fixture_1(self, session_auto):
print("session 1 print")

def test_session_auto_fixture_2(self):
print("session 1 print")
def test_session_auto_fixture():
print("session 1 print")

運(yùn)行結(jié)果如下:

5.Pytest fixture其他參數(shù)用法

1)ids參數(shù)-修改用例結(jié)果名稱(chēng)

@pytest.mark.parametrize() 還提供了第三個(gè) ids 參數(shù)來(lái)自定義顯示結(jié)果。

stars = ["劉德華", "張學(xué)友", "黎明", "郭富城"]
# 利用列表生成式生成一個(gè)用例名稱(chēng)的列表
ids = [f"test-case-vvxyksv9kd" for d in range(len(stars))]

@pytest.mark.parametrize("name", stars, ids=ids)
def test_multi_param(name):
print(f"my name is {name}")

注:ids生成的用例名稱(chēng)數(shù)量一定要和用例數(shù)量一致,否則會(huì)報(bào)錯(cuò),

執(zhí)行結(jié)果如下:

2)name參數(shù)-重命名fixture函數(shù)名稱(chēng)

@pytest.fixture(name="rename_get_user_info")
def get_user_info():
user_name = "周潤(rùn)發(fā)"
print(user_name)

# 此處需傳入重命名后的fixture函數(shù)名
@pytest.mark.usefixtures("rename_get_user_info")
def test_parametrize_by_use_fixtures():
"""通過(guò)usefixtures裝飾器傳入fixture"""
print(f"test parametrize use fixtures")

def test_parametrize_by_fixture_name(rename_get_user_info):
"""將fixture函數(shù)名作為形參傳入"""
print(f"test parametrize use fixtures")

3)params參數(shù)-提供返回值供測(cè)試函數(shù)調(diào)用

示例一

@pytest.fixture(params=[{"name": "周潤(rùn)發(fā)"}, {"age": 61}, {"height": 183}])
def fix_func(request): # request為內(nèi)建fixture
# 使用request.param作為返回值供測(cè)試函數(shù)調(diào)用,params的參數(shù)列表中包含了做少元素,該fixture就會(huì)被調(diào)用幾次,分別作用在每個(gè)測(cè)試函數(shù)上
return request.param # request.param為固定寫(xiě)法

def test_fix_func(fix_func):
print(f"fixture函數(shù)fix_func的返回值為:{fix_func}")

"""打印結(jié)果如下:
fixture函數(shù)fix_func的返回值為:{'name': '周潤(rùn)發(fā)'}
fixture函數(shù)fix_func的返回值為:{'age': 61}
fixture函數(shù)fix_func的返回值為:{'height': 183}
"""

示例二:

params = [
{"case_id": 1, "case_title": "驗(yàn)證正常添加車(chē)輛", "car_name": "蘇C99688", "car_type": 1, "origin": 1, "expected": "200"},
{"case_id": 2, "case_title": "驗(yàn)證添加重復(fù)車(chē)輛", "car_name": "蘇C99688", "car_type": 1, "origin": 1, "expected": "500"},
{"case_id": 3, "case_title": "驗(yàn)證車(chē)牌號(hào)為空", "car_name": "", "car_type": 2, "origin": 1, "expected": "500"}]

@pytest.fixture(params=params)
def add_car_params(request):
return request.param

def test_add_car(add_car_params):
print(f"{add_car_params['case_id']}-{add_car_params['case_title']}-{add_car_params['car_name']}")

"""
運(yùn)行結(jié)果如下:
1-驗(yàn)證正常添加車(chē)輛-蘇C99688
2-驗(yàn)證添加重復(fù)車(chē)輛-蘇C99688
3-驗(yàn)證車(chē)牌號(hào)為空-
"""

6.內(nèi)置fixture

1)tmpdir和tmpdir_factory

內(nèi)置的tmpdir和tmpdir_factory負(fù)責(zé)在測(cè)試開(kāi)始運(yùn)行前創(chuàng)建臨時(shí)文件目錄,并在測(cè)試結(jié)束后刪除。如果測(cè)試代碼要對(duì)文件進(jìn)行讀/寫(xiě)操作,那么可以使用tmpdir或tmpdir_factory來(lái)創(chuàng)建文件或目錄。單個(gè)測(cè)試使用tmpdir,多個(gè)測(cè)試使用tmpdir_factory。tmpdir的作用范圍是函數(shù)級(jí)別,tmpdir_factory的作用范圍是會(huì)話(huà)級(jí)別。

def test_tmpdir(tmpdir):
# tmpdir already has a path name associated with it
# join() extends the path to include a filename
# the file is created when it's written to
a_file = tmpdir.join('something.txt')

# you can create directories
a_sub_dir = tmpdir.mkdir('anything')

# you can create files in directories (created when written)
another_file = a_sub_dir.join('something_else.txt')

# this write creates 'something.txt'
a_file.write('contents may settle during shipping')

# this write creates 'anything/something_else.txt'
another_file.write('something different')

# you can read the files as well
assert a_file.read() == 'contents may settle during shipping'
assert another_file.read() == 'something different'

def test_tmpdir_factory(tmpdir_factory):
# you should start with making a directory
# a_dir acts like the object returned from the tmpdir fixture
a_dir = tmpdir_factory.mktemp('mydir')

# base_temp will be the parent dir of 'mydir'
# you don't have to use getbasetemp()
# using it here just to show that it's available
base_temp = tmpdir_factory.getbasetemp()
print('base:', base_temp)

# the rest of this test looks the same as the 'test_tmpdir()'
# example except I'm using a_dir instead of tmpdir

a_file = a_dir.join('something.txt')
a_sub_dir = a_dir.mkdir('anything')
another_file = a_sub_dir.join('something_else.txt')

a_file.write('contents may settle during shipping')
another_file.write('something different')

assert a_file.read() == 'contents may settle during shipping'
assert another_file.read() == 'something different'

2)pytestconfig

內(nèi)置的pytestconfig可以通過(guò)命令行參數(shù)、選項(xiàng)、配置文件、插件、運(yùn)行目錄等方式來(lái)控制pytest。pytestconfig是request.config的快捷方式,它在pytest文檔里有時(shí)候被稱(chēng)為“pytest配置對(duì)象”。

要理解pytestconfig如何工作,可以添加一個(gè)自定義的命令行選項(xiàng),然后在測(cè)試中讀取該選項(xiàng)。

def pytest_addoption(parser):
""""添加一個(gè)命令行選項(xiàng)"""
parser.addoption(
"--env", default="test", choices=["dev", "test", "pre"], help="enviroment parameter")

以pytest_addoption添加的命令行選項(xiàng)必須通過(guò)插件來(lái)實(shí)現(xiàn),或者在項(xiàng)目頂層目錄的conftest.py文件中完成。它所在的conftest.py不能處于測(cè)試子目錄下。

上述是一個(gè)傳入測(cè)試環(huán)境的命令行選項(xiàng),接下來(lái)可以在測(cè)試用例中使用這些選項(xiàng)。

def test_option(pytestconfig):
print('the current environment is:', pytestconfig.getoption('env'))
# 運(yùn)行測(cè)試
pytest -s -q test_config.py::test_option

由于前面的pytest_addoption中定義的env的默認(rèn)參數(shù)是test,所以通過(guò)pytestconfig.getoption獲取到的env的值就是test:

3)其他內(nèi)置fixture

  • cache:作用是存儲(chǔ)一段測(cè)試會(huì)話(huà)的信息,在下一段測(cè)試會(huì)話(huà)中使用;
  • capsys:capsys 有兩個(gè)功能:允許使用代碼讀取 stdout 和 stderr;可以臨時(shí)禁制抓取日志輸出;
  • monkeypatch:可以在運(yùn)行期間對(duì)類(lèi)或模塊進(jìn)行動(dòng)態(tài)修改。在測(cè)試中,monkey patch 常用于替換被測(cè)試代碼的部分運(yùn)行環(huán)境,或者將輸入依賴(lài)或輸出依賴(lài)替換成更容易測(cè)試的對(duì)象或函數(shù);
  • doctest_namespace:doctest 模塊是 Python 標(biāo)準(zhǔn)庫(kù)的一部分,借助它,可以在函數(shù)的文檔字符串中放入示例代碼,并通過(guò)測(cè)試確保有效。你可以使用 --doctest-modules 標(biāo)識(shí)搜尋并運(yùn)行 doctest 測(cè)試用例;
  • recwarn:可以用來(lái)檢查待測(cè)代碼產(chǎn)生的警告信息;recwarn 的值就像是一個(gè)警告信息列表,列表里的每個(gè)警告信息都有4個(gè)屬性 category、message、filename、lineno。警告信息在測(cè)試開(kāi)始后收集,如果你在意的警告信息出現(xiàn)在測(cè)試尾部,則可以在信息收集前使用 recwarn.clear() 清除不需要的內(nèi)容。除了 recwarn,pytest 還可以使用 pytest.warns() 來(lái)檢查警告信息。

二、Pytest conftest全局作用文件詳解

Pytest支持在測(cè)試的目錄中,創(chuàng)建conftest.py文件,進(jìn)行全局配置。

conftest.py文件須知:

  • 可以跨.py文件調(diào)用,有多個(gè).py文件調(diào)用時(shí),可讓conftest.py只調(diào)用了一次fixture,或調(diào)用多次fixture;
  • conftest.py與運(yùn)行的用例要在同一個(gè)pakage下,并且有__init__.py文件;
  • 不需要import導(dǎo)入conftest.py,pytest用例會(huì)自動(dòng)識(shí)別該文件,放到項(xiàng)目的根目錄下就可以全局目錄調(diào)用了,如果放到某個(gè)package下,那就在package內(nèi)有效,可有多個(gè)conftest.py;
  • conftest.py配置腳本名稱(chēng)是固定的,不能改名稱(chēng);
  • conftest.py文件不能被其他文件導(dǎo)入;
  • 所有同目錄測(cè)試文件運(yùn)行前都會(huì)執(zhí)行conftest.py文件;

到此這篇關(guān)于Pytest fixture及conftest相關(guān)詳解的文章就介紹到這了,更多相關(guān)Pytest fixture conftest內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論