使用 pytest 创建 fixture
在编写单元测试时,您有时需要在测试开始前做一些准备工作。如果把这些准备直接写在单元测试里,会让测试更难读,而且可能需要重复多次。好在 pytest 提供了 fixture,可以用来解决这些问题。
在本练习中,pandas 已经以 pd 导入,并且可以使用下面的 extract() 函数!
def extract(file_path):
return pd.read_csv(file_path)
本练习是课程的一部分
使用 Python 的 ETL 和 ELT
练习说明
- 导入
pytest库。 - 创建名为
raw_tax_data的pytestfixture。 - 返回
raw_dataDataFrame。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import pytest
____
# Create a pytest fixture
@pytest.fixture()
def ____():
raw_data = extract("raw_tax_data.csv")
# Return the raw DataFrame
return ____