CS 240: Data Structures and Algorithms
James Madison University, Spring 2013

In-Class Activity: Black-Box Unit Testing

Objective

The goal of today's activity is to get comfortable developing test cases and working with Python's unit testing tools.

Exercises

  1. Download the following files: The files array_set.pyc and broken_array_set.pyc are Python byte-code versions of modules containing completed (but not completely tested) Set classes. pyc files are analogous to .class files in Java. They can be imported and used like other modules, but they are not stored in a human-readable format. (Note that Python byte-code is NOT portable between different versions of Python. The set modules listed above will only work with Python 2.7.)

    set_tests.py is a module that contains unit testing code for the Set class. Right now, it only includes tests for __init__, add and __contains__. Your goal today will be to write tests for the discard method.
  2. Take a minute to look over the file set_tests.py. Try running the tests. Note that array_set.pyc must be in the same directory as set_tests.py in order for the tests to execute. The Set class defined in array_set.pyc should pass all tests and you should see output like the following:
    ...........
    ----------------------------------------------------------------------
    Ran 11 tests in 0.033s
    
    OK
    
  3. Spend a few minutes minutes brain-storming test cases for the discard method. Write down a list of possible tests.
  4. Implement as many of your tests as you have time for, either individually or with a partner. The discard method defined in array_set.pyc is correct (as far as I know). It should pass all of your tests. The version of discard defined in broken_array_set.pyc is not correct. It contains at least two serious bugs. Your goal is to write tests that allow you to identify those bugs.

    You can change which module is being tested by changing the statement

    import array_set as set_module
    
    to
    import broken_array_set as set_module
    
    in set_tests.py.

Finishing Up

There is nothing to hand in for this assignment. Make sure that you save a copy of your code. Your code should include the names of your group members so that you can acknowledge them in your PA2 submission.