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