Teachpack : Testing Framework

Download testing.ss to your computer. Use DrScheme's "Language" / "Add Teachpack ..." menu option to add this teachpack. The testing teachpack provides the functions below, which help us make and use test cases.


This teachpack provides four constructs for testing programs:

The first three forms create tests:

  • (check-expect test-expression expected-value) where test-expression and expected-value are both expressions.
    The form evaluates test-expression and then verifies that its value is the same as expected-value.
  • (check-within test expected delta) where test-expression, expected, and delta are expressions.
    The form evaluates test-expression and verifies that it produces a real number such that its value is equal to expected plus or minus delta.
  • (check-error test message) where test and expected-message are expressions
    The form evaluates test and verifies that it signals an error with expected-message as the error report.
  • You should place them below the relevant function definitions in the Definitions Window. Eventually you should place all tests, such as these, at the bottom of the window.

    Finally, generate-report is a function that displays statistics of running tests:

  • generate-report : -> true
    (generate-report) displays the results of all tests created with check-expect, check-within, and check-error.
  • Place it at the end of the program or run the expression from the Interactions window after clicking RUN.


    Example: Place the following seven lines into the Definitions Window and click RUN:

    
    (check-expect (+ 1 1) 2)
    (check-expect (+ 1 1) 3)
    
    (check-within (+ 1 1) 2.1 .001)
    (check-within (+ 1 1) 2.1 .1)
    
    (check-error (+ 1 1) "3")
    (check-error (/ 1 0) "/: division by zero")
    
    (generate-report)
    
    

    Before you do so, try to figure out which of these tests succeed and which fail. After clicking run, you see a separate frame that records how many succeeded and failed and detail information about the failures, including links for highlighting the source.