시작하기무료로 시작하기

fixture로 데이터 파이프라인 단위 테스트하기

지난 영상에서 단위 테스트가 데이터 파이프라인에 대한 신뢰를 높이고, 개발 전반에서 버그를 잡는 데도 도움이 된다는 것을 배웠습니다. 이번 연습 문제에서는 pytest 라이브러리와 assert를 사용해 fixture와 단위 테스트를 모두 작성해 보겠습니다.

단위 테스트를 작성하게 될 transform 함수는 참고용으로 아래에 제시되어 있습니다. pandaspd로 임포트되어 있으며, pytest() 라이브러리도 로드되어 사용할 준비가 되어 있습니다.

def transform(raw_data):
    raw_data["tax_rate"] = raw_data["total_taxes_paid"] / raw_data["total_taxable_income"]
    raw_data.set_index("industry_name", inplace=True)
    return raw_data

이 연습은 강의의 일부입니다

Python으로 ETL과 ELT

강의 보기

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Define a pytest fixture
@pytest.fixture()
____ ____():
    raw_data = pd.read_csv("raw_tax_data.csv")
    
    # Transform the raw_data, store in clean_data DataFrame, and return the variable
    clean_data = ____
    return ____
코드 편집 및 실행