SimpleTest v1.0
The Simple Development Library regression testing framework
Project: the Simple Development Library.
::Simple::Test::cget
?-store?::Simple::Test::configure
?-store list?::Simple::Test::flags
::Simple::Test::initialize
::Simple::Test::reset
::Simple::Test::results
::Simple::Test::set-test-criteria
testCriteria::Simple::Test::shutdown
::Simple::Test::statistics
test-case
tag title ?-regexp? ?-prerequisites script-list? ?-dependencies testtag-list? ?-setup script? ?-script script? ?-cleanup script? ?-output string? ?-error string? ?-return string?::Simple::Test::ENVIRONMENT-NOT-INITIALIZED
::Simple::Test::NO-SCRIPT-FLAG
::Simple::Test::RETURN-AND-ERROR-FLAGS
::Simple::Test::BAD-SET-OF-RANGES
::Simple::Test::BAD-FLAG-TO-STORE
::Simple::Test::cget
?-store?::Simple::Test::configure
?-store list?::Simple::Test::flags
::Simple::Test::initialize
::Simple::Test::reset
::Simple::Test::results
::Simple::Test::set-test-criteria
testCriteria::Simple::Test::shutdown
::Simple::Test::statistics
test-case
tag title ?-regexp? ?-prerequisites script-list? ?-dependencies testtag-list? ?-setup script? ?-script script? ?-cleanup script? ?-output string? ?-error string? ?-return string?Synopsis: the Simple Development Library regression testing framework.
Keywords: regression, test and package.
This package provides a complete framework for automated regression testing.
Procedures are provided to initialize, reset and shutdown the test environment, set criteria of which cases to test, run the test cases as well as others to obtain the test results and statistics.
The ::Simple::Test::initialize
procedure sets up a test environment where test cases can be run via the test-case
procedure. By using ::Simple::Test::set-test-criteria
, one can set a criteria to specify which test cases to test or ignore.
Upon testing a test case, its prerequisites are first assessed; if any prerequisite is not fulfilled, the test case is skipped. Next, the list of dependencies are checked. If any of the dependencies did not pass, the test case is skipped. Otherwise, the test case is run, and by comparing its output, errors and return values with the corresponding expected values, each test is flagged as passed or failed.
The detailed test results and the flags given to the corresponding call to the test-case
procedures are available via the ::Simple::Test::results
and ::Simple::Test::flags
procedures, respectively. Finally, the overall statistics of all the test cases can be obtained via the ::Simple::Test::statistics
procedure.
Use ::Simple::Test::reset
to reset the test environment and ::Simple::Test::shutdown
to clean up after testing.
# Install the package package require SimplePackage ::Simple::Package::require-and-install SimpleTest # Initialize the test environment ::Simple::Test::initialize # Set the test criteria: test cases number 1, 3 and following ::Simple::Test::set-test-criteria 1,3: # Execute several test cases # This one passes test-case test-case-1 { First test case } -setup { # Create a variable in the test environment set foo 1 } -script { puts -nonewline $foo } -output 1 # This one is ignored test-case test-case-2 { Second test case } -script { } # This one fails test-case test-case-3 { Third test case } -script { return $foo } -return 2 # This one is skipped test-case test-case-4 { Fourth test case } -prerequisites { {$foo == 2} } -script { } # This one passes also test-case test-case-5 { Fifth test case } -script { error {ERROR MESSAGE} } -cleanup { # Delete the variable in the test environment unset foo } -error {ERROR MESSAGE} # This one depends on test-case-1 and test-case-2 test-case test-case-6 { Sixth test case } -dependencies { test-case-1 test-case-2 } -script { puts -nonewline 1 } -output 1 # This displays the following: # |1 test-case-1 First test case: passed # |2 test-case-2 Second test case: ignored # |3 test-case-3 Third test case: failed # | -return = {2} # | Actual return = {1} # |4 test-case-4 Fourth test case: skipped # |5 test-case-5 Fifth test case: passed # |6 test-case-6 Sixth test case: skipped foreach result [::Simple::Test::results]\ optionPairs [::Simple::Test::flags] { # Get the test case information foreach {number tag title ignored skipped passed diagnostics}\ $result break if {$ignored} { set status ignored } elseif {$skipped} { set status skipped } elseif {$passed} { set status passed } else { set status failed } # Display a short report for each test case puts [format {%1d %10s %20s %s} $number $tag $title: $status] # For failed tests, display the test case flags and diagnostics if {![string compare $status failed]} { foreach {option contents} $optionPairs { puts [format {%16s = {%s}} $option $contents] } foreach {reason extra} $diagnostics { puts [format {%16s = {%s}} $reason $extra] } } } # Final tests statistics foreach {nTests passed failed skipped ignored}\ [::Simple::Test::statistics] break # Display some test cases statistics # This displays the following: # |Total test cases 6 # |Test cases passed 2 # |Test cases failed 1 # |Test cases skipped 2 # |Test cases ignored 1 puts [format {Total test cases %d} $nTests] puts [format {Test cases passed %d} $passed] puts [format {Test cases failed %d} $failed] puts [format {Test cases skipped %d} $skipped] puts [format {Test cases ignored %d} $ignored] # Shutdown the test environment ::Simple::Test::shutdown
Date | Reason |
19-feb-1999 | Unreleased first version 0.1 |
23-apr-2000 | First public release, version 0.2 |
10-sep-2001 | Second public release, version 0.4 |
17-feb-2003 | Third public release, version 0.5 |
25-sep-2003 | Named test interpreter, version 0.5.1 |
22-jun-2005 | The Simple Development Library version 1.0 |
Copyright (C) 1999-2005, Juan C. Gil (jgil@gmv.es).
Paradigm: procedural.
Requisites: SimpleType 1.0
.
Description: test case tag: public-api-1, ...
Description: criteria for test cases to be tested: :, 3, 3,7, :7, ..
Type: list.
Value: (empty string).
Description: flags to store for each test case tested, which can later be obtained via the ::Simple::Test::flags
procedure.
::Simple::Test
::Simple::Test::Priv
::Simple::Test::cget
?-store?Synopsis: gets the package options.
Details: see section 4.1.
::Simple::Test::configure
?-store list?Synopsis: configures the package options.
Details: see section 4.2.
::Simple::Test::flags
Synopsis: returns the test stored flags.
Details: see section 4.3.
::Simple::Test::initialize
Synopsis: initializes the test environment.
Details: see section 4.4.
::Simple::Test::reset
Synopsis: resets the test environment.
Details: see section 4.5.
::Simple::Test::results
Synopsis: returns the test cases results.
Details: see section 4.6.
::Simple::Test::set-test-criteria
testCriteriaSynopsis: sets the criteria to specify which test cases to test.
Details: see section 4.7.
::Simple::Test::shutdown
Synopsis: shutdowns the test environment.
Details: see section 4.8.
::Simple::Test::statistics
Synopsis: returns test cases statistics.
Details: see section 4.9.
test-case
tag title ?-regexp? ?-prerequisites script-list? ?-dependencies testtag-list? ?-setup script? ?-script script? ?-cleanup script? ?-output string? ?-error string? ?-return string?Synopsis: runs a test case.
Details: see section 4.10.
::Simple::Test::ENVIRONMENT-NOT-INITIALIZED
Message: test environment not initialized.
Explanation: the attempted operation requires an initialized test environment.
Corrective action: use the ::Simple::Test::initialize
procedure to initialize the test environment.
::Simple::Test::NO-SCRIPT-FLAG
Message: no -script for test "test case tag".
Explanation: test case test case tag lacks the compulsory -script flag.
::Simple::Test::RETURN-AND-ERROR-FLAGS
Message: both -return and -error given for test case "test case tag".
Explanation: test case test case tag has both -return and -error flags, but these are mutually exclusive.
::Simple::Test::BAD-SET-OF-RANGES
Message: bad set of ranges "ranges", range "range" is not well formed.
Explanation: the set of ranges ranges ranges has bad format; range is the bad range.
Corrective action: a set of ranges is one or more ranges separated by commas. A range is one unsigned integer or two unsigned integers separated by a colon where the first, the second or even both unsigned integers may not be present, meaning that the range is open by the left, by the right, or by both ends, respectively.
::Simple::Test::BAD-FLAG-TO-STORE
Message: bad flag to store "flag": must be -cleanup, -dependencies, -error, -output, -prerequisites, -regexp, -return, -script or -setup.
Explanation: flag flag is not allowed.
Corrective action: supply a flag among those in the list.
The test environment is in fact a Tcl slave interpreter in which the puts
command has been modified so that the test cases output can be collected.
The test interpreter name is contained in the ::Simple::Test::TestInterp variable. It is possible to set it to the empty string so as to use the toplevel interpreter. This is useful to run the tests within debugers which may have problems with child interpreters.
Consider whether it is worth it migrating the test infrastructure to use the facilities of the Tcl test harness package, tcltest
.
Add support for criteria to specify which test cases to test based on the test case tag.
Keep track of the test cases tags. Ensure they are not duplicated. Ensure tags of the form <tag>-<number> are correlative.
Allow to specify a user defined procedure for handling each test case results as soon as they are run.
::Simple::Test::cget
?-store?Synopsis: gets the package options.
Access mode: public.
Arguments:
Argument | Type | Default value/ choices | Description |
-store | boolflag | (n/a) | Gets the list of options to store for each test case tested which can be later be obtained via the ::Simple::Test::flags procedure. The returned list of options options is ordered as in the input to the corresponding test-case procedure. |
Returns: the requested option value or the whole list of options if none specified.
::Simple::Test::configure
?-store list?Synopsis: configures the package options.
Access mode: public.
Arguments:
Argument | Type | Default value/ choices | Description |
?-store? | list | (empty string) | Flags to store for each test case tested, which can be later be obtained via the ::Simple::Test::flags procedure. If the given argument is "all", then all options are stored. |
Returns: the empty string.
::Simple::Test::flags
Synopsis: returns the test stored flags.
Access mode: public.
This procedure returns a list containing the stored flags and their contents for a test case as given to the corresponding test-case
procedure. Each element in the list is a list of two-element lists, being the first a flag and the second its contents; this format is suitable to be used with the array get
command. As -regexp is a boolean flag, its contents is set to the empty string.
The stored flag are those of the -store package option. By default, no flags are stored. Nevertheless, if a test case fails the given items among those in the -store package option are stored along with the -regexp flag. If the test case throws an error when no error was expected, this procedure return an -error flag with "NONE" as contents.
Arguments: none.
Returns: the test cases flags.
::Simple::Test::initialize
Synopsis: initializes the test environment.
Access mode: public.
Arguments: none.
Returns: the empty string.
Effects:
Creates the test interpreter.
It is compulsory to call this procedure before testing.
::Simple::Test::reset
Synopsis: resets the test environment.
Access mode: public.
Arguments: none.
Returns: the empty string.
::Simple::Test::results
Synopsis: returns the test cases results.
Access mode: public.
This procedure returns a list containing for each test case tested the following elements:
0 | number |
1 | tag |
2 | title |
3 | whether it was ignored |
4 | whether it was skipped |
5 | whether it was passed or failed |
6 | in case it failed, the diagnostics; this is a list of two-elements lists, being the first element a reason and the second extra information |
Possible reasons and their corresponding extra information are the following:
Reason | Extra information |
Dependencies failed | List of failed dependencies |
Prerequisite failed | Error information (::errorInfo contents) |
Unfulfilled prerequisite | Prerequisite script failed |
Setup failed | Error information (::errorInfo contents) |
Cleanup failed | Error information (::errorInfo contents) |
Actual output | Actual test case output |
Actual error | Actual test case error |
Actual return | Actual test case return |
Error information | Error information (::errorInfo contents) |
The actual output, error or return appears when they did not match the expected output, error or return values, respectively. Notice that the actual output may appear together with the actual error or return, but not both. The error information appears when an error is thrown from the test case script.
Arguments: none.
Returns: the test cases results.
::Simple::Test::set-test-criteria
testCriteriaSynopsis: sets the criteria to specify which test cases to test.
Access mode: public.
This procedure sets the expression that specifies which test cases to test from the given criteria. Test cases matching the criteria are tested while all others are ignored.
Arguments:
Argument | Type | Default value/ choices | Description |
testCriteria | testcriteria | (n/a) | Criteria of which test cases to test |
Returns: the empty string.
Allow criteria based on test case tags.
::Simple::Test::shutdown
Synopsis: shutdowns the test environment.
Access mode: public.
Arguments: none.
Returns: the empty string.
Effects:
Deletes the test interpreter.
::Simple::Test::statistics
Synopsis: returns test cases statistics.
Access mode: public.
Arguments: none.
Returns: a list with the following elements:
0 | number of test cases executed |
1 | number of test cases passed |
2 | number of test cases failed |
3 | number of test cases skipped |
4 | number of test cases ignored |
test-case
tag title ?-regexp? ?-prerequisites script-list? ?-dependencies testtag-list? ?-setup script? ?-script script? ?-cleanup script? ?-output string? ?-error string? ?-return string?(Exported from ::Simple::Test
).
Synopsis: runs a test case.
Access mode: exported.
Keywords: test.
This is the actual test case handler. Works by running the test case in the test environment where the output of puts
is available at the end. If that output matches the expected output and any error message and return values match the expected ones, the test case is considered passed, otherwise the test fails. The output of this procedure is stored and can be obtained via the ::Simple::Test::results
procedure.
If the expected output, return value or error are empty, the test passes only if it does not produce any output, return value or error, respectively. If the expected return value or error is not given, then its actual value is not checked.
By specifying the -regexp flag, the expected output and return value or error are matched as regular expressions so that it is possible to validate non-constant values.
Before running the test case, each prerequisite in the list of prerequisites is evaluated. If any of them fails, the test is skipped. Next, the list of dependencies is checked. If any of the dependencies does not pass, the test is skipped.
Otherwise, the setup script is evaluated afterwards. If there is an error, the test is failed. Otherwise, the actual test is run followed by the cleanup script. An error in the cleanup script also results in the test case to be regarded as failed.
# A test which checks some output test-case T1 {Test 1} -script {puts -nonewline "foo"} -output foo # A test which checks an error message test-case T2 {Test 2} -script {return -code error bar} -error bar # A test which checks the output using a regular expression test-case T3 {Test 3} -regexp -script {puts -nonewline foo} -output {oo$}
Arguments:
Argument | Type | Default value/ choices | Description |
tag | testtag | (n/a) | Test case tag |
title | string | (n/a) | Test case title |
-regexp | boolflag | (n/a) | Whether the expected output, return and error shall be matched as regular expressions |
?-prerequisites? | script-list | (empty string) | Prerequisites scripts list |
?-dependencies? | testtag-list | (empty string) | List of test cases tags this test depends on |
?-setup? | script | (empty string) | Setup script |
?-script? | script | (empty string) | Test case script |
?-cleanup? | script | (empty string) | Cleanup script |
?-output? | string | (empty string) | Test case expected output |
?-error? | string | (empty string) | Test case expected error |
?-return? | string | (empty string) | Test case expected return |
Returns: whether the test passed or not.
Effects:
Those of the evaluated scripts in the test environment.
The -script argument is compulsory.
The -error and -return arguments are mutually exclusive.
It is compulsory to have called ::Simple::Test::initialize
before calling this procedure.
The order of the flags is irrelevant but their parameters can not start by an hyphen.