Using the new fixtures

With our two fixtures in place, we now have a file that is created before each test method and which is deleted after each test method. This means that each test method is starting in a stable, known state. This is critical to making reproducible tests. Let's pass this filename to analyze_text() by modifying our existing test:

# text_analyzer.py

class TextAnalysisTests(unittest.TestCase):
. . .
def test_function_runs(self):
"Basic smoke test: does the function run."
analyze_text(self.filename)

Remember that our setUp() stored the filename on self.filename. Since the self argument passed to the fixtures is the same instance as that passed to the test methods, our test can access the filename using that attribute.

Of course, when we run our test we see that this test fails because analyze_text() doesn't accept any arguments yet:

% python text_analyzer.py
E
======================================================================
ERROR: test_function_runs (__main__.TextAnalysisTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "text_analyzer.py", line 25, in test_function_runs
analyze_text(self.filename)
TypeError: analyze_text() takes no arguments (1 given)

----------------------------------------------------------------------
Ran 1 test in 0.003s

FAILED (errors=1)

We can fix this by simply adding a parameter to analyze_text():

# text_analyzer.py

def analyze_text(filename):
pass

And if we run our tests again, we see that we're once again passing:

% python text_analyzer.py
.
----------------------------------------------------------------------
Ran 1 test in 0.003s

OK

We still don't have an implementation that does anything useful, but you can start to see how the tests drive the implementation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset