OSDN Git Service

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