Sometimes you need to prepare test data (load files, create objects etc.). If you want to prepare the same data for multiple tests, you can use a fixture.
Create a new module conftest.py
in the same directory as your tests.
There, add a function that loads the file data/mobydick_summary.txt
:
Place the decorator @pytest.fixture
on top of it:
:::python3
import pytest
@pytest.fixture
def text_summary():
return open(...).read()
Now create a module test_corpus.py
with a function that uses the fixture:
:::python3
def test_short_sample(text_summary):
assert count_words(text_summary) == 77
Execute the module with pytest
. Note that you do not need to import conftest
. Pytest does that automatically.
Create a fixture for the full text of the book mobydick_full.txt
as well.
Create a fixture in conftest.py
that prepares a dictionary with word counts using the word_counter.count_words_dict()
function.
:::python3
from word_counter import count_words_dict
@pytest.fixture
def count_dict(text_summary):
return ...
Write a simple test that makes sure the dictionary is not empty.