tcltest.n
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:44k
- '"
- '" Copyright (c) 1990-1994 The Regents of the University of California
- '" Copyright (c) 1994-1997 Sun Microsystems, Inc.
- '" Copyright (c) 1998-1999 Scriptics Corporation
- '" Copyright (c) 2000 Ajuba Solutions
- '" Contributions from Don Porter, NIST, 2002. (not subject to US copyright)
- '"
- '" See the file "license.terms" for information on usage and redistribution
- '" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
- '"
- '" RCS: @(#) $Id: tcltest.n,v 1.38.2.6 2007/02/20 17:53:18 dgp Exp $
- '"
- .so man.macros
- .TH "tcltest" n 2.2 tcltest "Tcl Bundled Packages"
- .BS
- '" Note: do not modify the .SH NAME line immediately below!
- .SH NAME
- tcltest - Test harness support code and utilities
- .SH SYNOPSIS
- .nf
- fBpackage require tcltest ?2.2.5?fR
- .sp
- fBtcltest::test fIname description ?option value ...?fR
- fBtcltest::test fIname description ?constraints? body resultfR
- .sp
- fBtcltest::loadTestedCommandsfR
- fBtcltest::makeDirectory fIname ?directory?fR
- fBtcltest::removeDirectory fIname ?directory?fR
- fBtcltest::makeFile fIcontents name ?directory?fR
- fBtcltest::removeFile fIname ?directory?fR
- fBtcltest::viewFile fIname ?directory?fR
- fBtcltest::cleanupTests fI?runningMultipleTests?fR
- fBtcltest::runAllTestsfR
- .sp
- fBtcltest::configurefR
- fBtcltest::configure fIoptionfR
- fBtcltest::configure fIoption value ?option value ...?fR
- fBtcltest::customMatch fImode commandfR
- fBtcltest::testConstraint fIconstraint ?value?fR
- fBtcltest::outputChannel fI?channelID?fR
- fBtcltest::errorChannel fI?channelID?fR
- fBtcltest::interpreter fI?interp?fR
- .sp
- fBtcltest::debug fI?level?fR
- fBtcltest::errorFile fI?filename?fR
- fBtcltest::limitConstraints fI?boolean?fR
- fBtcltest::loadFile fI?filename?fR
- fBtcltest::loadScript fI?script?fR
- fBtcltest::match fI?patternList?fR
- fBtcltest::matchDirectories fI?patternList?fR
- fBtcltest::matchFiles fI?patternList?fR
- fBtcltest::outputFile fI?filename?fR
- fBtcltest::preserveCore fI?level?fR
- fBtcltest::singleProcess fI?boolean?fR
- fBtcltest::skip fI?patternList?fR
- fBtcltest::skipDirectories fI?patternList?fR
- fBtcltest::skipFiles fI?patternList?fR
- fBtcltest::temporaryDirectory fI?directory?fR
- fBtcltest::testsDirectory fI?directory?fR
- fBtcltest::verbose fI?level?fR
- .sp
- fBtcltest::test fIname description optionListfR
- fBtcltest::bytestring fIstringfR
- fBtcltest::normalizeMsg fImsgfR
- fBtcltest::normalizePath fIpathVarfR
- fBtcltest::workingDirectory fI?dir?fR
- .fi
- .BE
- .SH DESCRIPTION
- .PP
- The fBtcltestfR package provides several utility commands useful
- in the construction of test suites for code instrumented to be
- run by evaluation of Tcl commands. Notably the built-in commands
- of the Tcl library itself are tested by a test suite using the
- tcltest package.
- .PP
- All the commands provided by the fBtcltestfR package are defined
- in and exported from the fB::tcltestfR namespace, as indicated in
- the fBSYNOPSISfR above. In the following sections, all commands
- will be described by their simple names, in the interest of brevity.
- .PP
- The central command of fBtcltestfR is [fBtestfR] that defines
- and runs a test. Testing with [fBtestfR] involves evaluation
- of a Tcl script and comparing the result to an expected result, as
- configured and controlled by a number of options. Several other
- commands provided by fBtcltestfR govern the configuration of
- [fBtestfR] and the collection of many [fBtestfR] commands into
- test suites.
- .PP
- See fBCREATING TEST SUITES WITH TCLTESTfR below for an extended example
- of how to use the commands of fBtcltestfR to produce test suites
- for your Tcl-enabled code.
- .SH COMMANDS
- .TP
- fBtestfR fIname description ?option value ...?fR
- Defines and possibly runs a test with the name fInamefR and
- description fIdescriptionfR. The name and description of a test
- are used in messages reported by [fBtestfR] during the
- test, as configured by the options of fBtcltestfR. The
- remaining fIoption valuefR arguments to [fBtestfR]
- define the test, including the scripts to run, the conditions
- under which to run them, the expected result, and the means
- by which the expected and actual results should be compared.
- See fBTESTSfR below for a complete description of the valid
- options and how they define a test. The [fBtestfR] command
- returns an empty string.
- .TP
- fBtestfR fIname description ?constraints? body resultfR
- This form of [fBtestfR] is provided to support test suites written
- for version 1 of the fBtcltestfR package, and also a simpler
- interface for a common usage. It is the same as
- [fBtestfR fIname descriptionfB -constraints fIconstraintsfB -body
- fIbodyfB -result fIresultfR]. All other options to [fBtestfR]
- take their default values. When fIconstraintsfR is omitted, this
- form of [fBtestfR] can be distinguished from the first because
- all fIoptionfRs begin with ``-''.
- .TP
- fBloadTestedCommandsfR
- Evaluates in the caller's context the script specified by
- [fBconfigure -loadfR] or [fBconfigure -loadfilefR].
- Returns the result of that script evaluation, including any error
- raised by the script. Use this command and the related
- configuration options to provide the commands to be tested to
- the interpreter running the test suite.
- .TP
- fBmakeFilefR fIcontents name ?directory?fR
- Creates a file named fInamefR relative to
- directory fIdirectoryfR and write fIcontentsfR
- to that file using the encoding [fBencoding systemfR].
- If fIcontentsfR does not end with a newline, a newline
- will be appended so that the file named fInamefR
- does end with a newline. Because the system encoding is used,
- this command is only suitable for making text files.
- The file will be removed by the next evaluation
- of [fBcleanupTestsfR], unless it is removed by
- [fBremoveFilefR] first. The default value of
- fIdirectoryfR is the directory [fBconfigure -tmpdirfR].
- Returns the full path of the file created. Use this command
- to create any text file required by a test with contents as needed.
- .TP
- fBremoveFilefR fIname ?directory?fR
- Forces the file referenced by fInamefR to be removed. This file name
- should be relative to fIdirectoryfR. The default value of
- fIdirectoryfR is the directory [fBconfigure -tmpdirfR].
- Returns an empty string. Use this command to delete files
- created by [fBmakeFilefR].
- .TP
- fBmakeDirectoryfR fIname ?directory?fR
- Creates a directory named fInamefR relative to directory fIdirectoryfR.
- The directory will be removed by the next evaluation of [fBcleanupTestsfR],
- unless it is removed by [fBremoveDirectoryfR] first.
- The default value of fIdirectoryfR is the directory
- [fBconfigure -tmpdirfR].
- Returns the full path of the directory created. Use this command
- to create any directories that are required to exist by a test.
- .TP
- fBremoveDirectoryfR fIname ?directory?fR
- Forces the directory referenced by fInamefR to be removed. This
- directory should be relative to fIdirectoryfR.
- The default value of fIdirectoryfR is the directory
- [fBconfigure -tmpdirfR].
- Returns an empty string. Use this command to delete any directories
- created by [fBmakeDirectoryfR].
- .TP
- fBviewFilefR fIfile ?directory?fR
- Returns the contents of fIfilefR, except for any
- final newline, just as [fBread -nonewlinefR] would return.
- This file name should be relative to fIdirectoryfR.
- The default value of fIdirectoryfR is the directory
- [fBconfigure -tmpdirfR]. Use this command
- as a convenient way to turn the contents of a file generated
- by a test into the result of that test for matching against
- an expected result. The contents of the file are read using
- the system encoding, so its usefulness is limited to text
- files.
- .TP
- fBcleanupTestsfR
- Intended to clean up and summarize after several tests have been
- run. Typically called once per test file, at the end of the file
- after all tests have been completed. For best effectiveness, be
- sure that the [fBcleanupTestsfR] is evaluated even if an error
- occurs earlier in the test file evaluation.
- .sp
- Prints statistics about the tests run and removes files that were
- created by [fBmakeDirectoryfR] and [fBmakeFilefR] since the
- last [fBcleanupTestsfR]. Names of files and directories
- in the directory [fBconfigure -tmpdirfR] created since
- the last [fBcleanupTestsfR], but not created by
- [fBmakeFilefR] or [fBmakeDirectoryfR] are printed
- to [fBoutputChannelfR]. This command also restores the original
- shell environment, as described by the ::env
- array. Returns an empty string.
- .TP
- fBrunAllTestsfR
- This is a master command meant to run an entire suite of tests,
- spanning multiple files and/or directories, as governed by
- the configurable options of fBtcltestfR. See fBRUNNING ALL TESTSfR
- below for a complete description of the many variations possible
- with [fBrunAllTestsfR].
- .SH "CONFIGURATION COMMANDS"
- .TP
- fBconfigurefR
- Returns the list of configurable options supported by fBtcltestfR.
- See fBCONFIGURABLE OPTIONSfR below for the full list of options,
- their valid values, and their effect on fBtcltestfR operations.
- .TP
- fBconfigure fIoptionfR
- Returns the current value of the supported configurable option fIoptionfR.
- Raises an error if fIoptionfR is not a supported configurable option.
- .TP
- fBconfigure fIoption value ?option value ...?fR
- Sets the value of each configurable option fIoptionfR to the
- corresponding value fIvaluefR, in order. Raises an error if
- an fIoptionfR is not a supported configurable option, or if
- fIvaluefR is not a valid value for the corresponding fIoptionfR,
- or if a fIvaluefR is not provided. When an error is raised, the
- operation of [fBconfigurefR] is halted, and subsequent fIoption valuefR
- arguments are not processed.
- .sp
- If the environment variable fB::env(TCLTEST_OPTIONS)fR exists when
- the fBtcltestfR package is loaded (by [fBpackage require tcltestfR])
- then its value is taken as a list of arguments to pass to [fBconfigurefR].
- This allows the default values of the configuration options to be
- set by the environment.
- .TP
- fBcustomMatch fImode scriptfR
- Registers fImodefR as a new legal value of the fB-matchfR option
- to [fBtestfR]. When the fB-match fImodefR option is
- passed to [fBtestfR], the script fIscriptfR will be evaluated
- to compare the actual result of evaluating the body of the test
- to the expected result.
- To perform the match, the fIscriptfR is completed with two additional
- words, the expected result, and the actual result, and the completed script
- is evaluated in the global namespace.
- The completed script is expected to return a boolean value indicating
- whether or not the results match. The built-in matching modes of
- [fBtestfR] are fBexactfR, fBglobfR, and fBregexpfR.
- .TP
- fBtestConstraint fIconstraint ?boolean?fR
- Sets or returns the boolean value associated with the named fIconstraintfR.
- See fBTEST CONSTRAINTSfR below for more information.
- .TP
- fBinterpreterfR fI?executableName?fR
- Sets or returns the name of the executable to be [fBexecfR]ed by
- [fBrunAllTestsfR] to run each test file when
- [fBconfigure -singleprocfR] is false.
- The default value for [fBinterpreterfR] is the name of the
- currently running program as returned by [fBinfo nameofexecutablefR].
- .TP
- fBoutputChannelfR fI?channelID?fR
- Sets or returns the output channel ID. This defaults to stdout.
- Any test that prints test related output should send
- that output to [fBoutputChannelfR] rather than letting
- that output default to stdout.
- .TP
- fBerrorChannelfR fI?channelID?fR
- Sets or returns the error channel ID. This defaults to stderr.
- Any test that prints error messages should send
- that output to [fBerrorChannelfR] rather than printing
- directly to stderr.
- .SH "SHORTCUT COMMANDS"
- .TP
- fBdebug fI?level?fR
- Same as [fBconfigure -debug fI?level?fR].
- .TP
- fBerrorFile fI?filename?fR
- Same as [fBconfigure -errfile fI?filename?fR].
- .TP
- fBlimitConstraints fI?boolean?fR
- Same as [fBconfigure -limitconstraints fI?boolean?fR].
- .TP
- fBloadFile fI?filename?fR
- Same as [fBconfigure -loadfile fI?filename?fR].
- .TP
- fBloadScript fI?script?fR
- Same as [fBconfigure -load fI?script?fR].
- .TP
- fBmatch fI?patternList?fR
- Same as [fBconfigure -match fI?patternList?fR].
- .TP
- fBmatchDirectories fI?patternList?fR
- Same as [fBconfigure -relateddir fI?patternList?fR].
- .TP
- fBmatchFiles fI?patternList?fR
- Same as [fBconfigure -file fI?patternList?fR].
- .TP
- fBoutputFile fI?filename?fR
- Same as [fBconfigure -outfile fI?filename?fR].
- .TP
- fBpreserveCore fI?level?fR
- Same as [fBconfigure -preservecore fI?level?fR].
- .TP
- fBsingleProcess fI?boolean?fR
- Same as [fBconfigure -singleproc fI?boolean?fR].
- .TP
- fBskip fI?patternList?fR
- Same as [fBconfigure -skip fI?patternList?fR].
- .TP
- fBskipDirectories fI?patternList?fR
- Same as [fBconfigure -asidefromdir fI?patternList?fR].
- .TP
- fBskipFiles fI?patternList?fR
- Same as [fBconfigure -notfile fI?patternList?fR].
- .TP
- fBtemporaryDirectory fI?directory?fR
- Same as [fBconfigure -tmpdir fI?directory?fR].
- .TP
- fBtestsDirectory fI?directory?fR
- Same as [fBconfigure -testdir fI?directory?fR].
- .TP
- fBverbose fI?level?fR
- Same as [fBconfigure -verbose fI?level?fR].
- .SH "OTHER COMMANDS"
- .PP
- The remaining commands provided by fBtcltestfR have better
- alternatives provided by fBtcltestfR or fBTclfR itself. They
- are retained to support existing test suites, but should be avoided
- in new code.
- .TP
- fBtestfR fIname description optionListfR
- This form of [fBtestfR] was provided to enable passing many
- options spanning several lines to [fBtestfR] as a single
- argument quoted by braces, rather than needing to backslash quote
- the newlines between arguments to [fBtestfR]. The fIoptionListfR
- argument is expected to be a list with an even number of elements
- representing fIoptionfR and fIvaluefR arguments to pass
- to [fBtestfR]. However, these values are not passed directly, as
- in the alternate forms of [fBswitchfR]. Instead, this form makes
- an unfortunate attempt to overthrow Tcl's substitution rules by
- performing substitutions on some of the list elements as an attempt to
- implement a ``do what I mean'' interpretation of a brace-enclosed
- ``block''. The result is nearly impossible to document clearly, and
- for that reason this form is not recommended. See the examples in
- fBCREATING TEST SUITES WITH TCLTESTfR below to see that this
- form is really not necessary to avoid backslash-quoted newlines.
- If you insist on using this form, examine
- the source code of fBtcltestfR if you want to know the substitution
- details, or just enclose the third through last argument
- to [fBtestfR] in braces and hope for the best.
- .TP
- fBworkingDirectoryfR fI?directoryName?fR
- Sets or returns the current working directory when the test suite is
- running. The default value for workingDirectory is the directory in
- which the test suite was launched. The Tcl commands [fBcdfR] and
- [fBpwdfR] are sufficient replacements.
- .TP
- fBnormalizeMsgfR fImsgfR
- Returns the result of removing the ``extra'' newlines from fImsgfR,
- where ``extra'' is rather imprecise. Tcl offers plenty of string
- processing commands to modify strings as you wish, and
- [fBcustomMatchfR] allows flexible matching of actual and expected
- results.
- .TP
- fBnormalizePathfR fIpathVarfR
- Resolves symlinks in a path, thus creating a path without internal
- redirection. It is assumed that fIpathVarfR is absolute.
- fIpathVarfR is modified in place. The Tcl command [fBfile normalizefR]
- is a sufficient replacement.
- .TP
- fBbytestringfR fIstringfR
- Construct a string that consists of the requested sequence of bytes,
- as opposed to a string of properly formed UTF-8 characters using the
- value supplied in fIstringfR. This allows the tester to create
- denormalized or improperly formed strings to pass to C procedures that
- are supposed to accept strings with embedded NULL types and confirm
- that a string result has a certain pattern of bytes. This is
- exactly equivalent to the Tcl command [fBencoding convertfrom identityfR].
- .SH TESTS
- .PP
- The [fBtestfR] command is the heart of the fBtcltestfR package.
- Its essential function is to evaluate a Tcl script and compare
- the result with an expected result. The options of [fBtestfR]
- define the test script, the environment in which to evaluate it,
- the expected result, and how the compare the actual result to
- the expected result. Some configuration options of fBtcltestfR
- also influence how [fBtestfR] operates.
- .PP
- The valid options for [fBtestfR] are summarized:
- .CS
- .ta 0.8i
- fBtestfR fInamefR fIdescriptionfR
- ?-constraints fIkeywordList|expressionfR?
- ?-setup fIsetupScriptfR?
- ?-body fItestScriptfR?
- ?-cleanup fIcleanupScriptfR?
- ?-result fIexpectedAnswerfR?
- ?-output fIexpectedOutputfR?
- ?-errorOutput fIexpectedErrorfR?
- ?-returnCodes fIcodeListfR?
- ?-match fImodefR?
- .CE
- The fInamefR may be any string. It is conventional to choose
- a fInamefR according to the pattern:
- .CS
- fItargetfR-fImajorNumfR.fIminorNumfR
- .CE
- For white-box (regression) tests, the target should be the name of the
- C function or Tcl procedure being tested. For black-box tests, the
- target should be the name of the feature being tested. Some conventions
- call for the names of black-box tests to have the suffix fB_bbfR.
- Related tests should share a major number. As a test suite evolves,
- it is best to have the same test name continue to correspond to the
- same test, so that it remains meaningful to say things like ``Test
- foo-1.3 passed in all releases up to 3.4, but began failing in
- release 3.5.''
- .PP
- During evaluation of [fBtestfR], the fInamefR will be compared
- to the lists of string matching patterns returned by
- [fBconfigure -matchfR], and [fBconfigure -skipfR]. The test
- will be run only if fInamefR matches any of the patterns from
- [fBconfigure -matchfR] and matches none of the patterns
- from [fBconfigure -skipfR].
- .PP
- The fIdescriptionfR should be a short textual description of the
- test. The fIdescriptionfR is included in output produced by the
- test, typically test failure messages. Good fIdescriptionfR values
- should briefly explain the purpose of the test to users of a test suite.
- The name of a Tcl or C function being tested should be included in the
- description for regression tests. If the test case exists to reproduce
- a bug, include the bug ID in the description.
- .PP
- Valid attributes and associated values are:
- .TP
- fB-constraints fIkeywordList|expressionfR
- The optional fB-constraintsfR attribute can be list of one or more
- keywords or an expression. If the fB-constraintsfR value is a list of
- keywords, each of these keywords should be the name of a constraint
- defined by a call to [fBtestConstraintfR]. If any of the listed
- constraints is false or does not exist, the test is skipped. If the
- fB-constraintsfR value is an expression, that expression
- is evaluated. If the expression evaluates to true, then the test is run.
- Note that the expression form of fB-constraintsfR may interfere with the
- operation of [fBconfigure -constraintsfR] and
- [fBconfigure -limitconstraintsfR], and is not recommended.
- Appropriate constraints should be added to any tests that should
- not always be run. That is, conditional evaluation of a test
- should be accomplished by the fB-constraintsfR option, not by
- conditional evaluation of [fBtestfR]. In that way, the same
- number of tests are always reported by the test suite, though
- the number skipped may change based on the testing environment.
- The default value is an empty list.
- See fBTEST CONSTRAINTSfR below for a list of built-in constraints
- and information on how to add your own constraints.
- .TP
- fB-setup fIscriptfR
- The optional fB-setupfR attribute indicates a fIscriptfR that will be run
- before the script indicated by the fB-bodyfR attribute. If evaluation
- of fIscriptfR raises an error, the test will fail. The default value
- is an empty script.
- .TP
- fB-body fIscriptfR
- The fB-bodyfR attribute indicates the fIscriptfR to run to carry out the
- test. It must return a result that can be checked for correctness.
- If evaluation of fIscriptfR raises an error, the test will fail.
- The default value is an empty script.
- .TP
- fB-cleanup fIscriptfR
- The optional fB-cleanupfR attribute indicates a fIscriptfR that will be
- run after the script indicated by the fB-bodyfR attribute.
- If evaluation of fIscriptfR raises an error, the test will fail.
- The default value is an empty script.
- .TP
- fB-match fImodefR
- The fB-matchfR attribute determines how expected answers supplied by
- fB-resultfR, fB-outputfR, and fB-errorOutputfR are compared. Valid
- values for fImodefR are fBregexpfR, fBglobfR, fBexactfR, and
- any value registered by a prior call to [fBcustomMatchfR]. The default
- value is fBexactfR.
- .TP
- fB-result fIexpectedValuefR
- The fB-resultfR attribute supplies the fIexpectedValuefR against which
- the return value from script will be compared. The default value is
- an empty string.
- .TP
- fB-output fIexpectedValuefR
- The fB-outputfR attribute supplies the fIexpectedValuefR against which
- any output sent to fBstdoutfR or [fBoutputChannelfR] during evaluation
- of the script(s) will be compared. Note that only output printed using
- [fB::putsfR] is used for comparison. If fB-outputfR is not specified,
- output sent to fBstdoutfR and [fBoutputChannelfR] is not processed for
- comparison.
- .TP
- fB-errorOutput fIexpectedValuefR
- The fB-errorOutputfR attribute supplies the fIexpectedValuefR against
- which any output sent to fBstderrfR or [fBerrorChannelfR] during
- evaluation of the script(s) will be compared. Note that only output
- printed using [fB::putsfR] is used for comparison. If fB-errorOutputfR
- is not specified, output sent to fBstderrfR and [fBerrorChannelfR] is
- not processed for comparison.
- .TP
- fB-returnCodes fIexpectedCodeListfR
- The optional fB-returnCodesfR attribute supplies fIexpectedCodeListfR,
- a list of return codes that may be accepted from evaluation of the
- fB-bodyfR script. If evaluation of the fB-bodyfR script returns
- a code not in the fIexpectedCodeListfR, the test fails. All
- return codes known to [fBreturnfR], in both numeric and symbolic
- form, including extended return codes, are acceptable elements in
- the fIexpectedCodeListfR. Default value is fB{ok return}fR.
- .PP
- To pass, a test must successfully evaluate its fB-setupfR, fB-bodyfR,
- and fB-cleanupfR scripts. The return code of the fB-bodyfR script and
- its result must match expected values, and if specified, output and error
- data from the test must match expected fB-outputfR and fB-errorOutputfR
- values. If any of these conditions are not met, then the test fails.
- Note that all scripts are evaluated in the context of the caller
- of [fBtestfR].
- .PP
- As long as [fBtestfR] is called with valid syntax and legal
- values for all attributes, it will not raise an error. Test
- failures are instead reported as output written to [fBoutputChannelfR].
- In default operation, a successful test produces no output. The output
- messages produced by [fBtestfR] are controlled by the
- [fBconfigure -verbosefR] option as described in fBCONFIGURABLE OPTIONSfR
- below. Any output produced by the test scripts themselves should be
- produced using [fB::putsfR] to [fBoutputChannelfR] or
- [fBerrorChannelfR], so that users of the test suite may
- easily capture output with the [fBconfigure -outfilefR] and
- [fBconfigure -errfilefR] options, and so that the fB-outputfR
- and fB-errorOutputfR attributes work properly.
- .SH "TEST CONSTRAINTS"
- .PP
- Constraints are used to determine whether or not a test should be skipped.
- Each constraint has a name, which may be any string, and a boolean
- value. Each [fBtestfR] has a fB-constraintsfR value which is a
- list of constraint names. There are two modes of constraint control.
- Most frequently, the default mode is used, indicated by a setting
- of [fBconfigure -limitconstraintsfR] to false. The test will run
- only if all constraints in the list are true-valued. Thus,
- the fB-constraintsfR option of [fBtestfR] is a convenient, symbolic
- way to define any conditions required for the test to be possible or
- meaningful. For example, a [fBtestfR] with fB-constraints unixfR
- will only be run if the constraint fBunixfR is true, which indicates
- the test suite is being run on a Unix platform.
- .PP
- Each [fBtestfR] should include whatever fB-constraintsfR are
- required to constrain it to run only where appropriate. Several
- constraints are pre-defined in the fBtcltestfR package, listed
- below. The registration of user-defined constraints is performed
- by the [fBtestConstraintfR] command. User-defined constraints
- may appear within a test file, or within the script specified
- by the [fBconfigure -loadfR] or [fBconfigure -loadfilefR]
- options.
- .PP
- The following is a list of constraints pre-defined by the
- fBtcltestfR package itself:
- .TP
- fIsingleTestInterpfR
- test can only be run if all test files are sourced into a single interpreter
- .TP
- fIunixfR
- test can only be run on any Unix platform
- .TP
- fIwinfR
- test can only be run on any Windows platform
- .TP
- fIntfR
- test can only be run on any Windows NT platform
- .TP
- fI95fR
- test can only be run on any Windows 95 platform
- .TP
- fI98fR
- test can only be run on any Windows 98 platform
- .TP
- fImacfR
- test can only be run on any Mac platform
- .TP
- fIunixOrWinfR
- test can only be run on a Unix or Windows platform
- .TP
- fImacOrWinfR
- test can only be run on a Mac or Windows platform
- .TP
- fImacOrUnixfR
- test can only be run on a Mac or Unix platform
- .TP
- fItempNotWinfR
- test can not be run on Windows. This flag is used to temporarily
- disable a test.
- .TP
- fItempNotMacfR
- test can not be run on a Mac. This flag is used
- to temporarily disable a test.
- .TP
- fIunixCrashfR
- test crashes if it's run on Unix. This flag is used to temporarily
- disable a test.
- .TP
- fIwinCrashfR
- test crashes if it's run on Windows. This flag is used to temporarily
- disable a test.
- .TP
- fImacCrashfR
- test crashes if it's run on a Mac. This flag is used to temporarily
- disable a test.
- .TP
- fIemptyTestfR
- test is empty, and so not worth running, but it remains as a
- place-holder for a test to be written in the future. This constraint
- has value false to cause tests to be skipped unless the user specifies
- otherwise.
- .TP
- fIknownBugfR
- test is known to fail and the bug is not yet fixed. This constraint
- has value false to cause tests to be skipped unless the user specifies
- otherwise.
- .TP
- fInonPortablefR
- test can only be run in some known development environment.
- Some tests are inherently non-portable because they depend on things
- like word length, file system configuration, window manager, etc.
- This constraint has value false to cause tests to be skipped unless
- the user specifies otherwise.
- .TP
- fIuserInteractionfR
- test requires interaction from the user. This constraint has
- value false to causes tests to be skipped unless the user specifies
- otherwise.
- .TP
- fIinteractivefR
- test can only be run in if the interpreter is in interactive mode
- (when the global tcl_interactive variable is set to 1).
- .TP
- fInonBlockFilesfR
- test can only be run if platform supports setting files into
- nonblocking mode
- .TP
- fIasyncPipeClosefR
- test can only be run if platform supports async flush and async close
- on a pipe
- .TP
- fIunixExecsfR
- test can only be run if this machine has Unix-style commands
- fBcatfR, fBechofR, fBshfR, fBwcfR, fBrmfR, fBsleepfR,
- fBfgrepfR, fBpsfR, fBchmodfR, and fBmkdirfR available
- .TP
- fIhasIsoLocalefR
- test can only be run if can switch to an ISO locale
- .TP
- fIrootfR
- test can only run if Unix user is root
- .TP
- fInotRootfR
- test can only run if Unix user is not root
- .TP
- fIeformatfR
- test can only run if app has a working version of sprintf with respect
- to the "e" format of floating-point numbers.
- .TP
- fIstdiofR
- test can only be run if [fBinterpreterfR] can be [fBopenfR]ed
- as a pipe.
- .PP
- The alternative mode of constraint control is enabled by setting
- [fBconfigure -limitconstraintsfR] to true. With that configuration
- setting, all existing constraints other than those in the constraint
- list returned by [fBconfigure -constraintsfR] are set to false.
- When the value of [fBconfigure -constraintsfR]
- is set, all those constraints are set to true. The effect is that
- when both options [fBconfigure -constraintsfR] and
- [fBconfigure -limitconstraintsfR] are in use, only those tests including
- only constraints from the [fBconfigure -constraintsfR] list
- are run; all others are skipped. For example, one might set
- up a configuration with
- .CS
- fBconfigurefR -constraints knownBug e
- -limitconstraints true e
- -verbose pass
- .CE
- to run exactly those tests that exercise known bugs, and discover
- whether any of them pass, indicating the bug had been fixed.
- .SH "RUNNING ALL TESTS"
- .PP
- The single command [fBrunAllTestsfR] is evaluated to run an entire
- test suite, spanning many files and directories. The configuration
- options of fBtcltestfR control the precise operations. The
- [fBrunAllTestsfR] command begins by printing a summary of its
- configuration to [fBoutputChannelfR].
- .PP
- Test files to be evaluated are sought in the directory
- [fBconfigure -testdirfR]. The list of files in that directory
- that match any of the patterns in [fBconfigure -filefR] and
- match none of the patterns in [fBconfigure -notfilefR] is generated
- and sorted. Then each file will be evaluated in turn. If
- [fBconfigure -singleprocfR] is true, then each file will
- be [fBsourcefR]d in the caller's context. If it is false,
- then a copy of [fBinterpreterfR] will be [fBexecfR]d to
- evaluate each file. The multi-process operation is useful
- when testing can cause errors so severe that a process
- terminates. Although such an error may terminate a child
- process evaluating one file, the master process can continue
- with the rest of the test suite. In multi-process operation,
- the configuration of fBtcltestfR in the master process is
- passed to the child processes as command line arguments,
- with the exception of [fBconfigure -outfilefR]. The
- [fBrunAllTestsfR] command in the
- master process collects all output from the child processes
- and collates their results into one master report. Any
- reports of individual test failures, or messages requested
- by a [fBconfigure -verbosefR] setting are passed directly
- on to [fBoutputChannelfR] by the master process.
- .PP
- After evaluating all selected test files, a summary of the
- results is printed to [fBoutputChannelfR]. The summary
- includes the total number of [fBtestfR]s evaluated, broken
- down into those skipped, those passed, and those failed.
- The summary also notes the number of files evaluated, and the names
- of any files with failing tests or errors. A list of
- the constraints that caused tests to be skipped, and the
- number of tests skipped for each is also printed. Also,
- messages are printed if it appears that evaluation of
- a test file has caused any temporary files to be left
- behind in [fBconfigure -tmpdirfR].
- .PP
- Having completed and summarized all selected test files,
- [fBrunAllTestsfR] then recursively acts on subdirectories
- of [fBconfigure -testdirfR]. All subdirectories that
- match any of the patterns in [fBconfigure -relateddirfR]
- and do not match any of the patterns in
- [fBconfigure -asidefromdirfR] are examined. If
- a file named fBall.tclfR is found in such a directory,
- it will be [fBsourcefR]d in the caller's context.
- Whether or not an examined directory contains an
- fBall.tclfR file, its subdirectories are also scanned
- against the [fBconfigure -relateddirfR] and
- [fBconfigure -asidefromdirfR] patterns. In this way,
- many directories in a directory tree can have all their
- test files evaluated by a single [fBrunAllTestsfR]
- command.
- .SH "CONFIGURABLE OPTIONS"
- The [fBconfigurefR] command is used to set and query the configurable
- options of fBtcltestfR. The valid options are:
- .TP
- fB-singleproc fIbooleanfR
- Controls whether or not [fBrunAllTestsfR] spawns a child process for
- each test file. No spawning when fIbooleanfR is true. Default
- value is false.
- .TP
- fB-debug fIlevelfR
- Sets the debug level to fIlevelfR, an integer value indicating how
- much debugging information should be printed to stdout. Note that
- debug messages always go to stdout, independent of the value of
- [fBconfigure -outfilefR]. Default value is 0. Levels are defined as:
- .RS
- .IP 0
- Do not display any debug information.
- .IP 1
- Display information regarding whether a test is skipped because it
- doesn't match any of the tests that were specified using by
- [fBconfigure -matchfR] (userSpecifiedNonMatch) or matches any of
- the tests specified by [fBconfigure -skipfR] (userSpecifiedSkip). Also
- print warnings about possible lack of cleanup or balance in test files.
- Also print warnings about any re-use of test names.
- .IP 2
- Display the flag array parsed by the command line processor, the
- contents of the ::env array, and all user-defined variables that exist
- in the current namespace as they are used.
- .IP 3
- Display information regarding what individual procs in the test
- harness are doing.
- .RE
- .TP
- fB-verbose fIlevelfR
- Sets the type of output verbosity desired to fIlevelfR,
- a list of zero or more of the elements fBbodyfR, fBpassfR,
- fBskipfR, fBstartfR, and fBerrorfR. Default value
- is fB{body error}fR.
- Levels are defined as:
- .RS
- .IP "body (b)"
- Display the body of failed tests
- .IP "pass (p)"
- Print output when a test passes
- .IP "skip (s)"
- Print output when a test is skipped
- .IP "start (t)"
- Print output whenever a test starts
- .IP "error (e)"
- Print errorInfo and errorCode, if they exist, when a test return code
- does not match its expected return code
- .RE
- The single letter abbreviations noted above are also recognized
- so that [fBconfigure -verbose ptfR] is the same as
- [fBconfigure -verbose {pass start}fR].
- .TP
- fB-preservecore fIlevelfR
- Sets the core preservation level to fIlevelfR. This level
- determines how stringent checks for core files are. Default
- value is 0. Levels are defined as:
- .RS
- .IP 0
- No checking - do not check for core files at the end of each test
- command, but do check for them in [fBrunAllTestsfR] after all
- test files have been evaluated.
- .IP 1
- Also check for core files at the end of each [fBtestfR] command.
- .IP 2
- Check for core files at all times described above, and save a
- copy of each core file produced in [fBconfigure -tmpdirfR].
- .RE
- .TP
- fB-limitconstraints fIbooleanfR
- Sets the mode by which [fBtestfR] honors constraints as described
- in fBTESTSfR above. Default value is false.
- .TP
- fB-constraints fIlistfR
- Sets all the constraints in fIlistfR to true. Also used in
- combination with [fBconfigure -limitconstraints truefR] to control an
- alternative constraint mode as described in fBTESTSfR above.
- Default value is an empty list.
- .TP
- fB-tmpdir fIdirectoryfR
- Sets the temporary directory to be used by [fBmakeFilefR],
- [fBmakeDirectoryfR], [fBviewFilefR], [fBremoveFilefR],
- and [fBremoveDirectoryfR] as the default directory where
- temporary files and directories created by test files should
- be created. Default value is [fBworkingDirectoryfR].
- .TP
- fB-testdir fIdirectoryfR
- Sets the directory searched by [fBrunAllTestsfR] for test files
- and subdirectories. Default value is [fBworkingDirectoryfR].
- .TP
- fB-file fIpatternListfR
- Sets the list of patterns used by [fBrunAllTestsfR] to determine
- what test files to evaluate. Default value is fB*.testfR.
- .TP
- fB-notfile fIpatternListfR
- Sets the list of patterns used by [fBrunAllTestsfR] to determine
- what test files to skip. Default value is fBl.*.testfR, so
- that any SCCS lock files are skipped.
- .TP
- fB-relateddir fIpatternListfR
- Sets the list of patterns used by [fBrunAllTestsfR] to determine
- what subdirectories to search for an fBall.tclfR file. Default
- value is fB*fR.
- .TP
- fB-asidefromdir fIpatternListfR
- Sets the list of patterns used by [fBrunAllTestsfR] to determine
- what subdirectories to skip when searching for an fBall.tclfR file.
- Default value is an empty list.
- .TP
- fB-match fIpatternListfR
- Set the list of patterns used by [fBtestfR] to determine whether
- a test should be run. Default value is fB*fR.
- .TP
- fB-skip fIpatternListfR
- Set the list of patterns used by [fBtestfR] to determine whether
- a test should be skipped. Default value is an empty list.
- .TP
- fB-load fIscriptfR
- Sets a script to be evaluated by [fBloadTestedCommandsfR].
- Default value is an empty script.
- .TP
- fB-loadfile fIfilenamefR
- Sets the filename from which to read a script to be evaluated
- by [fBloadTestedCommandsfR]. This is an alternative to
- fB-loadfR. They cannot be used together.
- .TP
- fB-outfile fIfilenamefR
- Sets the file to which all output produced by tcltest should be
- written. A file named fIfilenamefR will be [fBopenfR]ed for writing,
- and the resulting channel will be set as the value of [fBoutputChannelfR].
- .TP
- fB-errfile fIfilenamefR
- Sets the file to which all error output produced by tcltest
- should be written. A file named fIfilenamefR will be [fBopenfR]ed
- for writing, and the resulting channel will be set as the value
- of [fBerrorChannelfR].
- .SH "CREATING TEST SUITES WITH TCLTEST"
- .PP
- The fundamental element of a test suite is the individual [fBtestfR]
- command. We begin with several examples.
- .IP [1]
- Test of a script that returns normally.
- .CS
- fBtestfR example-1.0 {normal return} {
- format %s value
- } value
- .CE
- .IP [2]
- Test of a script that requires context setup and cleanup. Note the
- bracing and indenting style that avoids any need for line continuation.
- .CS
- fBtestfR example-1.1 {test file existence} -setup {
- set file [makeFile {} test]
- } -body {
- file exists $file
- } -cleanup {
- removeFile test
- } -result 1
- .CE
- .IP [3]
- Test of a script that raises an error.
- .CS
- fBtestfR example-1.2 {error return} -body {
- error message
- } -returnCodes error -result message
- .CE
- .IP [4]
- Test with a constraint.
- .CS
- fBtestfR example-1.3 {user owns created files} -constraints {
- unix
- } -setup {
- set file [makeFile {} test]
- } -body {
- file attributes $file -owner
- } -cleanup {
- removeFile test
- } -result $::tcl_platform(user)
- .CE
- .PP
- At the next higher layer of organization, several [fBtestfR] commands
- are gathered together into a single test file. Test files should have
- names with the fB.testfR extension, because that is the default pattern
- used by [fBrunAllTestsfR] to find test files. It is a good rule of
- thumb to have one test file for each source code file of your project.
- It is good practice to edit the test file and the source code file
- together, keeping tests synchronized with code changes.
- .PP
- Most of the code in the test file should be the [fBtestfR] commands.
- Use constraints to skip tests, rather than conditional evaluation
- of [fBtestfR]. That is, do this:
- .IP [5]
- .CS
- fBtestConstraintfR X [expr $myRequirement]
- fBtestfR goodConditionalTest {} X {
- # body
- } result
- .CE
- and do not do this:
- .IP [6]
- .CS
- if $myRequirement {
- test badConditionalTest {} {
- #body
- } result
- }
- .CE
- .PP
- Use the fB-setupfR and fB-cleanupfR options to establish and release
- all context requirements of the test body. Do not make tests depend on
- prior tests in the file. Those prior tests might be skipped. If several
- consecutive tests require the same context, the appropriate setup
- and cleanup scripts may be stored in variable for passing to each tests
- fB-setupfR and fB-cleanupfR options. This is a better solution than
- performing setup outside of [fBtestfR] commands, because the setup will
- only be done if necessary, and any errors during setup will be reported,
- and not cause the test file to abort.
- .PP
- A test file should be able to be combined with other test files and not
- interfere with them, even when [fBconfigure -singleproc 1fR] causes
- all files to be evaluated in a common interpreter. A simple way to
- achieve this is to have your tests define all their commands and variables
- in a namespace that is deleted when the test file evaluation is complete.
- A good namespace to use is a child namespace fBtestfR of the namespace
- of the module you are testing.
- .PP
- A test file should also be able to be evaluated directly as a script,
- not depending on being called by a master [fBrunAllTestsfR]. This
- means that each test file should process command line arguments to give
- the tester all the configuration control that fBtcltestfR provides.
- .PP
- After all [fBtestfR]s in a test file, the command [fBcleanupTestsfR]
- should be called.
- .IP [7]
- Here is a sketch of a sample test file illustrating those points:
- .CS
- package require tcltest 2.2
- eval fB::tcltest::configurefR $argv
- package require example
- namespace eval ::example::test {
- namespace import ::tcltest::*
- fBtestConstraintfR X [expr {...}]
- variable SETUP {#common setup code}
- variable CLEANUP {#common cleanup code}
- fBtestfR example-1 {} -setup $SETUP -body {
- # First test
- } -cleanup $CLEANUP -result {...}
- fBtestfR example-2 {} -constraints X -setup $SETUP -body {
- # Second test; constrained
- } -cleanup $CLEANUP -result {...}
- fBtestfR example-3 {} {
- # Third test; no context required
- } {...}
- fBcleanupTestsfR
- }
- namespace delete ::example::test
- .CE
- .PP
- The next level of organization is a full test suite, made up of several
- test files. One script is used to control the entire suite. The
- basic function of this script is to call [fBrunAllTestsfR] after
- doing any necessary setup. This script is usually named fBall.tclfR
- because that's the default name used by [fBrunAllTestsfR] when combining
- multiple test suites into one testing run.
- .IP [8]
- Here is a sketch of a sample test suite master script:
- .CS
- package require Tcl 8.4
- package require tcltest 2.2
- package require example
- fB::tcltest::configurefR -testdir
- [file dirname [file normalize [info script]]]
- eval fB::tcltest::configurefR $argv
- fB::tcltest::runAllTestsfR
- .CE
- .SH COMPATIBILITY
- .PP
- A number of commands and variables in the fB::tcltestfR namespace
- provided by earlier releases of fBtcltestfR have not been documented
- here. They are no longer part of the supported public interface of
- fBtcltestfR and should not be used in new test suites. However,
- to continue to support existing test suites written to the older
- interface specifications, many of those deprecated commands and
- variables still work as before. For example, in many circumstances,
- [fBconfigurefR] will be automatically called shortly after
- [fBpackage require tcltest 2.1fR] succeeds with arguments
- from the variable fB::argvfR. This is to support test suites
- that depend on the old behavior that fBtcltestfR was automatically
- configured from command line arguments. New test files should not
- depend on this, but should explicitly include
- .CS
- eval fB::tcltest::configurefR $::argv
- .CE
- to establish a configuration from command line arguments.
- .SH "KNOWN ISSUES"
- There are two known issues related to nested evaluations of [fBtestfR].
- The first issue relates to the stack level in which test scripts are
- executed. Tests nested within other tests may be executed at the same
- stack level as the outermost test. For example, in the following code:
- .CS
- fBtestfR level-1.1 {level 1} {
- -body {
- fBtestfR level-2.1 {level 2} {
- }
- }
- }
- .CE
- any script executed in level-2.1 may be executed at the same stack
- level as the script defined for level-1.1.
- .PP
- In addition, while two [fBtestfR]s have been run, results will only
- be reported by [fBcleanupTestsfR] for tests at the same level as
- test level-1.1. However, test results for all tests run prior to
- level-1.1 will be available when test level-2.1 runs. What this
- means is that if you try to access the test results for test level-2.1,
- it will may say that 'm' tests have run, 'n' tests have
- been skipped, 'o' tests have passed and 'p' tests have failed,
- where 'm', 'n', 'o', and 'p' refer to tests that were run at the
- same test level as test level-1.1.
- .PP
- Implementation of output and error comparison in the test command
- depends on usage of ::puts in your application code. Output is
- intercepted by redefining the ::puts command while the defined test
- script is being run. Errors thrown by C procedures or printed
- directly from C applications will not be caught by the test command.
- Therefore, usage of the fB-outputfR and fB-errorOutputfR
- options to [fBtestfR] is useful only for pure Tcl applications
- that use [fB::putsfR] to produce output.
- .SH KEYWORDS
- test, test harness, test suite