使用 pytest 進行整合測試

Python 測試入門

Alexander Levin

Data Scientist

什麼是整合測試?

  • 整合測試:一種軟體測試方法,用來驗證互動是否如常運作。

  • 整合:系統內 2 個以上模組之間的互動。

diagram of integration example

Python 測試入門

實務專案中的整合

範例:

  • 電源線
  • 網際網路連線
  • 檔案讀取驅動程式
  • 資料庫連線
  • Application Programming Interface (API) 整合
Python 測試入門

可能出錯的地方

可能的整合問題:

  • 連線中斷
  • 資料遺失
  • 互動延遲
  • 頻寬不足
  • 版本衝突
  • 介面不相容
  • 其他
Python 測試入門

整合測試範例

import pytest, os

@pytest.fixture
def setup_file():
    # Create temporary file
    file = "test_file.txt"
    with open(file, "w") as f1:
        f1.write("Test data 1")
    yield file
    os.remove(file)

def test_fs(setup_file):
    file = setup_file
    # Check that the file was created successfully
    assert os.path.exists(file)
Python 測試入門

重點整理

  • 整合測試:軟體測試方法;用來驗證整合是否如預期運作。

  • 實務專案通常包含許多不同的整合。

  • 整合測試能預防許多潛在問題。

  • 範例:檢查 Python 與檔案系統之間的整合。

Python 測試入門

一起來練習吧!

Python 測試入門

Preparing Video For Download...