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