Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/tcltest.n @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 44.2 KB
Line 
1'\"
2'\" Copyright (c) 1990-1994 The Regents of the University of California
3'\" Copyright (c) 1994-1997 Sun Microsystems, Inc.
4'\" Copyright (c) 1998-1999 Scriptics Corporation
5'\" Copyright (c) 2000 Ajuba Solutions
6'\" Contributions from Don Porter, NIST, 2002. (not subject to US copyright)
7'\"
8'\" See the file "license.terms" for information on usage and redistribution
9'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10'\"
11'\" RCS: @(#) $Id: tcltest.n,v 1.55 2007/12/13 15:22:33 dgp Exp $
12'\"
13.so man.macros
14.TH "tcltest" n 2.3 tcltest "Tcl Bundled Packages"
15.BS
16'\" Note:  do not modify the .SH NAME line immediately below!
17.SH NAME
18tcltest \- Test harness support code and utilities
19.SH SYNOPSIS
20.nf
21\fBpackage require tcltest ?2.3?\fR
22.sp
23\fBtcltest::test \fIname description ?option value ...?\fR
24\fBtcltest::test \fIname description ?constraints? body result\fR
25.sp
26\fBtcltest::loadTestedCommands\fR
27\fBtcltest::makeDirectory \fIname ?directory?\fR
28\fBtcltest::removeDirectory \fIname ?directory?\fR
29\fBtcltest::makeFile \fIcontents name ?directory?\fR
30\fBtcltest::removeFile \fIname ?directory?\fR
31\fBtcltest::viewFile \fIname ?directory?\fR
32\fBtcltest::cleanupTests \fI?runningMultipleTests?\fR
33\fBtcltest::runAllTests\fR
34.sp
35\fBtcltest::configure\fR
36\fBtcltest::configure \fIoption\fR
37\fBtcltest::configure \fIoption value ?option value ...?\fR
38\fBtcltest::customMatch \fImode command\fR
39\fBtcltest::testConstraint \fIconstraint ?value?\fR
40\fBtcltest::outputChannel \fI?channelID?\fR
41\fBtcltest::errorChannel \fI?channelID?\fR
42\fBtcltest::interpreter \fI?interp?\fR
43.sp
44\fBtcltest::debug \fI?level?\fR
45\fBtcltest::errorFile \fI?filename?\fR
46\fBtcltest::limitConstraints \fI?boolean?\fR
47\fBtcltest::loadFile \fI?filename?\fR
48\fBtcltest::loadScript \fI?script?\fR
49\fBtcltest::match \fI?patternList?\fR
50\fBtcltest::matchDirectories \fI?patternList?\fR
51\fBtcltest::matchFiles \fI?patternList?\fR
52\fBtcltest::outputFile \fI?filename?\fR
53\fBtcltest::preserveCore \fI?level?\fR
54\fBtcltest::singleProcess \fI?boolean?\fR
55\fBtcltest::skip \fI?patternList?\fR
56\fBtcltest::skipDirectories \fI?patternList?\fR
57\fBtcltest::skipFiles \fI?patternList?\fR
58\fBtcltest::temporaryDirectory \fI?directory?\fR
59\fBtcltest::testsDirectory \fI?directory?\fR
60\fBtcltest::verbose \fI?level?\fR
61.sp
62\fBtcltest::test \fIname description optionList\fR
63\fBtcltest::bytestring \fIstring\fR
64\fBtcltest::normalizeMsg \fImsg\fR
65\fBtcltest::normalizePath \fIpathVar\fR
66\fBtcltest::workingDirectory \fI?dir?\fR
67.fi
68.BE
69.SH DESCRIPTION
70.PP
71The \fBtcltest\fR package provides several utility commands useful
72in the construction of test suites for code instrumented to be
73run by evaluation of Tcl commands.  Notably the built-in commands
74of the Tcl library itself are tested by a test suite using the
75tcltest package.
76.PP
77All the commands provided by the \fBtcltest\fR package are defined
78in and exported from the \fB::tcltest\fR namespace, as indicated in
79the \fBSYNOPSIS\fR above.  In the following sections, all commands
80will be described by their simple names, in the interest of brevity.
81.PP
82The central command of \fBtcltest\fR is \fBtest\fR that defines
83and runs a test.  Testing with \fBtest\fR involves evaluation
84of a Tcl script and comparing the result to an expected result, as
85configured and controlled by a number of options.  Several other
86commands provided by \fBtcltest\fR govern the configuration of
87\fBtest\fR and the collection of many \fBtest\fR commands into
88test suites.
89.PP
90See \fBCREATING TEST SUITES WITH TCLTEST\fR below for an extended example
91of how to use the commands of \fBtcltest\fR to produce test suites
92for your Tcl-enabled code.
93.SH COMMANDS
94.TP
95\fBtest\fR \fIname description ?option value ...?\fR
96Defines and possibly runs a test with the name \fIname\fR and
97description \fIdescription\fR.  The name and description of a test
98are used in messages reported by \fBtest\fR during the
99test, as configured by the options of \fBtcltest\fR.  The
100remaining \fIoption value\fR arguments to \fBtest\fR
101define the test, including the scripts to run, the conditions
102under which to run them, the expected result, and the means
103by which the expected and actual results should be compared.
104See \fBTESTS\fR below for a complete description of the valid
105options and how they define a test.  The \fBtest\fR command
106returns an empty string.
107.TP
108\fBtest\fR \fIname description ?constraints? body result\fR
109This form of \fBtest\fR is provided to support test suites written
110for version 1 of the \fBtcltest\fR package, and also a simpler
111interface for a common usage.  It is the same as
112.QW "\fBtest\fR \fIname description\fB \-constraints \fIconstraints\fB \-body \fIbody\fB \-result \fIresult\fR" .
113All other options to \fBtest\fR
114take their default values.  When \fIconstraints\fR is omitted, this
115form of \fBtest\fR can be distinguished from the first because
116all \fIoption\fRs begin with
117.QW \- .
118.TP
119\fBloadTestedCommands\fR
120Evaluates in the caller's context the script specified by
121\fBconfigure \-load\fR or \fBconfigure \-loadfile\fR.
122Returns the result of that script evaluation, including any error
123raised by the script.  Use this command and the related
124configuration options to provide the commands to be tested to
125the interpreter running the test suite.
126.TP
127\fBmakeFile\fR \fIcontents name ?directory?\fR
128Creates a file named \fIname\fR relative to
129directory \fIdirectory\fR and write \fIcontents\fR
130to that file using the encoding \fBencoding system\fR.
131If \fIcontents\fR does not end with a newline, a newline
132will be appended so that the file named \fIname\fR
133does end with a newline.  Because the system encoding is used,
134this command is only suitable for making text files.
135The file will be removed by the next evaluation
136of \fBcleanupTests\fR, unless it is removed by
137\fBremoveFile\fR first.  The default value of
138\fIdirectory\fR is the directory \fBconfigure \-tmpdir\fR.
139Returns the full path of the file created.  Use this command
140to create any text file required by a test with contents as needed.
141.TP
142\fBremoveFile\fR \fIname ?directory?\fR
143Forces the file referenced by \fIname\fR to be removed.  This file name
144should be relative to \fIdirectory\fR.   The default value of
145\fIdirectory\fR is the directory \fBconfigure \-tmpdir\fR.
146Returns an empty string.  Use this command to delete files
147created by \fBmakeFile\fR.
148.TP
149\fBmakeDirectory\fR \fIname ?directory?\fR
150Creates a directory named \fIname\fR relative to directory \fIdirectory\fR.
151The directory will be removed by the next evaluation of \fBcleanupTests\fR,
152unless it is removed by \fBremoveDirectory\fR first.
153The default value of \fIdirectory\fR is the directory
154\fBconfigure \-tmpdir\fR.
155Returns the full path of the directory created.  Use this command
156to create any directories that are required to exist by a test.
157.TP
158\fBremoveDirectory\fR \fIname ?directory?\fR
159Forces the directory referenced by \fIname\fR to be removed. This
160directory should be relative to \fIdirectory\fR.
161The default value of \fIdirectory\fR is the directory
162\fBconfigure \-tmpdir\fR.
163Returns an empty string.  Use this command to delete any directories
164created by \fBmakeDirectory\fR.
165.TP
166\fBviewFile\fR \fIfile ?directory?\fR
167Returns the contents of \fIfile\fR, except for any
168final newline, just as \fBread \-nonewline\fR would return.
169This file name should be relative to \fIdirectory\fR.
170The default value of \fIdirectory\fR is the directory
171\fBconfigure \-tmpdir\fR.  Use this command
172as a convenient way to turn the contents of a file generated
173by a test into the result of that test for matching against
174an expected result.  The contents of the file are read using
175the system encoding, so its usefulness is limited to text
176files.
177.TP
178\fBcleanupTests\fR
179Intended to clean up and summarize after several tests have been
180run.  Typically called once per test file, at the end of the file
181after all tests have been completed.  For best effectiveness, be
182sure that the \fBcleanupTests\fR is evaluated even if an error
183occurs earlier in the test file evaluation.
184.RS
185.PP
186Prints statistics about the tests run and removes files that were
187created by \fBmakeDirectory\fR and \fBmakeFile\fR since the
188last \fBcleanupTests\fR.  Names of files and directories
189in the directory \fBconfigure \-tmpdir\fR created since
190the last \fBcleanupTests\fR, but not created by
191\fBmakeFile\fR or \fBmakeDirectory\fR are printed
192to \fBoutputChannel\fR.  This command also restores the original
193shell environment, as described by the \fB::env\fR
194array. Returns an empty string.
195.RE
196.TP
197\fBrunAllTests\fR
198This is a master command meant to run an entire suite of tests,
199spanning multiple files and/or directories, as governed by
200the configurable options of \fBtcltest\fR.  See \fBRUNNING ALL TESTS\fR
201below for a complete description of the many variations possible
202with \fBrunAllTests\fR.
203.SH "CONFIGURATION COMMANDS"
204.TP
205\fBconfigure\fR
206Returns the list of configurable options supported by \fBtcltest\fR.
207See \fBCONFIGURABLE OPTIONS\fR below for the full list of options,
208their valid values, and their effect on \fBtcltest\fR operations.
209.TP
210\fBconfigure \fIoption\fR
211Returns the current value of the supported configurable option \fIoption\fR.
212Raises an error if \fIoption\fR is not a supported configurable option.
213.TP
214\fBconfigure \fIoption value ?option value ...?\fR
215Sets the value of each configurable option \fIoption\fR to the
216corresponding value \fIvalue\fR, in order.  Raises an error if
217an \fIoption\fR is not a supported configurable option, or if
218\fIvalue\fR is not a valid value for the corresponding \fIoption\fR,
219or if a \fIvalue\fR is not provided.  When an error is raised, the
220operation of \fBconfigure\fR is halted, and subsequent \fIoption value\fR
221arguments are not processed.
222.RS
223.PP
224If the environment variable \fB::env(TCLTEST_OPTIONS)\fR exists when
225the \fBtcltest\fR package is loaded (by \fBpackage require tcltest\fR)
226then its value is taken as a list of arguments to pass to \fBconfigure\fR.
227This allows the default values of the configuration options to be
228set by the environment.
229.RE
230.TP
231\fBcustomMatch \fImode script\fR
232Registers \fImode\fR as a new legal value of the \fB\-match\fR option
233to \fBtest\fR.  When the \fB\-match \fImode\fR option is
234passed to \fBtest\fR, the script \fIscript\fR will be evaluated
235to compare the actual result of evaluating the body of the test
236to the expected result.
237To perform the match, the \fIscript\fR is completed with two additional
238words, the expected result, and the actual result, and the completed script
239is evaluated in the global namespace.
240The completed script is expected to return a boolean value indicating
241whether or not the results match.  The built-in matching modes of
242\fBtest\fR are \fBexact\fR, \fBglob\fR, and \fBregexp\fR.
243.TP
244\fBtestConstraint \fIconstraint ?boolean?\fR
245Sets or returns the boolean value associated with the named \fIconstraint\fR.
246See \fBTEST CONSTRAINTS\fR below for more information.
247.TP
248\fBinterpreter\fR \fI?executableName?\fR
249Sets or returns the name of the executable to be \fBexec\fRed by
250\fBrunAllTests\fR to run each test file when
251\fBconfigure \-singleproc\fR is false.
252The default value for \fBinterpreter\fR is the name of the
253currently running program as returned by \fBinfo nameofexecutable\fR.
254.TP
255\fBoutputChannel\fR \fI?channelID?\fR
256Sets or returns the output channel ID.  This defaults to stdout.
257Any test that prints test related output should send
258that output to \fBoutputChannel\fR rather than letting
259that output default to stdout.
260.TP
261\fBerrorChannel\fR \fI?channelID?\fR
262Sets or returns the error channel ID.  This defaults to stderr.
263Any test that prints error messages should send
264that output to \fBerrorChannel\fR rather than printing
265directly to stderr.
266.SH "SHORTCUT COMMANDS"
267.TP
268\fBdebug \fI?level?\fR
269Same as \fBconfigure \-debug \fI?level?\fR.
270.TP
271\fBerrorFile \fI?filename?\fR
272Same as \fBconfigure \-errfile \fI?filename?\fR.
273.TP
274\fBlimitConstraints \fI?boolean?\fR
275Same as \fBconfigure \-limitconstraints \fI?boolean?\fR.
276.TP
277\fBloadFile \fI?filename?\fR
278Same as \fBconfigure \-loadfile \fI?filename?\fR.
279.TP
280\fBloadScript \fI?script?\fR
281Same as \fBconfigure \-load \fI?script?\fR.
282.TP
283\fBmatch \fI?patternList?\fR
284Same as \fBconfigure \-match \fI?patternList?\fR.
285.TP
286\fBmatchDirectories \fI?patternList?\fR
287Same as \fBconfigure \-relateddir \fI?patternList?\fR.
288.TP
289\fBmatchFiles \fI?patternList?\fR
290Same as \fBconfigure \-file \fI?patternList?\fR.
291.TP
292\fBoutputFile \fI?filename?\fR
293Same as \fBconfigure \-outfile \fI?filename?\fR.
294.TP
295\fBpreserveCore \fI?level?\fR
296Same as \fBconfigure \-preservecore \fI?level?\fR.
297.TP
298\fBsingleProcess \fI?boolean?\fR
299Same as \fBconfigure \-singleproc \fI?boolean?\fR.
300.TP
301\fBskip \fI?patternList?\fR
302Same as \fBconfigure \-skip \fI?patternList?\fR.
303.TP
304\fBskipDirectories \fI?patternList?\fR
305Same as \fBconfigure \-asidefromdir \fI?patternList?\fR.
306.TP
307\fBskipFiles \fI?patternList?\fR
308Same as \fBconfigure \-notfile \fI?patternList?\fR.
309.TP
310\fBtemporaryDirectory \fI?directory?\fR
311Same as \fBconfigure \-tmpdir \fI?directory?\fR.
312.TP
313\fBtestsDirectory \fI?directory?\fR
314Same as \fBconfigure \-testdir \fI?directory?\fR.
315.TP
316\fBverbose \fI?level?\fR
317Same as \fBconfigure \-verbose \fI?level?\fR.
318.SH "OTHER COMMANDS"
319.PP
320The remaining commands provided by \fBtcltest\fR have better
321alternatives provided by \fBtcltest\fR or \fBTcl\fR itself.  They
322are retained to support existing test suites, but should be avoided
323in new code.
324.TP
325\fBtest\fR \fIname description optionList\fR
326This form of \fBtest\fR was provided to enable passing many
327options spanning several lines to \fBtest\fR as a single
328argument quoted by braces, rather than needing to backslash quote
329the newlines between arguments to \fBtest\fR.  The \fIoptionList\fR
330argument is expected to be a list with an even number of elements
331representing \fIoption\fR and \fIvalue\fR arguments to pass
332to \fBtest\fR.  However, these values are not passed directly, as
333in the alternate forms of \fBswitch\fR.  Instead, this form makes
334an unfortunate attempt to overthrow Tcl's substitution rules by
335performing substitutions on some of the list elements as an attempt to
336implement a
337.QW "do what I mean"
338interpretation of a brace-enclosed
339.QW block .
340The result is nearly impossible to document clearly, and
341for that reason this form is not recommended.  See the examples in
342\fBCREATING TEST SUITES WITH TCLTEST\fR below to see that this
343form is really not necessary to avoid backslash-quoted newlines.
344If you insist on using this form, examine
345the source code of \fBtcltest\fR if you want to know the substitution
346details, or just enclose the third through last argument
347to \fBtest\fR in braces and hope for the best.
348.TP
349\fBworkingDirectory\fR \fI?directoryName?\fR
350Sets or returns the current working directory when the test suite is
351running.  The default value for workingDirectory is the directory in
352which the test suite was launched.  The Tcl commands \fBcd\fR and
353\fBpwd\fR are sufficient replacements.
354.TP
355\fBnormalizeMsg\fR \fImsg\fR
356Returns the result of removing the
357.QW extra
358newlines from \fImsg\fR, where
359.QW extra
360is rather imprecise.  Tcl offers plenty of string
361processing commands to modify strings as you wish, and
362\fBcustomMatch\fR allows flexible matching of actual and expected
363results.
364.TP
365\fBnormalizePath\fR \fIpathVar\fR
366Resolves symlinks in a path, thus creating a path without internal
367redirection.  It is assumed that \fIpathVar\fR is absolute.
368\fIpathVar\fR is modified in place.  The Tcl command \fBfile normalize\fR
369is a sufficient replacement.
370.TP
371\fBbytestring\fR \fIstring\fR
372Construct a string that consists of the requested sequence of bytes,
373as opposed to a string of properly formed UTF-8 characters using the
374value supplied in \fIstring\fR.  This allows the tester to create
375denormalized or improperly formed strings to pass to C procedures that
376are supposed to accept strings with embedded NULL types and confirm
377that a string result has a certain pattern of bytes.  This is
378exactly equivalent to the Tcl command \fBencoding convertfrom identity\fR.
379.SH TESTS
380.PP
381The \fBtest\fR command is the heart of the \fBtcltest\fR package.
382Its essential function is to evaluate a Tcl script and compare
383the result with an expected result.  The options of \fBtest\fR
384define the test script, the environment in which to evaluate it,
385the expected result, and how the compare the actual result to
386the expected result.  Some configuration options of \fBtcltest\fR
387also influence how \fBtest\fR operates.
388.PP
389The valid options for \fBtest\fR are summarized:
390.PP
391.CS
392\fBtest\fR \fIname\fR \fIdescription\fR
393        ?-constraints \fIkeywordList|expression\fR?
394        ?-setup \fIsetupScript\fR?
395        ?-body \fItestScript\fR?
396        ?-cleanup \fIcleanupScript\fR?
397        ?-result \fIexpectedAnswer\fR?
398        ?-output \fIexpectedOutput\fR?
399        ?-errorOutput \fIexpectedError\fR?
400        ?-returnCodes \fIcodeList\fR?
401        ?-match \fImode\fR?
402.CE
403.PP
404The \fIname\fR may be any string.  It is conventional to choose
405a \fIname\fR according to the pattern:
406.PP
407.CS
408\fItarget\fR-\fImajorNum\fR.\fIminorNum\fR
409.CE
410.PP
411For white-box (regression) tests, the target should be the name of the
412C function or Tcl procedure being tested.  For black-box tests, the
413target should be the name of the feature being tested.  Some conventions
414call for the names of black-box tests to have the suffix \fB_bb\fR.
415Related tests should share a major number.  As a test suite evolves,
416it is best to have the same test name continue to correspond to the
417same test, so that it remains meaningful to say things like
418.QW "Test foo-1.3 passed in all releases up to 3.4, but began failing in release 3.5."
419.PP
420During evaluation of \fBtest\fR, the \fIname\fR will be compared
421to the lists of string matching patterns returned by
422\fBconfigure \-match\fR, and \fBconfigure \-skip\fR.  The test
423will be run only if \fIname\fR matches any of the patterns from
424\fBconfigure \-match\fR and matches none of the patterns
425from \fBconfigure \-skip\fR.
426.PP
427The \fIdescription\fR should be a short textual description of the
428test.  The \fIdescription\fR is included in output produced by the
429test, typically test failure messages.  Good \fIdescription\fR values
430should briefly explain the purpose of the test to users of a test suite.
431The name of a Tcl or C function being tested should be included in the
432description for regression tests.  If the test case exists to reproduce
433a bug, include the bug ID in the description.
434.PP
435Valid attributes and associated values are:
436.TP
437\fB\-constraints \fIkeywordList|expression\fR
438The optional \fB\-constraints\fR attribute can be list of one or more
439keywords or an expression.  If the \fB\-constraints\fR value is a list of
440keywords, each of these keywords should be the name of a constraint
441defined by a call to \fBtestConstraint\fR.  If any of the listed
442constraints is false or does not exist, the test is skipped.  If the
443\fB\-constraints\fR value is an expression, that expression
444is evaluated. If the expression evaluates to true, then the test is run.
445Note that the expression form of \fB\-constraints\fR may interfere with the
446operation of \fBconfigure \-constraints\fR and
447\fBconfigure \-limitconstraints\fR, and is not recommended.
448Appropriate constraints should be added to any tests that should
449not always be run.  That is, conditional evaluation of a test
450should be accomplished by the \fB\-constraints\fR option, not by
451conditional evaluation of \fBtest\fR.  In that way, the same
452number of tests are always reported by the test suite, though
453the number skipped may change based on the testing environment.
454The default value is an empty list. 
455See \fBTEST CONSTRAINTS\fR below for a list of built-in constraints
456and information on how to add your own constraints.
457.TP
458\fB\-setup \fIscript\fR
459The optional \fB\-setup\fR attribute indicates a \fIscript\fR that will be run
460before the script indicated by the \fB\-body\fR attribute.  If evaluation
461of \fIscript\fR raises an error, the test will fail.  The default value
462is an empty script.
463.TP
464\fB\-body \fIscript\fR
465The \fB\-body\fR attribute indicates the \fIscript\fR to run to carry out the
466test.  It must return a result that can be checked for correctness.
467If evaluation of \fIscript\fR raises an error, the test will fail.
468The default value is an empty script.
469.TP
470\fB\-cleanup \fIscript\fR
471The optional \fB\-cleanup\fR attribute indicates a \fIscript\fR that will be
472run after the script indicated by the \fB\-body\fR attribute.
473If evaluation of \fIscript\fR raises an error, the test will fail.
474The default value is an empty script.
475.TP
476\fB\-match \fImode\fR
477The \fB\-match\fR attribute determines how expected answers supplied by
478\fB\-result\fR, \fB\-output\fR, and \fB\-errorOutput\fR are compared.  Valid
479values for \fImode\fR are \fBregexp\fR, \fBglob\fR, \fBexact\fR, and
480any value registered by a prior call to \fBcustomMatch\fR.  The default
481value is \fBexact\fR.
482.TP
483\fB\-result \fIexpectedValue\fR
484The \fB\-result\fR attribute supplies the \fIexpectedValue\fR against which
485the return value from script will be compared. The default value is
486an empty string.
487.TP
488\fB\-output \fIexpectedValue\fR
489The \fB\-output\fR attribute supplies the \fIexpectedValue\fR against which
490any output sent to \fBstdout\fR or \fBoutputChannel\fR during evaluation
491of the script(s) will be compared.  Note that only output printed using
492\fB::puts\fR is used for comparison.  If \fB\-output\fR is not specified,
493output sent to \fBstdout\fR and \fBoutputChannel\fR is not processed for
494comparison.
495.TP
496\fB\-errorOutput \fIexpectedValue\fR
497The \fB\-errorOutput\fR attribute supplies the \fIexpectedValue\fR against
498which any output sent to \fBstderr\fR or \fBerrorChannel\fR during
499evaluation of the script(s) will be compared. Note that only output
500printed using \fB::puts\fR is used for comparison.  If \fB\-errorOutput\fR
501is not specified, output sent to \fBstderr\fR and \fBerrorChannel\fR is
502not processed for comparison.
503.TP
504\fB\-returnCodes \fIexpectedCodeList\fR
505The optional \fB\-returnCodes\fR attribute supplies \fIexpectedCodeList\fR,
506a list of return codes that may be accepted from evaluation of the
507\fB\-body\fR script.  If evaluation of the \fB\-body\fR script returns
508a code not in the \fIexpectedCodeList\fR, the test fails.  All
509return codes known to \fBreturn\fR, in both numeric and symbolic
510form, including extended return codes, are acceptable elements in
511the \fIexpectedCodeList\fR.  Default value is
512.QW \fBok return\fR.
513.PP
514To pass, a test must successfully evaluate its \fB\-setup\fR, \fB\-body\fR,
515and \fB\-cleanup\fR scripts.  The return code of the \fB\-body\fR script and
516its result must match expected values, and if specified, output and error
517data from the test must match expected \fB\-output\fR and \fB\-errorOutput\fR
518values.  If any of these conditions are not met, then the test fails.
519Note that all scripts are evaluated in the context of the caller
520of \fBtest\fR.
521.PP
522As long as \fBtest\fR is called with valid syntax and legal
523values for all attributes, it will not raise an error.  Test
524failures are instead reported as output written to \fBoutputChannel\fR.
525In default operation, a successful test produces no output.  The output
526messages produced by \fBtest\fR are controlled by the
527\fBconfigure \-verbose\fR option as described in \fBCONFIGURABLE OPTIONS\fR
528below.  Any output produced by the test scripts themselves should be
529produced using \fB::puts\fR to \fBoutputChannel\fR or
530\fBerrorChannel\fR, so that users of the test suite may
531easily capture output with the \fBconfigure \-outfile\fR and
532\fBconfigure \-errfile\fR options, and so that the \fB\-output\fR
533and \fB\-errorOutput\fR attributes work properly.
534.SH "TEST CONSTRAINTS"
535.PP
536Constraints are used to determine whether or not a test should be skipped.
537Each constraint has a name, which may be any string, and a boolean
538value.  Each \fBtest\fR has a \fB\-constraints\fR value which is a
539list of constraint names.  There are two modes of constraint control.
540Most frequently, the default mode is used, indicated by a setting
541of \fBconfigure \-limitconstraints\fR to false.  The test will run
542only if all constraints in the list are true-valued.  Thus,
543the \fB\-constraints\fR option of \fBtest\fR is a convenient, symbolic
544way to define any conditions required for the test to be possible or
545meaningful.  For example, a \fBtest\fR with \fB\-constraints unix\fR
546will only be run if the constraint \fBunix\fR is true, which indicates
547the test suite is being run on a Unix platform.
548.PP
549Each \fBtest\fR should include whatever \fB\-constraints\fR are
550required to constrain it to run only where appropriate.  Several
551constraints are pre-defined in the \fBtcltest\fR package, listed
552below.  The registration of user-defined constraints is performed
553by the \fBtestConstraint\fR command.  User-defined constraints
554may appear within a test file, or within the script specified
555by the \fBconfigure \-load\fR or \fBconfigure \-loadfile\fR
556options.
557.PP
558The following is a list of constraints pre-defined by the
559\fBtcltest\fR package itself:
560.TP
561\fIsingleTestInterp\fR
562test can only be run if all test files are sourced into a single interpreter
563.TP
564\fIunix\fR
565test can only be run on any Unix platform
566.TP
567\fIwin\fR
568test can only be run on any Windows platform
569.TP
570\fInt\fR
571test can only be run on any Windows NT platform
572.TP
573\fI95\fR
574test can only be run on any Windows 95 platform
575.TP
576\fI98\fR
577test can only be run on any Windows 98 platform
578.TP
579\fImac\fR
580test can only be run on any Mac platform
581.TP
582\fIunixOrWin\fR
583test can only be run on a Unix or Windows platform
584.TP
585\fImacOrWin\fR
586test can only be run on a Mac or Windows platform
587.TP
588\fImacOrUnix\fR
589test can only be run on a Mac or Unix platform
590.TP
591\fItempNotWin\fR
592test can not be run on Windows.  This flag is used to temporarily
593disable a test.
594.TP
595\fItempNotMac\fR
596test can not be run on a Mac.  This flag is used
597to temporarily disable a test.
598.TP
599\fIunixCrash\fR
600test crashes if it is run on Unix.  This flag is used to temporarily
601disable a test.
602.TP
603\fIwinCrash\fR
604test crashes if it is run on Windows.  This flag is used to temporarily
605disable a test.
606.TP
607\fImacCrash\fR
608test crashes if it is run on a Mac.  This flag is used to temporarily
609disable a test.
610.TP
611\fIemptyTest\fR
612test is empty, and so not worth running, but it remains as a
613place-holder for a test to be written in the future.  This constraint
614has value false to cause tests to be skipped unless the user specifies
615otherwise.
616.TP
617\fIknownBug\fR
618test is known to fail and the bug is not yet fixed.  This constraint
619has value false to cause tests to be skipped unless the user specifies
620otherwise.
621.TP
622\fInonPortable\fR
623test can only be run in some known development environment.
624Some tests are inherently non-portable because they depend on things
625like word length, file system configuration, window manager, etc.
626This constraint has value false to cause tests to be skipped unless
627the user specifies otherwise. 
628.TP
629\fIuserInteraction\fR
630test requires interaction from the user.  This constraint has
631value false to causes tests to be skipped unless the user specifies
632otherwise. 
633.TP
634\fIinteractive\fR
635test can only be run in if the interpreter is in interactive mode
636(when the global tcl_interactive variable is set to 1).
637.TP
638\fInonBlockFiles\fR
639test can only be run if platform supports setting files into
640nonblocking mode
641.TP
642\fIasyncPipeClose\fR
643test can only be run if platform supports async flush and async close
644on a pipe
645.TP
646\fIunixExecs\fR
647test can only be run if this machine has Unix-style commands
648\fBcat\fR, \fBecho\fR, \fBsh\fR, \fBwc\fR, \fBrm\fR, \fBsleep\fR,
649\fBfgrep\fR, \fBps\fR, \fBchmod\fR, and \fBmkdir\fR available
650.TP
651\fIhasIsoLocale\fR
652test can only be run if can switch to an ISO locale
653.TP
654\fIroot\fR
655test can only run if Unix user is root
656.TP
657\fInotRoot\fR
658test can only run if Unix user is not root
659.TP
660\fIeformat\fR
661test can only run if app has a working version of sprintf with respect
662to the
663.QW e
664format of floating-point numbers.
665.TP
666\fIstdio\fR
667test can only be run if \fBinterpreter\fR can be \fBopen\fRed
668as a pipe.
669.PP
670The alternative mode of constraint control is enabled by setting
671\fBconfigure \-limitconstraints\fR to true.  With that configuration
672setting, all existing constraints other than those in the constraint
673list returned by \fBconfigure \-constraints\fR are set to false.
674When the value of \fBconfigure \-constraints\fR
675is set, all those constraints are set to true.  The effect is that
676when both options \fBconfigure \-constraints\fR and
677\fBconfigure \-limitconstraints\fR are in use, only those tests including
678only constraints from the \fBconfigure \-constraints\fR list
679are run; all others are skipped.  For example, one might set
680up a configuration with
681.PP
682.CS
683\fBconfigure\fR -constraints knownBug \e
684          -limitconstraints true \e
685          -verbose pass
686.CE
687.PP
688to run exactly those tests that exercise known bugs, and discover
689whether any of them pass, indicating the bug had been fixed.
690.SH "RUNNING ALL TESTS"
691.PP
692The single command \fBrunAllTests\fR is evaluated to run an entire
693test suite, spanning many files and directories.  The configuration
694options of \fBtcltest\fR control the precise operations.  The
695\fBrunAllTests\fR command begins by printing a summary of its
696configuration to \fBoutputChannel\fR.
697.PP
698Test files to be evaluated are sought in the directory
699\fBconfigure \-testdir\fR.  The list of files in that directory
700that match any of the patterns in \fBconfigure \-file\fR and
701match none of the patterns in \fBconfigure \-notfile\fR is generated
702and sorted.  Then each file will be evaluated in turn.  If
703\fBconfigure \-singleproc\fR is true, then each file will
704be \fBsource\fRd in the caller's context.  If it is false,
705then a copy of \fBinterpreter\fR will be \fBexec\fR'd to
706evaluate each file.  The multi-process operation is useful
707when testing can cause errors so severe that a process
708terminates.  Although such an error may terminate a child
709process evaluating one file, the master process can continue
710with the rest of the test suite.  In multi-process operation,
711the configuration of \fBtcltest\fR in the master process is
712passed to the child processes as command line arguments,
713with the exception of \fBconfigure \-outfile\fR.  The
714\fBrunAllTests\fR command in the
715master process collects all output from the child processes
716and collates their results into one master report.  Any
717reports of individual test failures, or messages requested
718by a \fBconfigure \-verbose\fR setting are passed directly
719on to \fBoutputChannel\fR by the master process.
720.PP
721After evaluating all selected test files, a summary of the
722results is printed to \fBoutputChannel\fR.  The summary
723includes the total number of \fBtest\fRs evaluated, broken
724down into those skipped, those passed, and those failed.
725The summary also notes the number of files evaluated, and the names
726of any files with failing tests or errors.  A list of
727the constraints that caused tests to be skipped, and the
728number of tests skipped for each is also printed.  Also,
729messages are printed if it appears that evaluation of
730a test file has caused any temporary files to be left
731behind in \fBconfigure \-tmpdir\fR.
732.PP
733Having completed and summarized all selected test files,
734\fBrunAllTests\fR then recursively acts on subdirectories
735of \fBconfigure \-testdir\fR.  All subdirectories that
736match any of the patterns in \fBconfigure \-relateddir\fR
737and do not match any of the patterns in
738\fBconfigure \-asidefromdir\fR are examined.  If
739a file named \fBall.tcl\fR is found in such a directory,
740it will be \fBsource\fRd in the caller's context.
741Whether or not an examined directory contains an
742\fBall.tcl\fR file, its subdirectories are also scanned
743against the \fBconfigure \-relateddir\fR and
744\fBconfigure \-asidefromdir\fR patterns.  In this way,
745many directories in a directory tree can have all their
746test files evaluated by a single \fBrunAllTests\fR
747command.
748.SH "CONFIGURABLE OPTIONS"
749The \fBconfigure\fR command is used to set and query the configurable
750options of \fBtcltest\fR.  The valid options are:
751.TP
752\fB\-singleproc \fIboolean\fR
753Controls whether or not \fBrunAllTests\fR spawns a child process for
754each test file.  No spawning when \fIboolean\fR is true.  Default
755value is false.
756.TP
757\fB\-debug \fIlevel\fR
758Sets the debug level to \fIlevel\fR, an integer value indicating how
759much debugging information should be printed to stdout.  Note that
760debug messages always go to stdout, independent of the value of
761\fBconfigure \-outfile\fR.  Default value is 0.  Levels are defined as:
762.RS
763.IP 0
764Do not display any debug information.
765.IP 1
766Display information regarding whether a test is skipped because it
767does not match any of the tests that were specified using by
768\fBconfigure \-match\fR (userSpecifiedNonMatch) or matches any of
769the tests specified by \fBconfigure \-skip\fR (userSpecifiedSkip).  Also
770print warnings about possible lack of cleanup or balance in test files.
771Also print warnings about any re-use of test names.
772.IP 2
773Display the flag array parsed by the command line processor, the
774contents of the ::env array, and all user-defined variables that exist
775in the current namespace as they are used.
776.IP 3
777Display information regarding what individual procs in the test
778harness are doing.
779.RE
780.TP
781\fB\-verbose \fIlevel\fR
782Sets the type of output verbosity desired to \fIlevel\fR,
783a list of zero or more of the elements \fBbody\fR, \fBpass\fR,
784\fBskip\fR, \fBstart\fR, \fBerror\fR and \fBline\fR.  Default value
785is \fB{body error}\fR.
786Levels are defined as:
787.RS
788.IP "body (b)"
789Display the body of failed tests
790.IP "pass (p)"
791Print output when a test passes
792.IP "skip (s)"
793Print output when a test is skipped
794.IP "start (t)"
795Print output whenever a test starts
796.IP "error (e)"
797Print errorInfo and errorCode, if they exist, when a test return code
798does not match its expected return code
799.IP "line (l)"
800Print source file line information of failed tests
801.RE
802The single letter abbreviations noted above are also recognized
803so that
804.QW "\fBconfigure \-verbose pt\fR"
805is the same as
806.QW "\fBconfigure \-verbose {pass start}\fR" .
807.TP
808\fB\-preservecore \fIlevel\fR
809Sets the core preservation level to \fIlevel\fR.  This level
810determines how stringent checks for core files are.  Default
811value is 0.  Levels are defined as:
812.RS
813.IP 0
814No checking \(em do not check for core files at the end of each test
815command, but do check for them in \fBrunAllTests\fR after all
816test files have been evaluated.
817.IP 1
818Also check for core files at the end of each \fBtest\fR command.
819.IP 2
820Check for core files at all times described above, and save a
821copy of each core file produced in \fBconfigure \-tmpdir\fR.
822.RE
823.TP
824\fB\-limitconstraints \fIboolean\fR
825Sets the mode by which \fBtest\fR honors constraints as described
826in \fBTESTS\fR above.  Default value is false.
827.TP
828\fB\-constraints \fIlist\fR
829Sets all the constraints in \fIlist\fR to true.  Also used in
830combination with \fBconfigure \-limitconstraints true\fR to control an
831alternative constraint mode as described in \fBTESTS\fR above.
832Default value is an empty list.
833.TP
834\fB\-tmpdir \fIdirectory\fR
835Sets the temporary directory to be used by \fBmakeFile\fR,
836\fBmakeDirectory\fR, \fBviewFile\fR, \fBremoveFile\fR,
837and \fBremoveDirectory\fR as the default directory where
838temporary files and directories created by test files should
839be created.  Default value is \fBworkingDirectory\fR.
840.TP
841\fB\-testdir \fIdirectory\fR
842Sets the directory searched by \fBrunAllTests\fR for test files
843and subdirectories.  Default value is \fBworkingDirectory\fR.
844.TP
845\fB\-file \fIpatternList\fR
846Sets the list of patterns used by \fBrunAllTests\fR to determine
847what test files to evaluate.  Default value is
848.QW \fB*.test\fR .
849.TP
850\fB\-notfile \fIpatternList\fR
851Sets the list of patterns used by \fBrunAllTests\fR to determine
852what test files to skip.  Default value is
853.QW \fBl.*.test\fR ,
854so that any SCCS lock files are skipped.
855.TP
856\fB\-relateddir \fIpatternList\fR
857Sets the list of patterns used by \fBrunAllTests\fR to determine
858what subdirectories to search for an \fBall.tcl\fR file.  Default
859value is
860.QW \fB*\fR .
861.TP
862\fB\-asidefromdir \fIpatternList\fR
863Sets the list of patterns used by \fBrunAllTests\fR to determine
864what subdirectories to skip when searching for an \fBall.tcl\fR file.
865Default value is an empty list.
866.TP
867\fB\-match \fIpatternList\fR
868Set the list of patterns used by \fBtest\fR to determine whether
869a test should be run.  Default value is
870.QW \fB*\fR .
871.TP
872\fB\-skip \fIpatternList\fR
873Set the list of patterns used by \fBtest\fR to determine whether
874a test should be skipped.  Default value is an empty list.
875.TP
876\fB\-load \fIscript\fR
877Sets a script to be evaluated by \fBloadTestedCommands\fR.
878Default value is an empty script.
879.TP
880\fB\-loadfile \fIfilename\fR
881Sets the filename from which to read a script to be evaluated
882by \fBloadTestedCommands\fR.  This is an alternative to
883\fB\-load\fR.  They cannot be used together.
884.TP
885\fB\-outfile \fIfilename\fR
886Sets the file to which all output produced by tcltest should be
887written.  A file named \fIfilename\fR will be \fBopen\fRed for writing,
888and the resulting channel will be set as the value of \fBoutputChannel\fR.
889.TP
890\fB\-errfile \fIfilename\fR
891Sets the file to which all error output produced by tcltest
892should be written.  A file named \fIfilename\fR will be \fBopen\fRed
893for writing, and the resulting channel will be set as the value
894of \fBerrorChannel\fR.
895.SH "CREATING TEST SUITES WITH TCLTEST"
896.PP
897The fundamental element of a test suite is the individual \fBtest\fR
898command.  We begin with several examples.
899.IP [1]
900Test of a script that returns normally.
901.RS
902.PP
903.CS
904\fBtest\fR example-1.0 {normal return} {
905    format %s value
906} value
907.CE
908.RE
909.IP [2]
910Test of a script that requires context setup and cleanup.  Note the
911bracing and indenting style that avoids any need for line continuation.
912.RS
913.PP
914.CS
915\fBtest\fR example-1.1 {test file existence} -setup {
916    set file [makeFile {} test]
917} -body {
918    file exists $file
919} -cleanup {
920    removeFile test
921} -result 1
922.CE
923.RE
924.IP [3]
925Test of a script that raises an error.
926.RS
927.PP
928.CS
929\fBtest\fR example-1.2 {error return} -body {
930    error message
931} -returnCodes error -result message
932.CE
933.RE
934.IP [4]
935Test with a constraint.
936.RS
937.PP
938.CS
939\fBtest\fR example-1.3 {user owns created files} -constraints {
940    unix
941} -setup {
942    set file [makeFile {} test]
943} -body {
944    file attributes $file -owner
945} -cleanup {
946    removeFile test
947} -result $::tcl_platform(user)
948.CE
949.RE
950.PP
951At the next higher layer of organization, several \fBtest\fR commands
952are gathered together into a single test file.  Test files should have
953names with the \fB.test\fR extension, because that is the default pattern
954used by \fBrunAllTests\fR to find test files.  It is a good rule of
955thumb to have one test file for each source code file of your project.
956It is good practice to edit the test file and the source code file
957together, keeping tests synchronized with code changes.
958.PP
959Most of the code in the test file should be the \fBtest\fR commands.
960Use constraints to skip tests, rather than conditional evaluation
961of \fBtest\fR.
962.IP [5]
963Recommended system for writing conditional tests, using constraints to
964guard:
965.RS
966.PP
967.CS
968\fBtestConstraint\fR X [expr $myRequirement]
969\fBtest\fR goodConditionalTest {} X {
970    # body
971} result
972.CE
973.RE
974.IP [6]
975Discouraged system for writing conditional tests, using \fBif\fR to
976guard:
977.RS
978.PP
979.CS
980if $myRequirement {
981    test badConditionalTest {} {
982        #body
983    } result
984}
985.CE
986.RE
987.PP
988Use the \fB\-setup\fR and \fB\-cleanup\fR options to establish and release
989all context requirements of the test body.  Do not make tests depend on
990prior tests in the file.  Those prior tests might be skipped.  If several
991consecutive tests require the same context, the appropriate setup
992and cleanup scripts may be stored in variable for passing to each tests
993\fB\-setup\fR and \fB\-cleanup\fR options.  This is a better solution than
994performing setup outside of \fBtest\fR commands, because the setup will
995only be done if necessary, and any errors during setup will be reported,
996and not cause the test file to abort.
997.PP
998A test file should be able to be combined with other test files and not
999interfere with them, even when \fBconfigure \-singleproc 1\fR causes
1000all files to be evaluated in a common interpreter.  A simple way to
1001achieve this is to have your tests define all their commands and variables
1002in a namespace that is deleted when the test file evaluation is complete.
1003A good namespace to use is a child namespace \fBtest\fR of the namespace
1004of the module you are testing.
1005.PP
1006A test file should also be able to be evaluated directly as a script,
1007not depending on being called by a master \fBrunAllTests\fR.  This
1008means that each test file should process command line arguments to give
1009the tester all the configuration control that \fBtcltest\fR provides.
1010.PP
1011After all \fBtest\fRs in a test file, the command \fBcleanupTests\fR
1012should be called.
1013.IP [7]
1014Here is a sketch of a sample test file illustrating those points:
1015.RS
1016.PP
1017.CS
1018package require tcltest 2.2
1019eval \fB::tcltest::configure\fR $argv
1020package require example
1021namespace eval ::example::test {
1022    namespace import ::tcltest::*
1023    \fBtestConstraint\fR X [expr {...}]
1024    variable SETUP {#common setup code}
1025    variable CLEANUP {#common cleanup code}
1026    \fBtest\fR example-1 {} -setup $SETUP -body {
1027        # First test
1028    } -cleanup $CLEANUP -result {...}
1029    \fBtest\fR example-2 {} -constraints X -setup $SETUP -body {
1030        # Second test; constrained
1031    } -cleanup $CLEANUP -result {...}
1032    \fBtest\fR example-3 {} {
1033        # Third test; no context required
1034    } {...}
1035    \fBcleanupTests\fR
1036}
1037namespace delete ::example::test
1038.CE
1039.RE
1040.PP
1041The next level of organization is a full test suite, made up of several
1042test files.  One script is used to control the entire suite.  The
1043basic function of this script is to call \fBrunAllTests\fR after
1044doing any necessary setup.  This script is usually named \fBall.tcl\fR
1045because that is the default name used by \fBrunAllTests\fR when combining
1046multiple test suites into one testing run.
1047.IP [8]
1048Here is a sketch of a sample test suite master script:
1049.RS
1050.PP
1051.CS
1052package require Tcl 8.4
1053package require tcltest 2.2
1054package require example
1055\fB::tcltest::configure\fR -testdir \e
1056        [file dirname [file normalize [info script]]]
1057eval \fB::tcltest::configure\fR $argv
1058\fB::tcltest::runAllTests\fR
1059.CE
1060.RE
1061.SH COMPATIBILITY
1062.PP
1063A number of commands and variables in the \fB::tcltest\fR namespace
1064provided by earlier releases of \fBtcltest\fR have not been documented
1065here.  They are no longer part of the supported public interface of
1066\fBtcltest\fR and should not be used in new test suites.  However,
1067to continue to support existing test suites written to the older
1068interface specifications, many of those deprecated commands and
1069variables still work as before.  For example, in many circumstances,
1070\fBconfigure\fR will be automatically called shortly after
1071\fBpackage require tcltest 2.1\fR succeeds with arguments
1072from the variable \fB::argv\fR.  This is to support test suites
1073that depend on the old behavior that \fBtcltest\fR was automatically
1074configured from command line arguments.  New test files should not
1075depend on this, but should explicitly include
1076.PP
1077.CS
1078eval \fB::tcltest::configure\fR $::argv
1079.CE
1080.PP
1081to establish a configuration from command line arguments.
1082.SH "KNOWN ISSUES"
1083There are two known issues related to nested evaluations of \fBtest\fR.
1084The first issue relates to the stack level in which test scripts are
1085executed.  Tests nested within other tests may be executed at the same
1086stack level as the outermost test.  For example, in the following code:
1087.PP
1088.CS
1089\fBtest\fR level-1.1 {level 1} {
1090    -body {
1091        \fBtest\fR level-2.1 {level 2} {
1092        }
1093    }
1094}
1095.CE
1096.PP
1097any script executed in level-2.1 may be executed at the same stack
1098level as the script defined for level-1.1. 
1099.PP
1100In addition, while two \fBtest\fRs have been run, results will only
1101be reported by \fBcleanupTests\fR for tests at the same level as
1102test level-1.1.  However, test results for all tests run prior to
1103level-1.1 will be available when test level-2.1 runs.  What this
1104means is that if you try to access the test results for test level-2.1,
1105it will may say that
1106.QW m
1107tests have run,
1108.QW n
1109tests have been skipped,
1110.QW o
1111tests have passed and
1112.QW p
1113tests have failed, where
1114.QW m ,
1115.QW n ,
1116.QW o ,
1117and
1118.QW p
1119refer to tests that were run at the same test level as test level-1.1.
1120.PP
1121Implementation of output and error comparison in the test command
1122depends on usage of ::puts in your application code.  Output is
1123intercepted by redefining the ::puts command while the defined test
1124script is being run.  Errors thrown by C procedures or printed
1125directly from C applications will not be caught by the test command.
1126Therefore, usage of the \fB\-output\fR and \fB\-errorOutput\fR
1127options to \fBtest\fR is useful only for pure Tcl applications
1128that use \fB::puts\fR to produce output.
1129.SH KEYWORDS
1130test, test harness, test suite
Note: See TracBrowser for help on using the repository browser.