[dechin@dechin-manjaro pytest]$ pytest --versionpytest 6.2.2pytest單元測試用例撰寫根據(jù)前面一個章節(jié)中的random_number.py文件,我們可以對照的寫一個簡單測試用例:
# test_random_number.pyimport pytestfrom random_number import random_number_generator as rngdef test_random_number_generator():for i in range(10):random_number = rng()assert random_number == 0 or random_number == 1該測試用例的含義為:導(dǎo)入rng函數(shù)之后,測試10次該函數(shù)的返回值,所返回的值必須是0或者1的隨機數(shù),如果輸出了這兩個數(shù)字以外的返回結(jié)果,那么說明這個隨機數(shù)產(chǎn)生器功能上存在問題 ?;趐ytest的測試代碼可以通過如下的指令來運行:
[dechin@dechin-20n2s01200 pytest]$ py.test=========================================== test session starts ============================================platform linux -- Python 3.8.5, pytest-6.2.2, py-1.9.0, pluggy-0.13.1rootdir: /home/dechin/projects/2021-python/pytestplugins: cov-2.11.1, metadata-1.11.0, html-3.1.1collected 1 itemtest_random_number.py .[100%]============================================= warnings summary =============================================../../../anaconda3/lib/python3.8/site-packages/projectq/ops/_gates.py:118/home/dechin/anaconda3/lib/python3.8/site-packages/projectq/ops/_gates.py:118: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal with linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray.return np.matrix([[1, 0], [0, 1j]])../../../anaconda3/lib/python3.8/site-packages/projectq/ops/_gates.py:133/home/dechin/anaconda3/lib/python3.8/site-packages/projectq/ops/_gates.py:133: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal with linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray.return np.matrix([[1, 0], [0, cmath.exp(1j * cmath.pi / 4)]])test_random_number.py: 40 warnings/home/dechin/anaconda3/lib/python3.8/site-packages/projectq/ops/_gates.py:69: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal with linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray.return 1. / cmath.sqrt(2.) * np.matrix([[1, 1], [1, -1]])-- Docs: https://docs.pytest.org/en/stable/warnings.html====================================== 1 passed, 42 warnings in 0.50s ======================================從返回的結(jié)果來看,出現(xiàn)了1 passed而沒有failed,說明所有的測試用例都已經(jīng)執(zhí)行成功了,但是這里存在不少的告警warnings信息 。
pytest初始化配置文件在上一節(jié)的測試結(jié)果中,我們發(fā)現(xiàn)有非常多的測試告警 。假如我們確認這些告警信息可以忽略,那么我們可以通過在指令中配置忽略告警信息,或者直接使用這里介紹的pytest.ini來忽略相應(yīng)的告警信息:
# pytest.ini[pytest]filterwarnings =ignore::PendingDeprecationWarning在當前目錄下的ini配置文件中,我們添加了PendingDeprecationWarning作為忽略項,然后我們再回頭看一下上述用例的測試結(jié)果:
[dechin@dechin-manjaro pytest]$ py.test=========================================== test session starts ============================================platform linux -- Python 3.8.5, pytest-6.2.2, py-1.9.0, pluggy-0.13.1rootdir: /home/dechin/projects/2021-python/pytest, configfile: pytest.iniplugins: cov-2.11.1, metadata-1.11.0, html-3.1.1collected 1 itemtest_random_number.py .[100%]============================================ 1 passed in 0.50s =============================================這里返回的結(jié)果中就沒有告警信息了 。
pytest生成html格式報告為了更好的展現(xiàn)測試的結(jié)果,這里我們需要先安裝一個組件pytest-html:
以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請您及時就醫(yī)或請專業(yè)人士給予相關(guān)指導(dǎo)!
「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對您有所幫助:- js實現(xiàn)文件下載功能 html圖片按鈕代碼
- 自己制作一個網(wǎng)站的方法 html5制作網(wǎng)頁教程
- 浪漫的html表白源代碼 網(wǎng)頁表白代碼大全
- html頁面獲取url參數(shù) jquery獲取url中的參數(shù)有哪些
- 講解html中title標簽的作用 html標題代碼
- 網(wǎng)頁設(shè)計菜單欄導(dǎo)航實例 html5導(dǎo)航欄代碼
- 分享文件轉(zhuǎn)換工具類 html轉(zhuǎn)換成word文檔
- html左圖右文布局方法 html左右布局模板
- c語言二維數(shù)組定義和賦值 隨機數(shù)c語言代碼
- html簡易計算器代碼 javascript計算器代碼
