ตัวอย่างเชิงปฏิบัติ

Python เบื้องต้นสำหรับการทดสอบโค้ด

Alexander Levin

Data Scientist

ข้อมูลและ Pipeline

ข้อมูล: เงินเดือนในสายงาน Data Science

แต่ละแถวเก็บข้อมูลของพนักงาน Data Science พร้อมเงินเดือน ตำแหน่ง และคุณสมบัติอื่น ๆ

ตาราง ds salaries

Pipeline: คำนวณเงินเดือนเฉลี่ย

  1. อ่านข้อมูล
  2. กรองตามประเภทการจ้างงาน
  3. คำนวณเงินเดือนเฉลี่ย
  4. บันทึกผลลัพธ์
Python เบื้องต้นสำหรับการทดสอบโค้ด

โค้ดของ Pipeline

import pandas as pd

# Fixture to get the data
@pytest.fixture
def read_df():
    return pd.read_csv('ds_salaries.csv')
# Function to filter the data
def filter_df(df):
    return df[df['employment_type'] == 'FT']
# Function to get the mean
def get_mean(df):   
    return df['salary_in_usd'].mean()
Python เบื้องต้นสำหรับการทดสอบโค้ด

Integration Tests

กรณีทดสอบ:

  • การอ่านข้อมูล
  • การเขียนลงไฟล์

โค้ด:

def test_read_df(read_df):
    # Check the type of the dataframe
    assert isinstance(read_df, pd.DataFrame)
    # Check that df contains rows
    assert read_df.shape[0] > 0
Python เบื้องต้นสำหรับการทดสอบโค้ด

Integration Tests

ตัวอย่างการตรวจสอบว่า Python สร้างไฟล์ได้

def test_write():
    # Opening a file in writing mode
    with open('temp.txt', 'w') as wfile:
        # Writing the text to the file
        wfile.write('Testing stuff is awesome')
    # Checking the file exists
    assert os.path.exists('temp.txt')
    # Don't forget to clean after yourself
    os.remove('temp.txt')
Python เบื้องต้นสำหรับการทดสอบโค้ด

Unit Tests

กรณีทดสอบ:

  • ชุดข้อมูลที่กรองแล้วมีเฉพาะประเภทการจ้างงาน 'FT'
  • ฟังก์ชัน get_mean() คืนค่าเป็นตัวเลข

โค้ด:

def test_units(read_df):
    filtered = filter_df(read_df)
    assert filtered['employment_type'].unique() == ['FT']
    assert isinstance(get_mean(filtered), float)
Python เบื้องต้นสำหรับการทดสอบโค้ด

Feature Tests

กรณีทดสอบ:

  • ค่าเฉลี่ยมากกว่าศูนย์
  • ค่าเฉลี่ยไม่เกินเงินเดือนสูงสุดในชุดข้อมูล

โค้ด:

def test_feature(read_df):
    # Filtering the data
    filtered = filter_df(read_df)
    # Test case: mean is greater than zero
    assert get_mean(filtered) > 0
    # Test case: mean is not bigger than the maximum
    assert get_mean(filtered) <= read_df['salary_in_usd'].max()
Python เบื้องต้นสำหรับการทดสอบโค้ด

Performance Tests

กรณีทดสอบ:

  • เวลาทำงานของ Pipeline ตั้งแต่ต้นจนจบ

โค้ด:

def test_performance(benchmark, read_df):
    # Benchmark decorator
    @benchmark
    # Function to measure
    def get_result():
        filtered = filter_df(read_df)
        return get_mean(filtered)
Python เบื้องต้นสำหรับการทดสอบโค้ด

ชุดทดสอบขั้นสุดท้าย

import pytest

## Integration Tests
def test_read_df(read_df):
      # Check the type of the dataframe
    assert isinstance(read_df, pd.DataFrame)
    # Check that df contains rows
    assert read_df.shape[0] > 0
def test_write():
    with open('temp.txt', 'w') as wfile:
        wfile.write('12345')
    assert os.path.exists('temp.txt')
    os.remove('temp.txt')

## Unit Tests
def test_units(read_df):
    filtered = filter_df(read_df)
    assert filtered['employment_type'].unique() == ['FT']
    assert isinstance(get_mean(filtered), float)
## Feature Tests
def test_feature(read_df):
    # Filtering the data
    filtered = filter_df(read_df)
    # Test case: mean is greater than zero
    assert get_mean(filtered) > 0
    # Test case: mean is not bigger than the maximum
    assert get_mean(filtered) <= read_df['salary_in_usd'].max()

## Performance Tests
def test_performance(benchmark, read_df):
    # Benchmark decorator
    @benchmark
    # Function to measure
    def pipeline():
        filtered = filter_df(read_df)
        return get_mean(filtered)
Python เบื้องต้นสำหรับการทดสอบโค้ด

มาฝึกกันเถอะ!

Python เบื้องต้นสำหรับการทดสอบโค้ด

Preparing Video For Download...