目錄
前言
一、pytest單元測(cè)試框架
二、 單元測(cè)試框架和自動(dòng)化測(cè)試框架有什么關(guān)系
三、pytest簡(jiǎn)介
四、使用pytest默認(rèn)的測(cè)試用例規(guī)則及基礎(chǔ)應(yīng)用
五、pytest測(cè)試用例的運(yùn)行方式
六、pytest執(zhí)行測(cè)試用例的順序
七、如何分組執(zhí)行?編輯
八、pytest跳過用例
九、pytest框架實(shí)現(xiàn)的一些前后置(固件、夾具)處理
十、YAML文件實(shí)現(xiàn)接口自動(dòng)化
結(jié)語
前言
pytest 是一個(gè)成熟的全功能 Python 測(cè)試工具,可以幫助您編寫更好的程序。它與 Python 自帶的 Unittest 測(cè)試框架類似,但 pytest 使用起來更簡(jiǎn)潔和高效,并且兼容 unittest 框架。
一、pytest單元測(cè)試框架
(1)什么是單元測(cè)試
? ? ? ? 單元測(cè)試是指在軟件開發(fā)過程中,針對(duì)軟件的最小單位(如函數(shù)、方法等)進(jìn)行正確性的檢查測(cè)試。
(2)常見的單元測(cè)試框架
? ? ? ? Java:junit和testing
? ? ? ? python:unittest和pytest
(3)單元測(cè)試框架主要做什么?
? ? ? ? 1. 測(cè)試發(fā)現(xiàn):從多個(gè)文件中找到我們的測(cè)試用例
? ? ? ? 2. 測(cè)試執(zhí)行:按照一定的順序和規(guī)則執(zhí)行并生成結(jié)果
? ? ? ? 3. 測(cè)試判斷:通過斷言判斷預(yù)期結(jié)果和實(shí)際結(jié)果的差異
? ? ? ? 4. 測(cè)試報(bào)告:統(tǒng)計(jì)測(cè)試進(jìn)度、耗時(shí)、通過率,生成測(cè)試報(bào)告
二、 單元測(cè)試框架和自動(dòng)化測(cè)試框架有什么關(guān)系
(1)什么是自動(dòng)化測(cè)試
? ? ? ? 自動(dòng)化測(cè)試包括一切通過工具(程序)的方式來代替或輔助手工測(cè)試的行為。其主要手段是模擬手工測(cè)試,通過組織編寫自動(dòng)化腳本執(zhí)行測(cè)試用例,解決測(cè)試工作量大的問題,自動(dòng)化測(cè)試能有效的提高測(cè)試效率。
(2)自動(dòng)化測(cè)試的作用
? ? ? ? 1. 提高測(cè)試效率,降低維護(hù)成本
? ? ? ? 2. 減少人工干預(yù),提高測(cè)試準(zhǔn)確性,增加代碼的復(fù)用性
(3)二者的關(guān)系
? ? ? ? 單元測(cè)試是自動(dòng)化測(cè)試的組成部分之一
三、pytest簡(jiǎn)介
? ? ? ? 1. pytest是一個(gè)非常成熟的python單元測(cè)試框架,比unittest更加靈活,容易上手
? ? ? ? 2. pytest可以和selenium、request、qppium結(jié)合實(shí)現(xiàn)web自動(dòng)化、接口自動(dòng)化、app自動(dòng)化
? ? ? ? 3. pytest可以實(shí)現(xiàn)測(cè)試用例的跳過及rerun失敗用例重試
? ? ? ? 4. pytest可以和allure生成非常美觀的測(cè)試報(bào)告
? ? ? ? 5. pytest可以和Jenkins持續(xù)集成
? ? ? ? 6. pytest有很多強(qiáng)大的插件
? ? ? ? ? ? ? ? pytest-html (生成html格式的自動(dòng)化測(cè)試報(bào)告)
? ? ? ? ? ? ? ? pytest-xdist (測(cè)試用例分布式執(zhí)行,多cpu分發(fā))
? ? ? ? ? ? ? ? pytest-ordering (用于改變測(cè)試用例的執(zhí)行順序)
? ? ? ? ? ? ? ? pytest-rerunfailures (用例失敗后重跑)
? ? ? ? ? ? ? ? allure-pytest (用于生成美觀的測(cè)試報(bào)告)
? ? ? ? 在這里我們可以一次性將這些插件一起導(dǎo)入項(xiàng)目中,具體操作如下:
? ? ? ? 輸入pytest -version 即可查看是否導(dǎo)入成功,如果顯示失敗,可以查看一下位置是否出現(xiàn)錯(cuò)誤
四、使用pytest默認(rèn)的測(cè)試用例規(guī)則及基礎(chǔ)應(yīng)用
? ? ? ? 1. 模塊名必須以test_開頭或_test結(jié)尾
? ? ? ? 2. 測(cè)試類必須以Test開頭,并且不能有init方法
? ? ? ? 3. 測(cè)試方法必須以test開頭
五、pytest測(cè)試用例的運(yùn)行方式
(1)主函數(shù)模式
? ? ? ? 1. 運(yùn)行所有:pytest.main()
if __name__ == '__main__':
? ? pytest.main()
? ? ? ? 2. 指定模塊:
if __name__ == '__main__':
? ? pytest.main(['-vs','test_id.py'])
ps:我用的是mac系統(tǒng),在寫.py文件的路徑時(shí)是這樣寫的,如果是win,注意一下文件路徑即可。
? ? ? ? 3. 指定目錄
if __name__ == '__main__':
# 測(cè)試interface_testcase 目錄下的全部用例
? ? pytest.main(['-vs','interface_testcase'])
?
?
if __name__ == '__main__':
# 測(cè)試interface_testcase 目錄下的test_interface01.py 中的用例
? ? pytest.main(['-vs','interface_testcase/test_interface01.py'])
(2)命令行模式
? ? ? ? 1. 運(yùn)行所有:pytest
? ? ? ? 2. 指定模塊:pytest test_goods.py
? ? ? ? 3. 指定目錄:pytest testcase
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pytest testcase/test_goods.py
pytest中的參數(shù)詳解:
? ? ? ? -s:表示輸出調(diào)試信息,輸出print信息
? ? ? ? -v:顯示更加詳細(xì)的信息
? ? ? ? -vs:兩個(gè)參數(shù)可以同時(shí)使用
? ? ? ? -n:支持多線程或者分布式運(yùn)行
if __name__ == '__main__':
# 有兩個(gè)線程來執(zhí)行testcase 目錄下的測(cè)試用例
? ? pytest.main(['-vs','testcase','-n 2'])
? ? ? ? --reruns=NUM:失敗用例重跑,將這個(gè)模塊多執(zhí)行num次,最后返回結(jié)果
if __name__ == '__main__':
? ? pytest.main(['-vs','--reruns=2'])
? ? ? ? -x:只要有一個(gè)用例失敗,測(cè)試就會(huì)停止
? ? ? ? -maxfail=2:如果有兩個(gè)測(cè)試用例失敗,測(cè)試停止
? ? ? ? -k:根據(jù)測(cè)試用例的部分字符串指定測(cè)試用例
if __name__ == '__main__':
# 在testcase 目錄下,帶有case的測(cè)試用例全部被執(zhí)行
? ? pytest.main(['-vs','testcase','-k "case"'])
(3)通過讀取pytest.ini配置文件運(yùn)行
? ? ? ? pytest.ini是pytest單元測(cè)試框架的核心配置文件
? ? ? ? 1. 位置:一般放在項(xiàng)目的根目錄
? ? ? ? 2. 編碼:必須是ANSI,可以使用notepad++修改編碼格式
? ? ? ? 3. 作用:改變pytest默認(rèn)的行為
? ? ? ? 4. 運(yùn)行規(guī)則:不管是主函數(shù)的模式運(yùn)行還是命令行模式運(yùn)行,都會(huì)讀取這個(gè)文件
? ? ? ? pytest.ini配置文件的示例如下:
六、pytest執(zhí)行測(cè)試用例的順序
? ? ? ? unittest:根據(jù)ASCII大小執(zhí)行
? ? ? ? pytest:默認(rèn)按照用例先后順序執(zhí)行
? ? ? ? ? ? ? ? 可以通過@pytest.mark.run(order=)來指定執(zhí)行順序
七、如何分組執(zhí)行
if __name__ == '__main__':
# 只執(zhí)行smoke
? ? pytest.main(['-vs','-m "smoke"'])
?
if __name__ == '__main__':
# 執(zhí)行smoke或者usermanage
? ? pytest.main(['-vs','-m "smoke or usermanage"'])
八、pytest跳過用例
(1)無條件跳過
@pytest.mark.skip(reason="")
(2)有條件跳過
class TestStudent:
? ? age = 20;
?
? ? @pytest.mark.skipif(age>18, reason="age is over 18")
? ? def test_01(self):
? ? ? ? print('age is 20')
九、pytest框架實(shí)現(xiàn)的一些前后置(固件、夾具)處理
(1)setup/teardown,setup_class/teardown_class
? ? ? ? 為什么需要這些功能?
? ? ? ? ? ? ? ? 比如:web自動(dòng)化執(zhí)行用例之前都需要打開瀏覽器,在用例執(zhí)行完畢后都要關(guān)閉瀏覽器。如果每一次都重新寫打開、關(guān)閉瀏覽器的代碼會(huì)造成代碼冗余。
? ? ? ? setup/teardown 示例如下:這種方式的前后置會(huì)在每一個(gè)測(cè)試用例前后都執(zhí)行一次
class TestA:
? ? def setup(self):
? ? ? ? print("\n open the client")
?
? ? def test_01(self):
? ? ? ? print('this is a test')
?
? ? def teardown(self):
? ? ? ? print("\n close the client")
?
if __name__ == '__main__':
? ? pytest.main()
? ? ? ? setup_class/teardown_class 示例如下:這種方式的前后置會(huì)在每個(gè)類前后執(zhí)行一次
class TestA:
? ? def setup_class(self):
? ? ? ? print("\n open the client")
?
? ? def test_01(self):
? ? ? ? print('this is a test')
?
? ? def teardown_class(self):
? ? ? ? print("\n close the client")
?
if __name__ == '__main__':
? ? pytest.main()
(2)使用@pytest.fixture裝飾器實(shí)現(xiàn)部分用例的前后置
? ? ? ? @pytest.fixture裝飾器的幾個(gè)參數(shù):
? ? ? ? ? ? ? ? scope:表示被裝飾器標(biāo)記的方法的作用域(function默認(rèn),class,module,package,session)
? ? ? ? ? ? ? ? params:參數(shù)化(支持list[],tuple(),[(),()],([],[]))
? ? ? ? ? ? ? ? autouse=True:自動(dòng)執(zhí)行,默認(rèn)是false
? ? ? ? ? ? ? ? ids:當(dāng)使用params參數(shù)時(shí),給每一個(gè)參數(shù)設(shè)置一個(gè)變量名
? ? ? ? ? ? ? ? name:標(biāo)記方法的別名
? ? ? ? 只實(shí)現(xiàn)前置的示例:
@pytest.fixture(scope="function")
# 這個(gè)my_fixture()只能被當(dāng)前.py文件中的函數(shù)使用
def my_fixture():
? ? print("實(shí)現(xiàn)前置")
?
class TestA:
?
? ? def test_01(self,my_fixture):
? ? ? ? print('this is a test')
?
if __name__ == '__main__':
? ? pytest.main()
? ? ? ? 既有前置又有后置的示例:
@pytest.fixture(scope="function")
def my_fixture():
? ? print("實(shí)現(xiàn)前置")
? ? yield
? ? print("實(shí)現(xiàn)后置")
?
class TestA:
?
? ? def test_01(self,my_fixture):
? ? ? ? print('this is a test')
?
if __name__ == '__main__':
? ? pytest.main()
(3)通過conftest.py和@pytest.fixture()結(jié)合實(shí)現(xiàn)全局前置
? ? ? ? 1. conftest.py文件是一個(gè)單獨(dú)存放的夾具配置文件,名稱不能更改
? ? ? ? 2. 可以在不同的.py文件中使用同一個(gè)my_fixture函數(shù)
? ? ? ? 3. conftest.py需要和運(yùn)行的測(cè)試用例放在同一級(jí)目錄下
十、YAML文件實(shí)現(xiàn)接口自動(dòng)化
? ? ? ? 接口自動(dòng)化測(cè)試小示例:(本次使用的接口說B站上的軟件測(cè)試課程網(wǎng)站)
class TestApi:
? ??
? ? def test_02(self):
# 如果想用我這個(gè)示例來做實(shí)驗(yàn)的話,需要將這個(gè)網(wǎng)頁先打開
# https://www.bilibili.com/video/BV1Wf4y147en?p=9&spm_id_from=pageDriver
?
? ? ? ? url = 'https://www.bilibili.com/video/BV1Wf4y147en'
? ? ? ? params = {
? ? ? ? ? ? "p" :"9",
? ? ? ? ? ? "spm_id_from":"pageDriver"
? ? ? ? }
# 這里要導(dǎo)入一個(gè)requests庫
? ? ? ? res = requests.get(url,params=params)
? ? ? ? print(res.text)
?
if __name__ == '__main__':
? ? pytest.main()
? ? ? ? 將上面的測(cè)試用例轉(zhuǎn)換成讀取YAML文件形式:
? ? ? ? ? ? ? ? 先寫一個(gè)YAML文件,如下:
? ? ? ? ? ? ? ? ? ? ? ? 注意!冒號(hào)的后面一定要有空格?。。∵@是yaml文件的格式
? ? ? ? 然后編譯讀取YAML文件的工具類
class YamlUtil:
? ? def __init__(self,yaml_file):
? ? ? ? self.yaml_file = yaml_file
?
? ? def read_yaml(self):
? ? ? ? with open(self.yaml_file,encoding='utf-8') as f:
? ? ? ? ? ? # 反序列化
? ? ? ? ? ? value = yaml.load(f,Loader=yaml.FullLoader)
? ? ? ? ? ? print(value)
? ? ? ? ? ? return value
? ? ? ? 最后進(jìn)行測(cè)試
class TestApi:
? ? @pytest.mark.parametrize('args', YamlUtil('testcase/test_api.yaml').read_yaml())
? ? def test_01_baili(self, args):
? ? ? ? print(args)
? ? ? ? url = args['request']['url']
? ? ? ? params = args['request']['params']
? ? ? ? res = requests.get(url, params=params)
? ? ? ? print(res.text)
?
if __name__ == '__main__':
? ? pytest.main()
結(jié)語
感謝每一個(gè)認(rèn)真閱讀我文章的人?。?!
如果下面這些資料用得到的話可以直接拿走:
1、自學(xué)開發(fā)或者測(cè)試必備的完整項(xiàng)目源碼與環(huán)境
2、測(cè)試工作中所有模板(測(cè)試計(jì)劃、測(cè)試用例、測(cè)試報(bào)告等)
3、軟件測(cè)試經(jīng)典面試題
4、Python/Java自動(dòng)化測(cè)試實(shí)戰(zhàn).pdf
5、Jmeter/postman接口測(cè)試全套視頻獲取
6、Python學(xué)習(xí)路線圖
重點(diǎn):配套學(xué)習(xí)資料和視頻教學(xué)文章來源:http://www.zghlxwxcb.cn/news/detail-410997.html
?那么在這里我也精心準(zhǔn)備了上述大綱的詳細(xì)資料包含:電子書,簡(jiǎn)歷模板,各種工作模板,面試寶典,自學(xué)項(xiàng)目等。需要的朋友和我在評(píng)論區(qū)互動(dòng)交流吧!文章來源地址http://www.zghlxwxcb.cn/news/detail-410997.html
到了這里,關(guān)于【碼尚教育】Python自動(dòng)化測(cè)試框架pytest—入門學(xué)習(xí)筆記的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!