Unit Tests¶
Warming Up¶
How many words are in the following sentence?¶
Call me Ishmael.
How many words are in the next sentence?¶
"you haint no objections to sharing a harpooneer's blanket, have ye? I s'pose you are goin' a-whalin', so you'd better get used to that sort of thing."
Exercise 1: Test a Python function¶
The function count_words()
in the module word_counter.py calculates the number of words in a text body.
For instance, we would expect the following input to result in a word count of 3
:
Call me Ishmael
Your task is to prove that the count_words()
function in fact returns 3
.
Run the example test in test_unit_test.py
with
python -m pytest test/test_unit_test.py
Exercise 2: A test fails if code is broken¶
Run the test in test_broken_code.py
and inspect the output.
It fails, because there is a bug in the function count_words()
.
The example string this time contains words separated by tabulators.
Fix the code in word_counter.py
, so that the test in test_broken_code.py
passes.
Exercise 3: A test fails if the test is broken¶
Run the test in test_broken_test.py
and inspect the output.
It fails, because there is a bug in the test file.
Fix the test code, so that the test passes.
Exercise 4: Test border cases¶
High quality tests cover many different situations. Common situations for the program word_counter.py include:
test case | description | example input | expected output |
---|---|---|---|
empty | input is valid, but empty | "" | 0 |
minimal | smallest reasonable input | "whale" | 1 |
typical | representative input | "whale eats captain" | 3 |
invalid | input is supposed to fail | 777 | TypeError |
maximum | largest reasonable input | Melville's entire book | > 200000 |
nasty | difficult example | "That #~&%* program still doesn't work!" | 6 |
Your task is to make all tests in test_border_cases.py pass.
Exercise 5: Special characters¶
Add a new feature to the word_counter.py program. The program should remove special characters from the text before counting words.
Your task is to write a test for this new feature.