1. 程式人生 > 其它 >Pytest學習(一)

Pytest學習(一)

最近看了許多Pytest的文件,發現有些已經是很早以前的版本了。沿著學習的道路,我記錄下我學習Pytest的過程

安裝

pip install pytest

安裝很簡單,如果第一次安裝用上述pip安裝,如果已經安裝了使用

pip install -U pytest

-U引數具有更新作用

我的使用環境

platform win32 -- Python 3.8.10, pytest-6.2.4, py-1.10.0, pluggy-0.13.1

建立第一個測試

用四行程式碼建立一個簡單的測試函式:

# content of test_sample.py
def func(x):
    return
x + 1 def test_answer(): assert func(3) == 5

執行

$ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 1 item

test_sample.py F                                                     [
100%] ================================= FAILURES ================================= _______________________________ test_answer ________________________________ def test_answer(): > assert func(3) == 5 E assert 4 == 5 E + where 4 = func(3) test_sample.py:6: AssertionError
========================= short test summary info ========================== FAILED test_sample.py::test_answer - assert 4 == 5 ============================ 1 failed in 0.12s =============================

這裡是在cmd中執行的,也可以在pycharm的Terminal執行(如果你安裝了虛擬環境)

這個[100%]指執行所有測試用例的總體進度。完成後,pytest會顯示一個失敗報告,因為func(3)不返5

pytest可以搜尋指定目錄下包括子目錄的test_*.py和*_test.py指令碼。注意:這裡是“.py”檔案

斷言

pytest允許您使用標準的pythonassert用於驗證Python測試中的期望和值

# content of test_assert1.py
def f():
    return 3


def test_function():
    assert f() == 4

斷言函式返回某個值。如果此斷言失敗,您將看到函式呼叫的返回值:

$ pytest test_assert1.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 1 item

test_assert1.py F                                                    [100%]

================================= FAILURES =================================
______________________________ test_function _______________________________

    def test_function():
>       assert f() == 4
E       assert 3 == 4
E        +  where 3 = f()

test_assert1.py:6: AssertionError
========================= short test summary info ==========================
FAILED test_assert1.py::test_function - assert 3 == 4
============================ 1 failed in 0.12s =============================

pytest支援顯示最常見的子表示式的值,包括呼叫、屬性、比較以及二進位制和一元運算子。

def test_ne():
    a = 7
    assert a % 2 == 0, "value was odd, should be even"

pytest執行結果

==================================================================================== FAILURES =====================================================================================
_____________________________________________________________________________________ test_ne _____________________________________________________________________________________

    def test_ne():
        a = 7
>       assert a % 2 == 0, "value was odd, should be even"
E       AssertionError: value was odd, should be even
E       assert (7 % 2) == 0

2_test.py:10: AssertionError

如圖你只是知道7%2==0失敗,但7%2的值你不知道。

改為:

def test_ne():
    a = 7
    value = a % 2
    assert value == 0, "value was odd, should be even"

這樣會得出:

_____________________________________________________________________________________ test_ne _____________________________________________________________________________________

    def test_ne():
        a = 7
        value = a % 2
>       assert value == 0, "value was odd, should be even"
E       AssertionError: value was odd, should be even
E       assert 1 == 0

2_test.py:11: AssertionError

這樣就會知道value的值,會幫助我們發現問題

關於預期異常的斷言

import pytest


def test_zero_division():
    with pytest.raises(ZeroDivisionError):
        1 / 0

如果您需要訪問實際的異常資訊,可以使用:

def test_recursion_depth():
    with pytest.raises(RuntimeError) as excinfo:

        def f():
            f()

        f()
    assert "maximum recursion" in str(excinfo.value)
以前-好記性不如爛筆頭 現在-好記性不如爛鍵盤