实践示例

Python 测试入门

Alexander Levin

Data Scientist

数据与流水线

数据:数据科学薪资。

每行包含一名数据从业者的薪资、职位等信息。

数据科学薪资表

流水线:计算平均薪资:

  1. 读取数据
  2. 按雇佣类型筛选
  3. 计算平均薪资
  4. 保存结果
Python 测试入门

流水线代码

import pandas as pd

# 获取数据的 fixture
@pytest.fixture
def read_df():
    return pd.read_csv('ds_salaries.csv')
# 过滤函数
def filter_df(df):
    return df[df['employment_type'] == 'FT']
# 计算均值
def get_mean(df):   
    return df['salary_in_usd'].mean()
Python 测试入门

集成测试

测试用例:

  • 读取数据
  • 写入文件

代码:

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 测试入门

集成测试

示例:检查 Python 能否创建文件。

def test_write():
    # 以写入模式打开文件
    with open('temp.txt', 'w') as wfile:
        # 写入文本
        wfile.write('Testing stuff is awesome')
    # 检查文件是否存在
    assert os.path.exists('temp.txt')
    # 别忘了清理
    os.remove('temp.txt')
Python 测试入门

单元测试

测试用例:

  • 过滤后数据集仅含"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 测试入门

特性测试

测试用例:

  • 均值大于 0
  • 均值不大于数据集中的最高薪资

代码:

def test_feature(read_df):
    # 过滤数据
    filtered = filter_df(read_df)
    # 用例:均值大于 0
    assert get_mean(filtered) > 0
    # 用例:均值不大于最大值
    assert get_mean(filtered) <= read_df['salary_in_usd'].max()
Python 测试入门

性能测试

测试用例:

  • 从开始到结束的流水线执行时间

代码:

def test_performance(benchmark, read_df):
    # 基准装饰器
    @benchmark
    # 待测函数
    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 测试入门

Passons à la pratique !

Python 测试入门

Preparing Video For Download...