python制作考试系统_Python系统学习 - Pytest单元测试框架
哈咯,大家,我們今天來學(xué)習(xí)pytest單元測(cè)試框架,這個(gè)框架要比unittest更加易于使用,結(jié)構(gòu)性也更加好,希望大家能夠從中學(xué)習(xí)到有用的東西,然后在下一章,我們來使用pytest單元測(cè)試框架來搭建一個(gè)web自動(dòng)化測(cè)試項(xiàng)目,一起加油!
直接上整體學(xué)習(xí)項(xiàng)目結(jié)構(gòu)圖:pytest項(xiàng)目結(jié)構(gòu)
pytest1.安裝pytest:pip3 install pytest
2.pytest框架下的,測(cè)試文件和測(cè)試函數(shù)都要以test開頭
3.在命令行輸入,pytest,即會(huì)執(zhí)行test開頭的文件
4.pytest.main()也同樣可以開始執(zhí)行 - pytest會(huì)在當(dāng)前命令行所在目錄下全局搜索test開頭的文件進(jìn)行執(zhí)行測(cè)試
這里我們主要針對(duì)unittest 和 pytest的不同之處進(jìn)行學(xué)習(xí)
一、斷言:
pytest并沒有提供專門的斷言方法,而是直接使用assert進(jìn)行斷言,如:
assert a == b
assert a in b
assert abc() is True
assert not abc()
assert abc() is False
二、Fixture:
是對(duì)測(cè)試方法、測(cè)試函數(shù)、測(cè)試類和整個(gè)測(cè)試文件進(jìn)行初始化或還原測(cè)試環(huán)境
例:
# 裝飾器從session -> module -> class -> function
(都是針對(duì)每個(gè)里面包含的用例去執(zhí)行,覆蓋單位是用例)
session:多個(gè)文件調(diào)用一次,可以跨.py文件調(diào)用,每個(gè).py是一個(gè)module
module:每個(gè).py文件調(diào)用一次,每個(gè).py是一個(gè)module
class:每個(gè)類調(diào)用一次
function:每個(gè)函數(shù)或方法調(diào)用一次
# 如果你想一個(gè)module下的都用上,那就打開改成True, 如下,這樣就不需要往每個(gè)函數(shù)里傳入fixture
# yield xxx接著后續(xù)的代碼實(shí)現(xiàn)tearDown的功能
# 如果是把fixture單獨(dú)存放,定義一個(gè)conftest.py, 該文件要和case在同級(jí)目錄下,case文件中聲明# content of conftest.py
conftest.py:
# coding = utf8
import os
os.path.abspath(".")
from time import sleep
import pytest
"""
a py file which saved pytest's fixture for use
"""
@pytest.fixture(scope = "function")
def before_case_execute():
sleep(3)
yield before_case_execute
sleep(3)
test_sample.py:
# coding = utf8
# content of conftest.py
import os
os.path.abspath(".")
import pytest
from time import sleep
# 裝飾器從session -> module -> class -> function
# 如果你想一個(gè)module下的都用上,那就打開改成True, 如下,這樣就不需要往每個(gè)函數(shù)里傳入fixture
# yield實(shí)現(xiàn)tearDown的功能
@pytest.fixture(scope = "function")
def before_each_case():
sleep(3)
print("Set up now")
yield before_each_case
sleep(3)
print("Tear down now")
def add(x):
return x + 1
# def test_add(before_each_case):
def test_add(before_case_execute):
assert add(3) == 4
if __name__ == "__main__":
pytest.main()三、參數(shù)化
pytest的參數(shù)化與unittest類似,通過@pytest.mark.parameterize進(jìn)行裝飾,傳入需要的值名、值、以及為每一個(gè)值指定對(duì)應(yīng)的case名
test_parameterize.py:
# coding = utf8
import os
os.path.abspath(".")
# pytest參數(shù)化
import pytest
@pytest.mark.parametrize(
"x, y, z",
[(1, 2, 9), (4, 5, 9), (7, 8, 5)],
ids = ["case1", "case2", "case3"]
)
def test_add(x, y, z):
assert x + y == z
# terminal進(jìn)入當(dāng)前py文件路徑,pytest -v test_parameterize.py指定執(zhí)行
# if __name__ == "__main__":
# pytest.main(["-v", "-k add", "./test_parameterize.py"])四、運(yùn)行測(cè)試
1.指定名稱中包含某字符串的用例執(zhí)行:pytest -k add test_parameterize.py
2.減少運(yùn)行的冗長(zhǎng)日志信息:pytest -q test_parameterize.py
3.如果出現(xiàn)一條測(cè)試用例失敗則退出測(cè)試:pytest -x test_parameterize.py
4.運(yùn)行測(cè)試目錄:pytest ./pytest_object
5.指定特定類或方法執(zhí)行:pytest test_parameterize.py::test_add 用::進(jìn)行分隔
6.通過main()運(yùn)行測(cè)試:pytest.main(["-v", "-k add", "./test_parameterize.py"]) 每個(gè)參數(shù)用逗號(hào)隔開
五、生成測(cè)試報(bào)告
1.生成JUnit XML文件:pytest ./pytest_project --junit-xml=./pytest_project/report/pytest_log.xml
2.生成在線測(cè)試報(bào)告:pytest test_parameterize.py --pastebin=all 執(zhí)行到最后會(huì)生成一個(gè)鏈接,如下:View paste B3CA?bpaste.net
六、conftest配置文件
(conftest.py 是pytest特有到本地測(cè)試配置文件:
1.可以用來設(shè)置項(xiàng)目級(jí)別的Fixture
2.導(dǎo)入外部插件
3.指定鉤子函數(shù))
pytest擴(kuò)展1.生成HTML格式的測(cè)試報(bào)告:
pip3 install pytest-html
pytest ./pytest_project --html=./pytest_project/report/pytest_html_result.html
2.在用例失敗時(shí)重試:
pip3 install pytest-rerunfailures
pytest -v pytest_expand.py --reruns 3
(重試機(jī)制可以增加測(cè)試用例的穩(wěn)定性,避免因?yàn)橐恍┚W(wǎng)絡(luò)原因、機(jī)器卡頓導(dǎo)致的問題)
3.pytest-parallel擴(kuò)展:
pip3 install pytest-parallel
pytest -q pytest_expand.py --tests-per-worker auto
(使用并行運(yùn)行測(cè)試case測(cè)試可能會(huì)導(dǎo)致互相產(chǎn)生干擾,需要謹(jǐn)慎使用)
# coding = utf8
import os
os.path.abspath(".")
import pytest
"""
pytest擴(kuò)展
"""
# pytest ./pytest_project --html=./pytest_project/report/pytest_html_result.html
def test_add():
assert 1 + 1 == 3
關(guān)注 + 收藏 + 點(diǎn)贊哦,謝謝啦~
總結(jié)
以上是生活随笔為你收集整理的python制作考试系统_Python系统学习 - Pytest单元测试框架的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 群晖 root_最新群晖DSM7.0降级
- 下一篇: keras卷积处理rgb输入_CNN卷积