偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Pytest斷言的使用:驗證執(zhí)行結(jié)果是否正確

開發(fā) 測試
在 pytest 中,斷言(assertion)是測試用例中最核心的部分之一。斷言用于驗證函數(shù)或方法的執(zhí)行結(jié)果是否符合預(yù)期。正確的斷言不僅能夠幫助我們發(fā)現(xiàn)錯誤,還能提供詳細(xì)的錯誤信息,便于快速定位問題。

前言

在 pytest 中,斷言(assertion)是測試用例中最核心的部分之一。斷言用于驗證函數(shù)或方法的執(zhí)行結(jié)果是否符合預(yù)期。正確的斷言不僅能夠幫助我們發(fā)現(xiàn)錯誤,還能提供詳細(xì)的錯誤信息,便于快速定位問題。

1. 基本斷言

1.1 示例代碼

假設(shè)我們有一個簡單的函數(shù) add,我們想要編寫一個測試用例來驗證這個函數(shù)的行為。

# 文件名:mylib.py

def add(a, b):
    return a + b

接下來,我們編寫一個測試用例來驗證 add 函數(shù)。

# 文件名:test_mylib.py

def test_add():
    assert add(1, 2) == 3
    assert add(-1, 1) == 0
    assert add(0, 0) == 0

2. 多重斷言

有時候我們需要在一個測試用例中驗證多個條件。可以使用多個 assert 語句來實現(xiàn)這一點。

2.1 示例代碼

# 文件名:test_mylib.py

def test_add_multiple_conditions():
    assert add(1, 2) == 3
    assert add(-1, 1) == 0
    assert add(0, 0) == 0
    assert add(100, 200) == 300

3. 斷言異常

有時我們需要驗證函數(shù)是否會拋出特定的異常??梢允褂?with pytest.raises() 上下文管理器來實現(xiàn)這一點。

3.1 示例代碼

假設(shè)我們有一個函數(shù) divide,它可能會拋出 ZeroDivisionError。

# 文件名:mylib.py

def divide(a, b):
    return a / b

接下來,我們編寫一個測試用例來驗證 divide 函數(shù)是否會拋出異常。

# 文件名:test_mylib.py

def test_divide_zero():
    with pytest.raises(ZeroDivisionError):
        divide(1, 0)

4. 斷言特定異常消息

有時我們需要驗證拋出的異常是否包含特定的消息。可以使用 match 參數(shù)來實現(xiàn)這一點。

4.1 示例代碼

# 文件名:test_mylib.py

def test_divide_zero_message():
    with pytest.raises(ZeroDivisionError, match="division by zero"):
        divide(1, 0)

5. 斷言列表和字典

在驗證數(shù)據(jù)結(jié)構(gòu)時,經(jīng)常需要斷言列表或字典是否符合預(yù)期。

5.1 示例代碼

假設(shè)我們有一個函數(shù) process_data,它返回一個列表。

# 文件名:mylib.py

def process_data():
    return [1, 2, 3]

接下來,我們編寫一個測試用例來驗證 process_data 函數(shù)的返回值。

# 文件名:test_mylib.py

def test_process_data():
    assert process_data() == [1, 2, 3]

5.2 斷言字典

假設(shè)我們有一個函數(shù) get_user_info,它返回一個字典。

# 文件名:mylib.py

def get_user_info():
    return {"name": "Alice", "age": 25}

接下來,我們編寫一個測試用例來驗證 get_user_info 函數(shù)的返回值。

# 文件名:test_mylib.py

def test_get_user_info():
    assert get_user_info() == {"name": "Alice", "age": 25}

6. 斷言浮點數(shù)

在處理浮點數(shù)時,由于浮點數(shù)的精度問題,直接使用 == 可能會導(dǎo)致誤判??梢允褂?pytest.approx 來實現(xiàn)這一點。

6.1 示例代碼

假設(shè)我們有一個函數(shù) calculate_pi,它返回一個近似值。

# 文件名:mylib.py

def calculate_pi():
    return 3.14159265358979323846

接下來,我們編寫一個測試用例來驗證 calculate_pi 函數(shù)的返回值。

# 文件名:test_mylib.py

def test_calculate_pi():
    assert calculate_pi() == pytest.approx(3.141592653589793, abs=1e-10)

7. 斷言字符串

在驗證字符串時,可以使用 assert 來檢查字符串是否包含特定的子串。

7.1 示例代碼

假設(shè)我們有一個函數(shù) greet,它返回一個問候字符串。

# 文件名:mylib.py

def greet(name):
    return f"Hello, {name}!"

接下來,我們編寫一個測試用例來驗證 greet 函數(shù)的返回值。

# 文件名:test_mylib.py

def test_greet():
    assert "Hello, Alice!" == greet("Alice")
    assert "Hello, Bob!" == greet("Bob")

8. 斷言元組

在驗證元組時,可以使用 assert 來檢查元組是否符合預(yù)期。

8.1 示例代碼

假設(shè)我們有一個函數(shù) get_coordinates,它返回一個坐標(biāo)元組。

# 文件名:mylib.py

def get_coordinates():
    return (1.0, 2.0)

接下來,我們編寫一個測試用例來驗證 get_coordinates 函數(shù)的返回值。

# 文件名:test_mylib.py

def test_get_coordinates():
    assert get_coordinates() == (1.0, 2.0)

9. 斷言列表和字典的子集

在驗證列表或字典的子集時,可以使用 Python 的內(nèi)置函數(shù) all 或 any 來實現(xiàn)這一點。

9.1 示例代碼

假設(shè)我們有一個函數(shù) get_students,它返回一個學(xué)生列表。

# 文件名:mylib.py

def get_students():
    return [{"name": "Alice", "age": 25}, {"name": "Bob", "age": 30}]

接下來,我們編寫一個測試用例來驗證 get_students 函數(shù)的返回值。

# 文件名:test_mylib.py

def test_get_students():
    students = get_students()
    assert all(student["age"] >= 18 for student in students)
    assert any(student["name"] == "Alice" for student in students)

10. 斷言日志

在驗證日志輸出時,可以使用 caplog 固定夾(fixture)來捕獲日志輸出。

10.1 示例代碼

假設(shè)我們有一個函數(shù) log_error,它記錄一條錯誤日志。

# 文件名:mylib.py

import logging
logger = logging.getLogger(__name__)
def log_error(message):
    logger.error(message)

接下來,我們編寫一個測試用例來驗證 log_error 函數(shù)的日志輸出。

# 文件名:test_mylib.py

import logging
import pytest
def test_log_error(caplog):
    caplog.set_level(logging.ERROR)
    log_error("An error occurred")
    assert "An error occurred" in caplog.text

11. 總結(jié)

通過以上示例,我們詳細(xì)介紹了 pytest 中常用的斷言方法,并通過具體的示例代碼展示了它們的使用方法:

基本斷言:驗證函數(shù)的返回值。

多重斷言:驗證多個條件。

斷言異常:驗證函數(shù)是否會拋出特定的異常。

斷言特定異常消息:驗證拋出的異常是否包含特定的消息。

斷言列表和字典:驗證數(shù)據(jù)結(jié)構(gòu)是否符合預(yù)期。

斷言浮點數(shù):驗證浮點數(shù)是否近似相等。

斷言字符串:驗證字符串是否包含特定的子串。

斷言元組:驗證元組是否符合預(yù)期。

斷言列表和字典的子集:驗證列表或字典的子集。

斷言日志:驗證日志輸出。

責(zé)任編輯:華軒 來源: 測試開發(fā)學(xué)習(xí)交流
相關(guān)推薦

2023-10-26 07:54:27

JCStress工具

2017-07-11 09:36:38

機器學(xué)習(xí)算法結(jié)果

2014-04-30 15:37:05

2010-07-27 15:33:00

DB2數(shù)據(jù)庫備份

2022-06-06 06:10:00

密碼驗證安全

2021-07-28 07:22:40

SQL順序Hive

2009-02-20 11:03:25

Vista特點

2017-02-23 15:37:44

OptionObject容器

2010-05-06 09:52:11

Oracle發(fā)送郵件

2022-01-17 14:25:14

索引數(shù)據(jù)庫搜索

2017-07-10 13:09:45

前端Flexbox

2018-04-12 08:37:27

2010-02-03 15:40:37

Python函數(shù)

2019-11-14 16:23:07

MySQL索引數(shù)據(jù)庫

2014-03-03 10:10:37

PostgreSQL數(shù)組

2024-02-22 14:54:44

pytestreporting測試

2022-02-11 08:54:33

SpringWeb 應(yīng)用執(zhí)行表單

2010-07-06 09:33:07

SQL Server遠(yuǎn)

2010-07-22 11:09:33

SQL Server內(nèi)

2010-07-14 14:07:50

SQL Server
點贊
收藏

51CTO技術(shù)棧公眾號