1tcltest(n) Tcl Bundled Packages tcltest(n)
2
3
4
5______________________________________________________________________________
6
8 tcltest - Test harness support code and utilities
9
11 package require tcltest ?2.3?
12
13 tcltest::test name description ?option value ...?
14 tcltest::test name description ?constraints? body result
15
16 tcltest::loadTestedCommands
17 tcltest::makeDirectory name ?directory?
18 tcltest::removeDirectory name ?directory?
19 tcltest::makeFile contents name ?directory?
20 tcltest::removeFile name ?directory?
21 tcltest::viewFile name ?directory?
22 tcltest::cleanupTests ?runningMultipleTests?
23 tcltest::runAllTests
24
25 tcltest::configure
26 tcltest::configure option
27 tcltest::configure option value ?option value ...?
28 tcltest::customMatch mode command
29 tcltest::testConstraint constraint ?value?
30 tcltest::outputChannel ?channelID?
31 tcltest::errorChannel ?channelID?
32 tcltest::interpreter ?interp?
33
34 tcltest::debug ?level?
35 tcltest::errorFile ?filename?
36 tcltest::limitConstraints ?boolean?
37 tcltest::loadFile ?filename?
38 tcltest::loadScript ?script?
39 tcltest::match ?patternList?
40 tcltest::matchDirectories ?patternList?
41 tcltest::matchFiles ?patternList?
42 tcltest::outputFile ?filename?
43 tcltest::preserveCore ?level?
44 tcltest::singleProcess ?boolean?
45 tcltest::skip ?patternList?
46 tcltest::skipDirectories ?patternList?
47 tcltest::skipFiles ?patternList?
48 tcltest::temporaryDirectory ?directory?
49 tcltest::testsDirectory ?directory?
50 tcltest::verbose ?level?
51
52 tcltest::test name description optionList
53 tcltest::bytestring string
54 tcltest::normalizeMsg msg
55 tcltest::normalizePath pathVar
56 tcltest::workingDirectory ?dir?
57_________________________________________________________________
58
60 The tcltest package provides several utility commands useful in the
61 construction of test suites for code instrumented to be run by evalua‐
62 tion of Tcl commands. Notably the built-in commands of the Tcl library
63 itself are tested by a test suite using the tcltest package.
64
65 All the commands provided by the tcltest package are defined in and
66 exported from the ::tcltest namespace, as indicated in the SYNOPSIS
67 above. In the following sections, all commands will be described by
68 their simple names, in the interest of brevity.
69
70 The central command of tcltest is test that defines and runs a test.
71 Testing with test involves evaluation of a Tcl script and comparing the
72 result to an expected result, as configured and controlled by a number
73 of options. Several other commands provided by tcltest govern the con‐
74 figuration of test and the collection of many test commands into test
75 suites.
76
77 See CREATING TEST SUITES WITH TCLTEST below for an extended example of
78 how to use the commands of tcltest to produce test suites for your Tcl-
79 enabled code.
80
82 test name description ?option value ...?
83 Defines and possibly runs a test with the name name and descrip‐
84 tion description. The name and description of a test are used
85 in messages reported by test during the test, as configured by
86 the options of tcltest. The remaining option value arguments to
87 test define the test, including the scripts to run, the condi‐
88 tions under which to run them, the expected result, and the
89 means by which the expected and actual results should be com‐
90 pared. See TESTS below for a complete description of the valid
91 options and how they define a test. The test command returns an
92 empty string.
93
94 test name description ?constraints? body result
95 This form of test is provided to support test suites written for
96 version 1 of the tcltest package, and also a simpler interface
97 for a common usage. It is the same as “test name description
98 -constraints constraints -body body -result result”. All other
99 options to test take their default values. When constraints is
100 omitted, this form of test can be distinguished from the first
101 because all options begin with “-”.
102
103 loadTestedCommands
104 Evaluates in the caller's context the script specified by con‐
105 figure -load or configure -loadfile. Returns the result of that
106 script evaluation, including any error raised by the script.
107 Use this command and the related configuration options to pro‐
108 vide the commands to be tested to the interpreter running the
109 test suite.
110
111 makeFile contents name ?directory?
112 Creates a file named name relative to directory directory and
113 write contents to that file using the encoding encoding system.
114 If contents does not end with a newline, a newline will be
115 appended so that the file named name does end with a newline.
116 Because the system encoding is used, this command is only suit‐
117 able for making text files. The file will be removed by the
118 next evaluation of cleanupTests, unless it is removed by remove‐
119 File first. The default value of directory is the directory
120 configure -tmpdir. Returns the full path of the file created.
121 Use this command to create any text file required by a test with
122 contents as needed.
123
124 removeFile name ?directory?
125 Forces the file referenced by name to be removed. This file
126 name should be relative to directory. The default value of
127 directory is the directory configure -tmpdir. Returns an empty
128 string. Use this command to delete files created by makeFile.
129
130 makeDirectory name ?directory?
131 Creates a directory named name relative to directory directory.
132 The directory will be removed by the next evaluation of
133 cleanupTests, unless it is removed by removeDirectory first.
134 The default value of directory is the directory configure
135 -tmpdir. Returns the full path of the directory created. Use
136 this command to create any directories that are required to
137 exist by a test.
138
139 removeDirectory name ?directory?
140 Forces the directory referenced by name to be removed. This
141 directory should be relative to directory. The default value of
142 directory is the directory configure -tmpdir. Returns an empty
143 string. Use this command to delete any directories created by
144 makeDirectory.
145
146 viewFile file ?directory?
147 Returns the contents of file, except for any final newline, just
148 as read -nonewline would return. This file name should be rela‐
149 tive to directory. The default value of directory is the direc‐
150 tory configure -tmpdir. Use this command as a convenient way to
151 turn the contents of a file generated by a test into the result
152 of that test for matching against an expected result. The con‐
153 tents of the file are read using the system encoding, so its
154 usefulness is limited to text files.
155
156 cleanupTests
157 Intended to clean up and summarize after several tests have been
158 run. Typically called once per test file, at the end of the
159 file after all tests have been completed. For best effective‐
160 ness, be sure that the cleanupTests is evaluated even if an
161 error occurs earlier in the test file evaluation.
162
163 Prints statistics about the tests run and removes files that
164 were created by makeDirectory and makeFile since the last
165 cleanupTests. Names of files and directories in the directory
166 configure -tmpdir created since the last cleanupTests, but not
167 created by makeFile or makeDirectory are printed to outputChan‐
168 nel. This command also restores the original shell environment,
169 as described by the ::env array. Returns an empty string.
170
171 runAllTests
172 This is a master command meant to run an entire suite of tests,
173 spanning multiple files and/or directories, as governed by the
174 configurable options of tcltest. See RUNNING ALL TESTS below
175 for a complete description of the many variations possible with
176 runAllTests.
177
179 configure
180 Returns the list of configurable options supported by tcltest.
181 See CONFIGURABLE OPTIONS below for the full list of options,
182 their valid values, and their effect on tcltest operations.
183
184 configure option
185 Returns the current value of the supported configurable option
186 option. Raises an error if option is not a supported config‐
187 urable option.
188
189 configure option value ?option value ...?
190 Sets the value of each configurable option option to the corre‐
191 sponding value value, in order. Raises an error if an option is
192 not a supported configurable option, or if value is not a valid
193 value for the corresponding option, or if a value is not pro‐
194 vided. When an error is raised, the operation of configure is
195 halted, and subsequent option value arguments are not processed.
196
197 If the environment variable ::env(TCLTEST_OPTIONS) exists when
198 the tcltest package is loaded (by package require tcltest) then
199 its value is taken as a list of arguments to pass to configure.
200 This allows the default values of the configuration options to
201 be set by the environment.
202
203 customMatch mode script
204 Registers mode as a new legal value of the -match option to
205 test. When the -match mode option is passed to test, the script
206 script will be evaluated to compare the actual result of evalu‐
207 ating the body of the test to the expected result. To perform
208 the match, the script is completed with two additional words,
209 the expected result, and the actual result, and the completed
210 script is evaluated in the global namespace. The completed
211 script is expected to return a boolean value indicating whether
212 or not the results match. The built-in matching modes of test
213 are exact, glob, and regexp.
214
215 testConstraint constraint ?boolean?
216 Sets or returns the boolean value associated with the named con‐
217 straint. See TEST CONSTRAINTS below for more information.
218
219 interpreter ?executableName?
220 Sets or returns the name of the executable to be execed by
221 runAllTests to run each test file when configure -singleproc is
222 false. The default value for interpreter is the name of the
223 currently running program as returned by info nameofexecutable.
224
225 outputChannel ?channelID?
226 Sets or returns the output channel ID. This defaults to stdout.
227 Any test that prints test related output should send that output
228 to outputChannel rather than letting that output default to std‐
229 out.
230
231 errorChannel ?channelID?
232 Sets or returns the error channel ID. This defaults to stderr.
233 Any test that prints error messages should send that output to
234 errorChannel rather than printing directly to stderr.
235
237 debug ?level?
238 Same as configure -debug ?level?.
239
240 errorFile ?filename?
241 Same as configure -errfile ?filename?.
242
243 limitConstraints ?boolean?
244 Same as configure -limitconstraints ?boolean?.
245
246 loadFile ?filename?
247 Same as configure -loadfile ?filename?.
248
249 loadScript ?script?
250 Same as configure -load ?script?.
251
252 match ?patternList?
253 Same as configure -match ?patternList?.
254
255 matchDirectories ?patternList?
256 Same as configure -relateddir ?patternList?.
257
258 matchFiles ?patternList?
259 Same as configure -file ?patternList?.
260
261 outputFile ?filename?
262 Same as configure -outfile ?filename?.
263
264 preserveCore ?level?
265 Same as configure -preservecore ?level?.
266
267 singleProcess ?boolean?
268 Same as configure -singleproc ?boolean?.
269
270 skip ?patternList?
271 Same as configure -skip ?patternList?.
272
273 skipDirectories ?patternList?
274 Same as configure -asidefromdir ?patternList?.
275
276 skipFiles ?patternList?
277 Same as configure -notfile ?patternList?.
278
279 temporaryDirectory ?directory?
280 Same as configure -tmpdir ?directory?.
281
282 testsDirectory ?directory?
283 Same as configure -testdir ?directory?.
284
285 verbose ?level?
286 Same as configure -verbose ?level?.
287
289 The remaining commands provided by tcltest have better alternatives
290 provided by tcltest or Tcl itself. They are retained to support exist‐
291 ing test suites, but should be avoided in new code.
292
293 test name description optionList
294 This form of test was provided to enable passing many options
295 spanning several lines to test as a single argument quoted by
296 braces, rather than needing to backslash quote the newlines
297 between arguments to test. The optionList argument is expected
298 to be a list with an even number of elements representing option
299 and value arguments to pass to test. However, these values are
300 not passed directly, as in the alternate forms of switch.
301 Instead, this form makes an unfortunate attempt to overthrow
302 Tcl's substitution rules by performing substitutions on some of
303 the list elements as an attempt to implement a “do what I mean”
304 interpretation of a brace-enclosed “block”. The result is
305 nearly impossible to document clearly, and for that reason this
306 form is not recommended. See the examples in CREATING TEST
307 SUITES WITH TCLTEST below to see that this form is really not
308 necessary to avoid backslash-quoted newlines. If you insist on
309 using this form, examine the source code of tcltest if you want
310 to know the substitution details, or just enclose the third
311 through last argument to test in braces and hope for the best.
312
313 workingDirectory ?directoryName?
314 Sets or returns the current working directory when the test
315 suite is running. The default value for workingDirectory is the
316 directory in which the test suite was launched. The Tcl com‐
317 mands cd and pwd are sufficient replacements.
318
319 normalizeMsg msg
320 Returns the result of removing the “extra” newlines from msg,
321 where “extra” is rather imprecise. Tcl offers plenty of string
322 processing commands to modify strings as you wish, and custom‐
323 Match allows flexible matching of actual and expected results.
324
325 normalizePath pathVar
326 Resolves symlinks in a path, thus creating a path without inter‐
327 nal redirection. It is assumed that pathVar is absolute. path‐
328 Var is modified in place. The Tcl command file normalize is a
329 sufficient replacement.
330
331 bytestring string
332 Construct a string that consists of the requested sequence of
333 bytes, as opposed to a string of properly formed UTF-8 charac‐
334 ters using the value supplied in string. This allows the tester
335 to create denormalized or improperly formed strings to pass to C
336 procedures that are supposed to accept strings with embedded
337 NULL types and confirm that a string result has a certain pat‐
338 tern of bytes. This is exactly equivalent to the Tcl command
339 encoding convertfrom identity.
340
342 The test command is the heart of the tcltest package. Its essential
343 function is to evaluate a Tcl script and compare the result with an
344 expected result. The options of test define the test script, the envi‐
345 ronment in which to evaluate it, the expected result, and how the com‐
346 pare the actual result to the expected result. Some configuration
347 options of tcltest also influence how test operates.
348
349 The valid options for test are summarized:
350
351 test name description
352 ?-constraints keywordList|expression?
353 ?-setup setupScript?
354 ?-body testScript?
355 ?-cleanup cleanupScript?
356 ?-result expectedAnswer?
357 ?-output expectedOutput?
358 ?-errorOutput expectedError?
359 ?-returnCodes codeList?
360 ?-match mode?
361
362 The name may be any string. It is conventional to choose a name
363 according to the pattern:
364
365 target-majorNum.minorNum
366
367 For white-box (regression) tests, the target should be the name of the
368 C function or Tcl procedure being tested. For black-box tests, the
369 target should be the name of the feature being tested. Some conven‐
370 tions call for the names of black-box tests to have the suffix _bb.
371 Related tests should share a major number. As a test suite evolves, it
372 is best to have the same test name continue to correspond to the same
373 test, so that it remains meaningful to say things like “Test foo-1.3
374 passed in all releases up to 3.4, but began failing in release 3.5.”
375
376 During evaluation of test, the name will be compared to the lists of
377 string matching patterns returned by configure -match, and configure
378 -skip. The test will be run only if name matches any of the patterns
379 from configure -match and matches none of the patterns from configure
380 -skip.
381
382 The description should be a short textual description of the test. The
383 description is included in output produced by the test, typically test
384 failure messages. Good description values should briefly explain the
385 purpose of the test to users of a test suite. The name of a Tcl or C
386 function being tested should be included in the description for regres‐
387 sion tests. If the test case exists to reproduce a bug, include the
388 bug ID in the description.
389
390 Valid attributes and associated values are:
391
392 -constraints keywordList|expression
393 The optional -constraints attribute can be list of one or more
394 keywords or an expression. If the -constraints value is a list
395 of keywords, each of these keywords should be the name of a con‐
396 straint defined by a call to testConstraint. If any of the
397 listed constraints is false or does not exist, the test is
398 skipped. If the -constraints value is an expression, that
399 expression is evaluated. If the expression evaluates to true,
400 then the test is run. Note that the expression form of -con‐
401 straints may interfere with the operation of configure -con‐
402 straints and configure -limitconstraints, and is not recom‐
403 mended. Appropriate constraints should be added to any tests
404 that should not always be run. That is, conditional evaluation
405 of a test should be accomplished by the -constraints option, not
406 by conditional evaluation of test. In that way, the same number
407 of tests are always reported by the test suite, though the num‐
408 ber skipped may change based on the testing environment. The
409 default value is an empty list. See TEST CONSTRAINTS below for
410 a list of built-in constraints and information on how to add
411 your own constraints.
412
413 -setup script
414 The optional -setup attribute indicates a script that will be
415 run before the script indicated by the -body attribute. If
416 evaluation of script raises an error, the test will fail. The
417 default value is an empty script.
418
419 -body script
420 The -body attribute indicates the script to run to carry out the
421 test. It must return a result that can be checked for correct‐
422 ness. If evaluation of script raises an error, the test will
423 fail. The default value is an empty script.
424
425 -cleanup script
426 The optional -cleanup attribute indicates a script that will be
427 run after the script indicated by the -body attribute. If eval‐
428 uation of script raises an error, the test will fail. The
429 default value is an empty script.
430
431 -match mode
432 The -match attribute determines how expected answers supplied by
433 -result, -output, and -errorOutput are compared. Valid values
434 for mode are regexp, glob, exact, and any value registered by a
435 prior call to customMatch. The default value is exact.
436
437 -result expectedValue
438 The -result attribute supplies the expectedValue against which
439 the return value from script will be compared. The default value
440 is an empty string.
441
442 -output expectedValue
443 The -output attribute supplies the expectedValue against which
444 any output sent to stdout or outputChannel during evaluation of
445 the script(s) will be compared. Note that only output printed
446 using ::puts is used for comparison. If -output is not speci‐
447 fied, output sent to stdout and outputChannel is not processed
448 for comparison.
449
450 -errorOutput expectedValue
451 The -errorOutput attribute supplies the expectedValue against
452 which any output sent to stderr or errorChannel during evalua‐
453 tion of the script(s) will be compared. Note that only output
454 printed using ::puts is used for comparison. If -errorOutput is
455 not specified, output sent to stderr and errorChannel is not
456 processed for comparison.
457
458 -returnCodes expectedCodeList
459 The optional -returnCodes attribute supplies expectedCodeList, a
460 list of return codes that may be accepted from evaluation of the
461 -body script. If evaluation of the -body script returns a code
462 not in the expectedCodeList, the test fails. All return codes
463 known to return, in both numeric and symbolic form, including
464 extended return codes, are acceptable elements in the expected‐
465 CodeList. Default value is “ok”return.
466
467 To pass, a test must successfully evaluate its -setup, -body, and
468 -cleanup scripts. The return code of the -body script and its result
469 must match expected values, and if specified, output and error data
470 from the test must match expected -output and -errorOutput values. If
471 any of these conditions are not met, then the test fails. Note that
472 all scripts are evaluated in the context of the caller of test.
473
474 As long as test is called with valid syntax and legal values for all
475 attributes, it will not raise an error. Test failures are instead
476 reported as output written to outputChannel. In default operation, a
477 successful test produces no output. The output messages produced by
478 test are controlled by the configure -verbose option as described in
479 CONFIGURABLE OPTIONS below. Any output produced by the test scripts
480 themselves should be produced using ::puts to outputChannel or error‐
481 Channel, so that users of the test suite may easily capture output with
482 the configure -outfile and configure -errfile options, and so that the
483 -output and -errorOutput attributes work properly.
484
486 Constraints are used to determine whether or not a test should be
487 skipped. Each constraint has a name, which may be any string, and a
488 boolean value. Each test has a -constraints value which is a list of
489 constraint names. There are two modes of constraint control. Most
490 frequently, the default mode is used, indicated by a setting of config‐
491 ure -limitconstraints to false. The test will run only if all con‐
492 straints in the list are true-valued. Thus, the -constraints option of
493 test is a convenient, symbolic way to define any conditions required
494 for the test to be possible or meaningful. For example, a test with
495 -constraints unix will only be run if the constraint unix is true,
496 which indicates the test suite is being run on a Unix platform.
497
498 Each test should include whatever -constraints are required to con‐
499 strain it to run only where appropriate. Several constraints are pre-
500 defined in the tcltest package, listed below. The registration of
501 user-defined constraints is performed by the testConstraint command.
502 User-defined constraints may appear within a test file, or within the
503 script specified by the configure -load or configure -loadfile options.
504
505 The following is a list of constraints pre-defined by the tcltest pack‐
506 age itself:
507
508 singleTestInterp
509 test can only be run if all test files are sourced into a single
510 interpreter
511
512 unix test can only be run on any Unix platform
513
514 win test can only be run on any Windows platform
515
516 nt test can only be run on any Windows NT platform
517
518 95 test can only be run on any Windows 95 platform
519
520 98 test can only be run on any Windows 98 platform
521
522 mac test can only be run on any Mac platform
523
524 unixOrWin
525 test can only be run on a Unix or Windows platform
526
527 macOrWin
528 test can only be run on a Mac or Windows platform
529
530 macOrUnix
531 test can only be run on a Mac or Unix platform
532
533 tempNotWin
534 test can not be run on Windows. This flag is used to temporar‐
535 ily disable a test.
536
537 tempNotMac
538 test can not be run on a Mac. This flag is used to temporarily
539 disable a test.
540
541 unixCrash
542 test crashes if it is run on Unix. This flag is used to tempo‐
543 rarily disable a test.
544
545 winCrash
546 test crashes if it is run on Windows. This flag is used to tem‐
547 porarily disable a test.
548
549 macCrash
550 test crashes if it is run on a Mac. This flag is used to tempo‐
551 rarily disable a test.
552
553 emptyTest
554 test is empty, and so not worth running, but it remains as a
555 place-holder for a test to be written in the future. This con‐
556 straint has value false to cause tests to be skipped unless the
557 user specifies otherwise.
558
559 knownBug
560 test is known to fail and the bug is not yet fixed. This con‐
561 straint has value false to cause tests to be skipped unless the
562 user specifies otherwise.
563
564 nonPortable
565 test can only be run in some known development environment.
566 Some tests are inherently non-portable because they depend on
567 things like word length, file system configuration, window man‐
568 ager, etc. This constraint has value false to cause tests to be
569 skipped unless the user specifies otherwise.
570
571 userInteraction
572 test requires interaction from the user. This constraint has
573 value false to causes tests to be skipped unless the user speci‐
574 fies otherwise.
575
576 interactive
577 test can only be run in if the interpreter is in interactive
578 mode (when the global tcl_interactive variable is set to 1).
579
580 nonBlockFiles
581 test can only be run if platform supports setting files into
582 nonblocking mode
583
584 asyncPipeClose
585 test can only be run if platform supports async flush and async
586 close on a pipe
587
588 unixExecs
589 test can only be run if this machine has Unix-style commands
590 cat, echo, sh, wc, rm, sleep, fgrep, ps, chmod, and mkdir avail‐
591 able
592
593 hasIsoLocale
594 test can only be run if can switch to an ISO locale
595
596 root test can only run if Unix user is root
597
598 notRoot
599 test can only run if Unix user is not root
600
601 eformat
602 test can only run if app has a working version of sprintf with
603 respect to the “e” format of floating-point numbers.
604
605 stdio test can only be run if interpreter can be opened as a pipe.
606
607 The alternative mode of constraint control is enabled by setting con‐
608 figure -limitconstraints to true. With that configuration setting, all
609 existing constraints other than those in the constraint list returned
610 by configure -constraints are set to false. When the value of config‐
611 ure -constraints is set, all those constraints are set to true. The
612 effect is that when both options configure -constraints and configure
613 -limitconstraints are in use, only those tests including only con‐
614 straints from the configure -constraints list are run; all others are
615 skipped. For example, one might set up a configuration with
616
617 configure -constraints knownBug \
618 -limitconstraints true \
619 -verbose pass
620
621 to run exactly those tests that exercise known bugs, and discover
622 whether any of them pass, indicating the bug had been fixed.
623
625 The single command runAllTests is evaluated to run an entire test
626 suite, spanning many files and directories. The configuration options
627 of tcltest control the precise operations. The runAllTests command
628 begins by printing a summary of its configuration to outputChannel.
629
630 Test files to be evaluated are sought in the directory configure -test‐
631 dir. The list of files in that directory that match any of the pat‐
632 terns in configure -file and match none of the patterns in configure
633 -notfile is generated and sorted. Then each file will be evaluated in
634 turn. If configure -singleproc is true, then each file will be sourced
635 in the caller's context. If it is false, then a copy of interpreter
636 will be exec'd to evaluate each file. The multi-process operation is
637 useful when testing can cause errors so severe that a process termi‐
638 nates. Although such an error may terminate a child process evaluating
639 one file, the master process can continue with the rest of the test
640 suite. In multi-process operation, the configuration of tcltest in the
641 master process is passed to the child processes as command line argu‐
642 ments, with the exception of configure -outfile. The runAllTests com‐
643 mand in the master process collects all output from the child processes
644 and collates their results into one master report. Any reports of
645 individual test failures, or messages requested by a configure -verbose
646 setting are passed directly on to outputChannel by the master process.
647
648 After evaluating all selected test files, a summary of the results is
649 printed to outputChannel. The summary includes the total number of
650 tests evaluated, broken down into those skipped, those passed, and
651 those failed. The summary also notes the number of files evaluated,
652 and the names of any files with failing tests or errors. A list of the
653 constraints that caused tests to be skipped, and the number of tests
654 skipped for each is also printed. Also, messages are printed if it
655 appears that evaluation of a test file has caused any temporary files
656 to be left behind in configure -tmpdir.
657
658 Having completed and summarized all selected test files, runAllTests
659 then recursively acts on subdirectories of configure -testdir. All
660 subdirectories that match any of the patterns in configure -relateddir
661 and do not match any of the patterns in configure -asidefromdir are
662 examined. If a file named all.tcl is found in such a directory, it
663 will be sourced in the caller's context. Whether or not an examined
664 directory contains an all.tcl file, its subdirectories are also scanned
665 against the configure -relateddir and configure -asidefromdir patterns.
666 In this way, many directories in a directory tree can have all their
667 test files evaluated by a single runAllTests command.
668
670 The configure command is used to set and query the configurable options
671 of tcltest. The valid options are:
672
673 -singleproc boolean
674 Controls whether or not runAllTests spawns a child process for
675 each test file. No spawning when boolean is true. Default
676 value is false.
677
678 -debug level
679 Sets the debug level to level, an integer value indicating how
680 much debugging information should be printed to stdout. Note
681 that debug messages always go to stdout, independent of the
682 value of configure -outfile. Default value is 0. Levels are
683 defined as:
684
685 0 Do not display any debug information.
686
687 1 Display information regarding whether a test is skipped
688 because it does not match any of the tests that were
689 specified using by configure -match (userSpecifiedNon‐
690 Match) or matches any of the tests specified by configure
691 -skip (userSpecifiedSkip). Also print warnings about
692 possible lack of cleanup or balance in test files. Also
693 print warnings about any re-use of test names.
694
695 2 Display the flag array parsed by the command line proces‐
696 sor, the contents of the ::env array, and all user-
697 defined variables that exist in the current namespace as
698 they are used.
699
700 3 Display information regarding what individual procs in
701 the test harness are doing.
702
703 -verbose level
704 Sets the type of output verbosity desired to level, a list of
705 zero or more of the elements body, pass, skip, start, error and
706 line. Default value is {body error}. Levels are defined as:
707
708 body (b)
709 Display the body of failed tests
710
711 pass (p)
712 Print output when a test passes
713
714 skip (s)
715 Print output when a test is skipped
716
717 start (t)
718 Print output whenever a test starts
719
720 error (e)
721 Print errorInfo and errorCode, if they exist, when a test
722 return code does not match its expected return code
723
724 line (l)
725 Print source file line information of failed tests
726 The single letter abbreviations noted above are also recognized so that
727 “configure -verbose pt” is the same as “configure -verbose {pass
728 start}”.
729
730 -preservecore level
731 Sets the core preservation level to level. This level deter‐
732 mines how stringent checks for core files are. Default value is
733 0. Levels are defined as:
734
735 0 No checking — do not check for core files at the end of
736 each test command, but do check for them in runAllTests
737 after all test files have been evaluated.
738
739 1 Also check for core files at the end of each test com‐
740 mand.
741
742 2 Check for core files at all times described above, and
743 save a copy of each core file produced in configure
744 -tmpdir.
745
746 -limitconstraints boolean
747 Sets the mode by which test honors constraints as described in
748 TESTS above. Default value is false.
749
750 -constraints list
751 Sets all the constraints in list to true. Also used in combina‐
752 tion with configure -limitconstraints true to control an alter‐
753 native constraint mode as described in TESTS above. Default
754 value is an empty list.
755
756 -tmpdir directory
757 Sets the temporary directory to be used by makeFile, makeDirec‐
758 tory, viewFile, removeFile, and removeDirectory as the default
759 directory where temporary files and directories created by test
760 files should be created. Default value is workingDirectory.
761
762 -testdir directory
763 Sets the directory searched by runAllTests for test files and
764 subdirectories. Default value is workingDirectory.
765
766 -file patternList
767 Sets the list of patterns used by runAllTests to determine what
768 test files to evaluate. Default value is “*.test”.
769
770 -notfile patternList
771 Sets the list of patterns used by runAllTests to determine what
772 test files to skip. Default value is “l.*.test”, so that any
773 SCCS lock files are skipped.
774
775 -relateddir patternList
776 Sets the list of patterns used by runAllTests to determine what
777 subdirectories to search for an all.tcl file. Default value is
778 “*”.
779
780 -asidefromdir patternList
781 Sets the list of patterns used by runAllTests to determine what
782 subdirectories to skip when searching for an all.tcl file.
783 Default value is an empty list.
784
785 -match patternList
786 Set the list of patterns used by test to determine whether a
787 test should be run. Default value is “*”.
788
789 -skip patternList
790 Set the list of patterns used by test to determine whether a
791 test should be skipped. Default value is an empty list.
792
793 -load script
794 Sets a script to be evaluated by loadTestedCommands. Default
795 value is an empty script.
796
797 -loadfile filename
798 Sets the filename from which to read a script to be evaluated by
799 loadTestedCommands. This is an alternative to -load. They can‐
800 not be used together.
801
802 -outfile filename
803 Sets the file to which all output produced by tcltest should be
804 written. A file named filename will be opened for writing, and
805 the resulting channel will be set as the value of outputChannel.
806
807 -errfile filename
808 Sets the file to which all error output produced by tcltest
809 should be written. A file named filename will be opened for
810 writing, and the resulting channel will be set as the value of
811 errorChannel.
812
814 The fundamental element of a test suite is the individual test command.
815 We begin with several examples.
816
817 [1] Test of a script that returns normally.
818
819 test example-1.0 {normal return} {
820 format %s value
821 } value
822
823 [2] Test of a script that requires context setup and cleanup. Note
824 the bracing and indenting style that avoids any need for line
825 continuation.
826
827 test example-1.1 {test file existence} -setup {
828 set file [makeFile {} test]
829 } -body {
830 file exists $file
831 } -cleanup {
832 removeFile test
833 } -result 1
834
835 [3] Test of a script that raises an error.
836
837 test example-1.2 {error return} -body {
838 error message
839 } -returnCodes error -result message
840
841 [4] Test with a constraint.
842
843 test example-1.3 {user owns created files} -constraints {
844 unix
845 } -setup {
846 set file [makeFile {} test]
847 } -body {
848 file attributes $file -owner
849 } -cleanup {
850 removeFile test
851 } -result $::tcl_platform(user)
852
853 At the next higher layer of organization, several test commands are
854 gathered together into a single test file. Test files should have
855 names with the .test extension, because that is the default pattern
856 used by runAllTests to find test files. It is a good rule of thumb to
857 have one test file for each source code file of your project. It is
858 good practice to edit the test file and the source code file together,
859 keeping tests synchronized with code changes.
860
861 Most of the code in the test file should be the test commands. Use
862 constraints to skip tests, rather than conditional evaluation of test.
863
864 [5] Recommended system for writing conditional tests, using con‐
865 straints to guard:
866
867 testConstraint X [expr $myRequirement]
868 test goodConditionalTest {} X {
869 # body
870 } result
871
872 [6] Discouraged system for writing conditional tests, using if to
873 guard:
874
875 if $myRequirement {
876 test badConditionalTest {} {
877 #body
878 } result
879 }
880
881 Use the -setup and -cleanup options to establish and release all con‐
882 text requirements of the test body. Do not make tests depend on prior
883 tests in the file. Those prior tests might be skipped. If several
884 consecutive tests require the same context, the appropriate setup and
885 cleanup scripts may be stored in variable for passing to each tests
886 -setup and -cleanup options. This is a better solution than performing
887 setup outside of test commands, because the setup will only be done if
888 necessary, and any errors during setup will be reported, and not cause
889 the test file to abort.
890
891 A test file should be able to be combined with other test files and not
892 interfere with them, even when configure -singleproc 1 causes all files
893 to be evaluated in a common interpreter. A simple way to achieve this
894 is to have your tests define all their commands and variables in a
895 namespace that is deleted when the test file evaluation is complete. A
896 good namespace to use is a child namespace test of the namespace of the
897 module you are testing.
898
899 A test file should also be able to be evaluated directly as a script,
900 not depending on being called by a master runAllTests. This means that
901 each test file should process command line arguments to give the tester
902 all the configuration control that tcltest provides.
903
904 After all tests in a test file, the command cleanupTests should be
905 called.
906
907 [7] Here is a sketch of a sample test file illustrating those
908 points:
909
910 package require tcltest 2.2
911 eval ::tcltest::configure $argv
912 package require example
913 namespace eval ::example::test {
914 namespace import ::tcltest::*
915 testConstraint X [expr {...}]
916 variable SETUP {#common setup code}
917 variable CLEANUP {#common cleanup code}
918 test example-1 {} -setup $SETUP -body {
919 # First test
920 } -cleanup $CLEANUP -result {...}
921 test example-2 {} -constraints X -setup $SETUP -body {
922 # Second test; constrained
923 } -cleanup $CLEANUP -result {...}
924 test example-3 {} {
925 # Third test; no context required
926 } {...}
927 cleanupTests
928 }
929 namespace delete ::example::test
930
931 The next level of organization is a full test suite, made up of several
932 test files. One script is used to control the entire suite. The basic
933 function of this script is to call runAllTests after doing any neces‐
934 sary setup. This script is usually named all.tcl because that is the
935 default name used by runAllTests when combining multiple test suites
936 into one testing run.
937
938 [8] Here is a sketch of a sample test suite master script:
939
940 package require Tcl 8.4
941 package require tcltest 2.2
942 package require example
943 ::tcltest::configure -testdir \
944 [file dirname [file normalize [info script]]]
945 eval ::tcltest::configure $argv
946 ::tcltest::runAllTests
947
949 A number of commands and variables in the ::tcltest namespace provided
950 by earlier releases of tcltest have not been documented here. They are
951 no longer part of the supported public interface of tcltest and should
952 not be used in new test suites. However, to continue to support exist‐
953 ing test suites written to the older interface specifications, many of
954 those deprecated commands and variables still work as before. For
955 example, in many circumstances, configure will be automatically called
956 shortly after package require tcltest 2.1 succeeds with arguments from
957 the variable ::argv. This is to support test suites that depend on the
958 old behavior that tcltest was automatically configured from command
959 line arguments. New test files should not depend on this, but should
960 explicitly include
961
962 eval ::tcltest::configure $::argv
963
964 to establish a configuration from command line arguments.
965
967 There are two known issues related to nested evaluations of test. The
968 first issue relates to the stack level in which test scripts are exe‐
969 cuted. Tests nested within other tests may be executed at the same
970 stack level as the outermost test. For example, in the following code:
971
972 test level-1.1 {level 1} {
973 -body {
974 test level-2.1 {level 2} {
975 }
976 }
977 }
978
979 any script executed in level-2.1 may be executed at the same stack
980 level as the script defined for level-1.1.
981
982 In addition, while two tests have been run, results will only be
983 reported by cleanupTests for tests at the same level as test level-1.1.
984 However, test results for all tests run prior to level-1.1 will be
985 available when test level-2.1 runs. What this means is that if you try
986 to access the test results for test level-2.1, it will may say that “m”
987 tests have run, “n” tests have been skipped, “o” tests have passed and
988 “p” tests have failed, where “m”, “n”, “o”, and “p” refer to tests that
989 were run at the same test level as test level-1.1.
990
991 Implementation of output and error comparison in the test command
992 depends on usage of ::puts in your application code. Output is inter‐
993 cepted by redefining the ::puts command while the defined test script
994 is being run. Errors thrown by C procedures or printed directly from C
995 applications will not be caught by the test command. Therefore, usage
996 of the -output and -errorOutput options to test is useful only for pure
997 Tcl applications that use ::puts to produce output.
998
1000 test, test harness, test suite
1001
1002
1003
1004tcltest 2.3 tcltest(n)