[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3. Specifying test cases

Once a human tester finds a bug, it should be the last time a human tester finds that bug. Automatic tests should check for it from then on.

Andrew Hunt and David Thomas The Pragmatic Programmer

Random testing is neat, and sometimes uncovers interesting cases that you may not have tried. But to be sure you are covering specific cases, you need to specify them somehow. The list example at the end of the overview is one way, but another is reading them from a file. QCheck provides a small API for using files within a directory or lines within a text file as test cases.

 

The following functions generate file and directory names as test cases. This is useful, for example, for regression tests of a compiler – just keep a directory of source files to be compiled. The directory stream should be read all the way to the end, or else the directory handle will not be properly closed. (The check function does this automatically.)

 
type dirstream                              
val openDir : string → dirstream           
val nextFile : (string, dirstream) reader   

Here is an example of how to run tests on filenames in a directory:

 
    check (Files.nextFile, pretty_printer)
          (test_name, test_predicate)
          (Files.openDir directory_path)
 

The following functions produce lines of text from a file as test cases. The produced strings include newlines, but see below for how to filter them.

 
type filestream                             
val openFile : string → filestream         
val nextLine : (string, filestream) reader  

Here are some simple utilities for readers. The types should be self-explanatory. The chop function removes newlines from the ends of string readers (such as nextLine).

 
val map : ('a → 'b) → ('a,'c) reader → ('b,'c) reader 
val filter : ('a → bool) → ('a,'b) reader → ('a,'b) reader 
val chop : (string,'a) reader → (string,'a) reader 

[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Chris League on April, 14 2008 using texi2html 1.78.