....
舉個栗子:
1、建立乙個test_sample.py檔案
2、**如下:
def inc(x):
return x + 1
def test_answer1():
assert inc(3) == 4
def test_answer2():
assert inc(3) == 5
3、執行測試用例
(1)在cmd或者控制台terminal執行:
切換到test_sample.py所在目錄,執行『pytest』命令
(2)通過main方法執行測試用例
4、命名規則:
測試檔案以test_開頭(以_test結尾也可以)
測試類以test開頭,並且不能帶有 init 方法
測試函式以test_開頭
5、總結:
pytest是乙個第三方的測試框架,相比unittest,更加的簡單靈活,主要表現在以下幾點:
1、不必像unittest一樣,必須建立測試類;
2、pytest並沒有提供專門的斷言方法,而是直接使用python的assert斷言,比unittest自帶的斷言方法簡單;
Python單元測試框架 pytest
一 介紹 pytest是乙個非常成熟的全功能的python測試框架,主要特點有以下幾點 1 簡單靈活,容易上手 2 支援引數化 4 pytest具有很多第三方外掛程式,並且可以自定義擴充套件,比較好用的如pytest selenium 整合selenium pytest html 完美html測試報...
python 的單元測試框架pytest 其一
1,最簡單的測試 新建的檔案要以 test py或 test.py 的命名形式!content of test sample.py deffunc x return x 1 deftest answer assert func 3 5 執行測試pytest將在當前目錄及其子目錄中執行test py或...
單元測試之pytest
前提 需要安裝pytest和pytest html 生成html測試報告 pip install pytest 和 pip install pytest htmlpytest單元測試中的類名和方法名必須是以test開頭,執行中只能找到test開頭的類和方法,比unittest更加嚴謹unittest...