Software Design and Implementation
T5: On Unit Testing
Assignment T5 should be completed in a pair programming partnership, generally two students.
- Understanding the value of thorough testing.
- Applying unit testing to code to root out bugs.
Unit TestingIt is considered a best practice in software development to include automatic "unit testing" of source code. Unit testing simply means systematically testing of each logical unit or logical block of code. This provides a way of verifying that these individual blocks of code, such as functions, are working properly. This is a good method because each test suite of unit tests can be preserved, making it possible to change the implementation of a function at a later time and still easily and quickly test that it still does what it was intended to do by making sure it still satisfies its unit tests.
Many organizations see both code and program documentation as valuable assets. Recently, suites of unit tests have been preserved to document the suite of tests used to test each function's correctness.This suite can be stored right in the code base long after the testing phase is complete. In fact, extra code in your program which is only there because it makes debugging or testing easier is called scaffolding.
Some errors are subtle. You should not feel bad when your code has errors. All programmers make errors. However, by becoming a strategic sleuth and documenting your sleuthery using unit tests, you can generally eliminate the errors. The European Space Agency might have saved millions of dollar is if they had only created and reused their earlier unit test suite when they ported the code to the Ariane 5 project from the Ariane 4 project.
Lets look at an example program which use a test suite with unit tests:
Please try to run and understand the flow of execution in the above programs as the test suites run. Note also that the unit tests are organized into blocks and those blocks are commented. For example, in testing whether an integer is even, we want to test large and small positive numbers, zero, and negative numbers which are large and small in magnitude.
Your Team Assignment instructionsThe point of this assignment is NOT to fix the code. DO NOT MODIFY ANY OF THE EXISTING CODE or we will reduce your credit. Your job as far as the code goes is ONLY to add an appropriate test-suite.
This is because the primary objective is to learn to use unit tests effectively. Some errors are really subtle. I am giving you one of these subtle errors. Sometimes in testing you can feel like you are going around and around in circles or banging your head against the wall. Unit tests are designed to help you become a detective and to remember what you have already ruled out.
- Download the following buggy python code sample:
- unittest-change.py and rename as yourusernames-unittest-t5.py