MulaiMulai sekarang secara gratis

Creating fixtures with pytest

When building unit tests, you'll sometimes have to do a bit of setup before testing can begin. Doing this setup within a unit test can make the tests more difficult to read, and may have to be repeated several times. Luckily, pytest offers a way to solve these problems, with fixtures.

For this exercise, pandas has been imported as pd, and the extract() function shown below is available for use!

def extract(file_path):
    return pd.read_csv(file_path)

Latihan ini adalah bagian dari kursus

ETL and ELT in Python

Lihat Kursus

Petunjuk latihan

  • Import the pytest library.
  • Create a pytest fixture called raw_tax_data.
  • Return the raw_data DataFrame.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# Import pytest
____

# Create a pytest fixture
@pytest.fixture()
def ____():
	raw_data = extract("raw_tax_data.csv")
    
    # Return the raw DataFrame
	return ____
Edit dan Jalankan Kode