Unit testing a data pipeline with fixtures
You've learned in the last video that unit testing can help to instill more trust in your data pipeline, and can even help to catch bugs throughout development. In this exercise, you'll practice writing both fixtures and unit tests, using the pytest
library and assert
.
The transform
function that you'll be building unit tests around is shown below for reference. pandas
has been imported as pd
, and the pytest()
library is loaded and ready for use.
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
This exercise is part of the course
ETL and ELT in Python
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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 ____