目錄
- 前言
- 什么是conftest.py
- conftest.py特點(diǎn)
- conftest.py用法
- conftest.py實(shí)際案例
- test_baidu目錄下
前言
在之前介紹fixture的文章中,我們使用到了conftest.py文件,那么conftest.py文件到底該如何使用呢,下面我們就來(lái)詳細(xì)了解一下conftest.py文件的特點(diǎn)和使用方法吧
什么是conftest.py
我們之前了解了fixture,fixture可以直接定義在測(cè)試腳本中,但是有些時(shí)候,我們希望一個(gè)fixture可以被復(fù)用,這就需要對(duì)fixture進(jìn)行集中管理,Pytest使用文件conftest.py
集中管理固件.在復(fù)雜的項(xiàng)目中,可以在不同的目錄層級(jí)定義conftest.py,其作用域?yàn)槠渌诘哪夸浐妥幽夸?,通常情況下,conftest.py
和@pytest.fixture()
會(huì)結(jié)合使用,來(lái)實(shí)現(xiàn)全局的前后置處理。
conftest.py特點(diǎn)
conftest.py
文件的名稱是固定的,不能修改
conftest.py
與運(yùn)行的用例要在同一個(gè)pakage下,并且有__init__.py
文件
- 不需要
import
導(dǎo)入conftest.py
文件,pytest用例會(huì)自動(dòng)識(shí)別該文件,放到根目錄下可以全局目錄調(diào)用,放在某個(gè)package下,那就在該package內(nèi)有效
- 不同目錄可以有自己的conftest.py,一個(gè)項(xiàng)目中可以有多個(gè)
conftest.py
- pytest會(huì)默認(rèn)讀取
conftest.py
里面的所有fixture,所有同目錄測(cè)試文件運(yùn)行前都會(huì)執(zhí)行conftest.py
文件
conftest.py用法
在我們實(shí)際的測(cè)試中,conftest.py文件需要結(jié)合fixture來(lái)使用,所以fixture中參數(shù)scope也適用conftest.py中fixture的特性,這里再說(shuō)明一下
- conftest中fixture的scope參數(shù)為session,所有的測(cè)試文件執(zhí)行前(后)執(zhí)行一次
conftest.py
文件中的fixture。
- conftest中fixture的scope參數(shù)為module,每一個(gè)測(cè)試.py文件執(zhí)行前(后)都會(huì)執(zhí)行一次
conftest.py
文件中的fixture
- conftest中fixture的scope參數(shù)為class,每一個(gè)測(cè)試文件中的測(cè)試類執(zhí)行前(后)都會(huì)執(zhí)行一次
conftest.py
文件中的fixture
- conftest中fixture的scope參數(shù)為function,所有文件的測(cè)試用例執(zhí)行前(后)都會(huì)執(zhí)行一次
conftest.py
文件中的fixture
conftest.py實(shí)際案例
我們按照這樣的目錄新建一個(gè)項(xiàng)目
在根目錄conftestdemo下
根目錄中的conftest.py文件中,一般寫(xiě)全局的fixture,比如登錄
conftest.py
import pytest
@pytest.fixture(scope="session")
def login():
print("***登錄成功,返回用戶名***")
name = "rockche"
yield name
print("***退出登錄***")
@pytest.fixture(autouse=True)
def get_name(login):
name = login
print(f"--每個(gè)用例都調(diào)用外層fixiture:打印用戶name:{name}--")
根目錄下的測(cè)試用例
test_1.py
def test_get_name(login):
name = login
print("***基礎(chǔ)用例:獲取用戶name***")
print(f"用戶名:{name}")
運(yùn)行conftestdemo下的所有用例
run.py
import pytest
if __name__ == '__main__':
pytest.main(["-s", "../conftestdemo/"])
test_baidu目錄下
配置針對(duì)baidu網(wǎng)站的測(cè)試用例獨(dú)有的fixture
conftest.py
import pytest
@pytest.fixture(scope="module")
def open_baidu(login):
name = login
print(f"用戶 {name} 打開(kāi)baidu")
test_case1.py
def test_case2_01(open_baidu):
print("搜索pytest")
def test_case2_02(open_baidu):
print("搜索博客園")
test_cnblogs目錄下
沒(méi)有__init__.py
文件也沒(méi)有conftest.py文件
test_case1.py
def test_no_fixture(login):
print("沒(méi)有__init__文件,直接進(jìn)入cnblogs", login)
test_taobao目錄下
配置針對(duì)taobao網(wǎng)站的測(cè)試用例獨(dú)有的fixture
conftest.py
import pytest
@pytest.fixture(scope="function")
def open_taobao(login):
name = login
print(f"用戶 {name} 進(jìn)入淘寶")
test_case1.py
class TestTaobao:
def test_case1_01(self, open_taobao):
print("選購(gòu)商品")
def test_case1_02(self, open_taobao):
print("進(jìn)入結(jié)算界面")
運(yùn)行run.py
到此這篇關(guān)于Pytest中conftest.py的用法的文章就介紹到這了,更多相關(guān)Pytest conftest.py內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- pytest conftest.py文件的使用講解
- python pytest進(jìn)階之conftest.py詳解