Ví dụ thực tiễn

Nhập môn Kiểm thử trong Python

Alexander Levin

Data Scientist

Dữ liệu và pipeline

Dữ liệu: lương trong khoa học dữ liệu.

Mỗi hàng chứa thông tin về một nhân sự khoa học dữ liệu với lương, chức danh và thuộc tính khác.

bảng lương DS

Pipeline: tính lương trung bình:

  1. Đọc dữ liệu
  2. Lọc theo loại việc làm
  3. Tính trung bình lương
  4. Lưu kết quả
Nhập môn Kiểm thử trong Python

Mã của pipeline

import pandas as pd

# Fixture lấy dữ liệu
@pytest.fixture
def read_df():
    return pd.read_csv('ds_salaries.csv')
# Hàm lọc dữ liệu
def filter_df(df):
    return df[df['employment_type'] == 'FT']
# Hàm tính trung bình
def get_mean(df):   
    return df['salary_in_usd'].mean()
Nhập môn Kiểm thử trong Python

Integration test

Ca kiểm thử:

  • Đọc dữ liệu
  • Ghi tệp

Mã:

def test_read_df(read_df):
    # Kiểm tra kiểu của dataframe
    assert isinstance(read_df, pd.DataFrame)
    # Kiểm tra df có hàng
    assert read_df.shape[0] > 0
Nhập môn Kiểm thử trong Python

Integration test

Ví dụ kiểm tra Python có thể tạo tệp.

def test_write():
    # Mở tệp ở chế độ ghi
    with open('temp.txt', 'w') as wfile:
        # Ghi nội dung vào tệp
        wfile.write('Testing stuff is awesome')
    # Kiểm tra tệp tồn tại
    assert os.path.exists('temp.txt')
    # Đừng quên dọn dẹp
    os.remove('temp.txt')
Nhập môn Kiểm thử trong Python

Unit test

Ca kiểm thử:

  • Dữ liệu đã lọc chỉ chứa loại việc làm 'FT'
  • Hàm get_mean() trả về một số

Mã:

def test_units(read_df):
    filtered = filter_df(read_df)
    assert filtered['employment_type'].unique() == ['FT']
    assert isinstance(get_mean(filtered), float)
Nhập môn Kiểm thử trong Python

Feature test

Ca kiểm thử:

  • Giá trị trung bình lớn hơn 0
  • Giá trị trung bình không lớn hơn lương tối đa trong dữ liệu

Mã:

def test_feature(read_df):
    # Lọc dữ liệu
    filtered = filter_df(read_df)
    # Trung bình > 0
    assert get_mean(filtered) > 0
    # Trung bình ≤ lương tối đa
    assert get_mean(filtered) <= read_df['salary_in_usd'].max()
Nhập môn Kiểm thử trong Python

Performance test

Ca kiểm thử:

  • Thời gian chạy pipeline từ đầu đến cuối

Mã:

def test_performance(benchmark, read_df):
    # Trình trang bị benchmark
    @benchmark
    # Hàm cần đo
    def get_result():
        filtered = filter_df(read_df)
        return get_mean(filtered)
Nhập môn Kiểm thử trong Python

Bộ kiểm thử cuối cùng

import pytest

## Integration Tests
def test_read_df(read_df):
      # Kiểm tra kiểu của dataframe
    assert isinstance(read_df, pd.DataFrame)
    # Kiểm tra df có hàng
    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):
    # Lọc dữ liệu
    filtered = filter_df(read_df)
    # Trung bình > 0
    assert get_mean(filtered) > 0
    # Trung bình ≤ tối đa
    assert get_mean(filtered) <= read_df['salary_in_usd'].max()

## Performance Tests
def test_performance(benchmark, read_df):
    # Trình trang bị benchmark
    @benchmark
    # Hàm cần đo
    def pipeline():
        filtered = filter_df(read_df)
        return get_mean(filtered)
Nhập môn Kiểm thử trong Python

Ayo berlatih!

Nhập môn Kiểm thử trong Python

Preparing Video For Download...