Lab: Unit tests with PyUnit
To complete the lab, answer these questions
Goals
- Learn about what unit tests are.
- Learn how to create and run unit tests using the Python unittest module.
Background
Python's unittest module, also known as PyUnit, gives you a simple tool to help you do Test-Driven Development (TDD). This exercise will give you a short introduction to unittest.
To get help, you can read through the Basic Example in the unittest documentation. Then take a look, briefly, at the more formal documentation of assertFalse and assertTrue and unittest.TestCase.
As always, if you have questions while you work, you can post in #questions on Slack.
- Get a clone of my CS257 samples repository. It should contain a folder called tdd/, containing Python files primechecker.py and primecheckertests.py.
- Don't open primechecker.py, but do the following to learn about PrimeChecker
and its two main methods:
python3 >>> import primechecker >>> dir(primechecker.PrimeChecker) ... >>> help(primechecker.PrimeChecker.is_prime) ... >>> help(primechecker.PrimeChecker.get_primes_below) ...
- Read through primecheckertests.py. Think about what you expect to see when you run it, and why.
- Run
python3 primecheckertests.py. Look through the output, compare it to the code, and think about why this command produces the output it produces. [See question in Google survey.] - Put print statements in
setUp,tearDown, and each of thetest_xxxxxmethods and rerunpython3 primecheckertests.py. Observe the order in which the print statements get executed. [See question in Google survey.] (NOTE: A better way to do this is to use a python debugger and put breakpoints in every function. I'll show you how to do that some day soon.) - In the Best practices: Test structure section of the Wikipedia page on TDD, the authors describe four phases of a good test case: setup, execution, validation, and cleanup. [See question in Google survey.]
- Based on the documentation for is_prime, what should happen when you call is_prime(0)? Use this information and this chart to fix test_zero. [See question in Google survey.]
- Add a new test to check what happens when you test a negative number for primality. Think about what you should name the test, and what should happen when you run it. [See question in Google survey.]
- Add more tests to check the boundary cases for is_prime and get_primes_below. Be creative—try to think of all the different ways programmers might abuse these methods, and create a test for each. Be ready to discuss your tests, and how they went. [See question in Google survey.]
- Open primechecker.py and break its algorithm in some simple way (e.g. on line 24, you could change "currentPrime = 2" to "currentPrime = 3"). Think about what you expect will happen if you re-run the unit tests after this change. The re-run the unit tests and compare the results to your expectations.
- Put primechecker.py back the way you found it, and make sure it passes all your tests.
- Consider this. Suppose you were about to embark on the implementation of a Python project, and you had already designed your class interfaces and typed up the method signatures as stubs. Now suppose you took the time at this stage to write unit tests analogous to primecheckertests.py. Think about how this would affect the progress of your code-writing and debugging. [See question in Google survey.]