使用 pytest 進行功能測試

Python 測試入門

Alexander Levin

Data Scientist

什麼是功能測試

Feature

  • 軟體系統的某項功能。
  • 滿足特定使用者需求。

Features

  • 範圍大於 units
  • 終端使用者可直接使用。

Feature testing

  • 一種軟體測試方法。
  • 用來驗證特定功能的行為。
  • 範例
    • 資料分佈檢查
    • 報表產製
Python 測試入門

Units 與 features:個人電腦

Units:

  • 每一個按鈕
  • 螢幕上的像素
  • LED 發光二極體
  • 磁碟上的區塊

Features

  • 鍵盤
  • 螢幕
  • 照明
  • 檔案系統
Python 測試入門

為什麼要做功能測試

功能測試可協助

  • 在使用者與系統互動的層級進行測試。

範圍比 unit 更廣:

  • 有單一 unit 壞了,並不代表整個功能不行。

反之亦然:

  • 所有 units 看似正常,並不代表功能就沒問題。
Python 測試入門

功能測試範例:設定

設定並定義功能程式碼:

# Setup
import pandas as pd
import pytest

df = pd.read_csv('laptops.csv')

# Filter feature
def filter_data_by_manuf(df, manufacturer_name):
    filtered_df = df\
        [df["Manufacturer"] == manufacturer_name]
    return filtered_df
Python 測試入門

功能測試範例:測試

測試程式碼:

# Feature test function
def test_unique():
    manuf_name = 'Apple'
    filtered = filter_data_by_manuf(df, manuf_name)
    assert filtered\
        ['Manufacturer'].nunique() == 1
    assert filtered\
        ['Manufacturer'].unique() == [manuf_name]
Python 測試入門

重點整理

功能測試:一種軟體測試方法,用於驗證特定功能的行為。

Features 的範圍大於 units

  • 若按鈕是 unit,則鍵盤是 feature。

功能測試 有助於確保使用者獲得預期結果。

要撰寫功能測試,需要設計測試案例。

設計功能測試的關鍵:先理解該系統有哪些功能。

Python 測試入門

一起來練習吧!

Python 測試入門

Preparing Video For Download...