CSC 226

CSC 226 logo

Software Design and Implementation


T5: On Unit Testing

Assignment T5 should be completed in a pair programming partnership, generally two students.


Objectives

  • Understanding the value of thorough testing.
  • Applying unit testing to code to root out bugs.
This assignment is to be completed in class in pairs.

Unit Testing

It 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 instructions

The 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.

  1. Download the following buggy python code sample:
  2. In the header of this program, put both you and your partner's names as having added the test suite.  Also, include the role each partner played.  e.g. navigator, driver, etc.
  3. Look carefully at the syntax and flow of control of the program. This program utilizes the unit test function described in chapter 6 of our textbook.
  4. Your job today is to create a good suite of unit tests which would help you to sleuth out categories of numbers for which the unit tests pass and categories in which the unit tests fail.
  5. Organize your unit tests into categories of inputs and add comments to each section regarding your thinking about each category.
  6. Create as many unit tests as you feel are necessary to correctly identify the categories of numbers in which the given i_steal_pennies() function fails to make correct change. This may be a large number of unit tests.  That is good!
  7. If you are extra-specially mathematically inclined, you might even be able to describe the category of numbers for which the program steals pennies. If you can, describe these numbers in the comments.  This is not required, but we will be impressed if you can do it.
  8. When we near the end of the period, upload this program with your completed test suite to Moodle.
  9. Remember that both partners need to submit to Moodle, but you only need to submit a single program per team and that you must include both team members' names and the roles you had in each of the files. 

Copyright © 2016 | http://cs.berea.edu/courses/csc226/ | Licensed under a Creative Commons Attribution-Share Alike 3.0 United States License