tcltest.n

来自「tcl是工具命令语言」· N 代码 · 共 1,066 行 · 第 1/3 页

N
1,066
字号
the constraints that caused tests to be skipped, and thenumber of tests skipped for each is also printed.  Also,messages are printed if it appears that evaluation ofa test file has caused any temporary files to be leftbehind in [\fBconfigure -tmpdir\fR]..PPHaving completed and summarized all selected test files,[\fBrunAllTests\fR] then recursively acts on subdirectoriesof [\fBconfigure -testdir\fR].  All subdirectories thatmatch any of the patterns in [\fBconfigure -relateddir\fR]and do not match any of the patterns in[\fBconfigure -asidefromdir\fR] are examined.  Ifa file named \fBall.tcl\fR is found in such a directory,it will be [\fBsource\fR]d in the caller's context.Whether or not an examined directory contains an\fBall.tcl\fR file, its subdirectories are also scannedagainst the [\fBconfigure -relateddir\fR] and[\fBconfigure -asidefromdir\fR] patterns.  In this way,many directories in a directory tree can have all theirtest files evaluated by a single [\fBrunAllTests\fR]command..SH "CONFIGURABLE OPTIONS"The [\fBconfigure\fR] command is used to set and query the configurableoptions of \fBtcltest\fR.  The valid options are:.TP\fB-singleproc \fIboolean\fRControls whether or not [\fBrunAllTests\fR] spawns a child process foreach test file.  No spawning when \fIboolean\fR is true.  Defaultvalue is false..TP\fB-debug \fIlevel\fRSets the debug level to \fIlevel\fR, an integer value indicating howmuch debugging information should be printed to stdout.  Note thatdebug messages always go to stdout, independent of the value of[\fBconfigure -outfile\fR].  Default value is 0.  Levels are defined as:.RS.IP 0Do not display any debug information..IP 1Display information regarding whether a test is skipped because itdoesn't match any of the tests that were specified using by[\fBconfigure -match\fR] (userSpecifiedNonMatch) or matches any ofthe tests specified by [\fBconfigure -skip\fR] (userSpecifiedSkip).  Alsoprint warnings about possible lack of cleanup or balance in test files..IP 2Display the flag array parsed by the command line processor, thecontents of the ::env array, and all user-defined variables that existin the current namespace as they are used..IP 3Display information regarding what individual procs in the testharness are doing..RE.TP\fB-verbose \fIlevel\fRSets the type of output verbosity desired to \fIlevel\fR,a list of zero or more of the elements \fBbody\fR, \fBpass\fR,\fBskip\fR, \fBstart\fR, and \fBerror\fR.  Default value is \fBbody\fR.Levels are defined as: .RS.IP "body (b)"Display the body of failed tests.IP "pass (p)"Print output when a test passes.IP "skip (s)"Print output when a test is skipped.IP "start (t)"Print output whenever a test starts.IP "error (e)"Print errorInfo and errorCode, if they exist, when a test return codedoes not match its expected return code.REThe single letter abbreviations noted above are also recognizedso that [\fBconfigure -verbose pt\fR] is the same as[\fBconfigure -verbose  {pass start}\fR]..TP\fB-preservecore \fIlevel\fRSets the core preservation level to \fIlevel\fR.  This leveldetermines how stringent checks for core files are.  Defaultvalue is 0.  Levels are defined as:.RS.IP 0No checking - do not check for core files at the end of each testcommand, but do check for them in [\fBrunAllTests\fR] after alltest files have been evaluated..IP 1Also check for core files at the end of each [\fBtest\fR] command..IP 2Check for core files at all times described above, and save a copy of each core file produced in [\fBconfigure -tmpdir\fR]..RE.TP\fB-limitconstraints \fIboolean\fRSets the mode by which [\fBtest\fR] honors constraints as describedin \fBTESTS\fR above.  Default value is false..TP\fB-constraints \fIlist\fRSets all the constraints in \fIlist\fR to true.  Also used incombination with [\fBconfigure -limitconstraints true\fR] to control analternative constraint mode as described in \fBTESTS\fR above.Default value is an empty list..TP\fB-tmpdir \fIdirectory\fRSets the temporary directory to be used by [\fBmakeFile\fR],[\fBmakeDirectory\fR], [\fBviewFile\fR], [\fBremoveFile\fR], and [\fBremoveDirectory\fR] as the default directory wheretemporary files and directories created by test files shouldbe created.  Default value is [\fBworkingDirectory\fR]..TP\fB-testdir \fIdirectory\fRSets the directory searched by [\fBrunAllTests\fR] for test filesand subdirectories.  Default value is [\fBworkingDirectory\fR]..TP\fB-file \fIpatternList\fRSets the list of patterns used by [\fBrunAllTests\fR] to determinewhat test files to evaluate.  Default value is \fB*.test\fR..TP\fB-notfile \fIpatternList\fRSets the list of patterns used by [\fBrunAllTests\fR] to determinewhat test files to skip.  Default value is \fBl.*.test\fR, sothat any SCCS lock files are skipped..TP\fB-relateddir \fIpatternList\fRSets the list of patterns used by [\fBrunAllTests\fR] to determinewhat subdirectories to search for an \fBall.tcl\fR file.  Defaultvalue is \fB*\fR..TP\fB-asidefromdir \fIpatternList\fRSets the list of patterns used by [\fBrunAllTests\fR] to determinewhat subdirectories to skip when searching for an \fBall.tcl\fR file.Default value is an empty list..TP\fB-match \fIpatternList\fRSet the list of patterns used by [\fBtest\fR] to determine whethera test should be run.  Default value is \fB*\fR..TP\fB-skip \fIpatternList\fRSet the list of patterns used by [\fBtest\fR] to determine whethera test should be skipped.  Default value is an empty list..TP\fB-load \fIscript\fRSets a script to be evaluated by [\fBloadTestedCommands\fR].Default value is an empty script..TP\fB-loadfile \fIfilename\fRSets the filename from which to read a script to be evaluatedby [\fBloadTestedCommands\fR].  This is an alternative to\fB-load\fR.  They cannot be used together..TP\fB-outfile \fIfilename\fR Sets the file to which all output produced by tcltest should bewritten.  A file named \fIfilename\fR will be [\fBopen\fR]ed for writing,and the resulting channel will be set as the value of [\fBoutputChannel\fR]..TP\fB-errfile \fIfilename\fRSets the file to which all error output produced by tcltestshould be written.  A file named \fIfilename\fR will be [\fBopen\fR]edfor writing, and the resulting channel will be set as the valueof [\fBerrorChannel\fR]..SH "CREATING TEST SUITES WITH TCLTEST".PPThe fundamental element of a test suite is the individual [\fBtest\fR]command.  We begin with several examples..IP [1]Test of a script that returns normally..CStest example-1.0 {normal return} {    format %s value} value.CE.IP [2]Test of a script that requires context setup and cleanup.  Note thebracing and indenting style that avoids any need for line continuation..CStest example-1.1 {test file existence} -setup {    set file [makeFile {} test]} -body {    file exists $file} -cleanup {    removeFile test} -result 1.CE.IP [3]Test of a script that raises an error..CStest example-1.2 {error return} -body {    error message} -returnCodes error -result message.CE.IP [4]Test with a constraint..CStest example-1.3 {user owns created files} -constraints {    unix} -setup {    set file [makeFile {} test]} -body {    file attributes $file -owner} -cleanup {    removeFile test} -result $::tcl_platform(user).CE.PPAt the next higher layer of organization, several [\fBtest\fR] commandsare gathered together into a single test file.  Test files should havenames with the \fB.test\fR extension, because that is the default patternused by [\fBrunAllTests\fR] to find test files.  It is a good rule ofthumb to have one test file for each source code file of your project.It is good practice to edit the test file and the source code filetogether, keeping tests synchronized with code changes..PP Most of the code in the test file should be the [\fBtest\fR] commands.Use constraints to skip tests, rather than conditional evaluationof [\fBtest\fR].  That is, do this:.IP [5].CStestConstraint X [expr $myRequirement]test goodConditionalTest {} X {    # body} result.CEand do not do this:.IP [6].CSif $myRequirement {    test badConditionalTest {} {	#body    } result}.CE.PPUse the \fB-setup\fR and \fB-cleanup\fR options to establish and releaseall context requirements of the test body.  Do not make tests depend onprior tests in the file.  Those prior tests might be skipped.  If severalconsecutive tests require the same context, the appropriate setupand cleanup scripts may be stored in variable for passing to each tests\fB-setup\fR and \fB-cleanup\fR options.  This is a better solution thanperforming setup outside of [\fBtest\fR] commands, because the setup willonly be done if necessary, and any errors during setup will be reported,and not cause the test file to abort..PPA test file should be able to be combined with other test files and notinterfere with them, even when [\fBconfigure -singleproc 1\fR] causesall files to be evaluated in a common interpreter.  A simple way toachieve this is to have your tests define all their commands and variablesin a namespace that is deleted when the test file evaluation is complete.A good namespace to use is a child namespace \fBtest\fR of the namespaceof the module you are testing..PPA test file should also be able to be evaluated directly as a script,not depending on being called by a master [\fBrunAllTests\fR].  Thismeans that each test file should process command line arguments to givethe tester all the configuration control that \fBtcltest\fR provides..PPAfter all [\fBtest\fR]s in a test file, the command [\fBcleanupTests\fR]should be called..IP [7]Here is a sketch of a sample test file illustrating those points:.CSpackage require tcltest 2.2eval tcltest::configure $argvpackage require examplenamespace eval ::example::test {    namespace import ::tcltest::*    testConstraint X [expr {...}]    variable SETUP {#common setup code}    variable CLEANUP {#common cleanup code}    test example-1 {} -setup $SETUP {	# First test    } -cleanup $CLEANUP -result {...}    test example-2 {} -constraints X -setup $SETUP {	# Second test; constrained    } -cleanup $CLEANUP -result {...}    test example-3 {} {	# Third test; no context required    } {...}    cleanupTests}namespace delete ::example::test.CE.PPThe next level of organization is a full test suite, made up of severaltest files.  One script is used to control the entire suite.  Thebasic function of this script is to call [\fBrunAllTests\fR] afterdoing any necessary setup.  This script is usually named \fBall.tcl\fRbecause that's the default name used by [\fBrunAllTests\fR] when combiningmultiple test suites into one testing run..IP [8]Here is a sketch of a sample test suite master script:.CSpackage require Tcl 8.4package require tcltest 2.2package require exampletcltest::configure -testdir \        [file dirname [file normalize [info script]]]eval tcltest::configure $argvtcltest::runAllTests.CE.SH COMPATIBILITY.PPA number of commands and variables in the \fB::tcltest\fR namespaceprovided by earlier releases of \fBtcltest\fR have not been documentedhere.  They are no longer part of the supported public interface of\fBtcltest\fR and should not be used in new test suites.  However,to continue to support existing test suites written to the olderinterface specifications, many of those deprecated commands andvariables still work as before.  For example, in many circumstances,[\fBconfigure\fR] will be automatically called shortly after[\fBpackage require tcltest 2.1\fR] succeeds with argumentsfrom the variable \fB::argv\fR.  This is to support test suitesthat depend on the old behavior that \fBtcltest\fR was automaticallyconfigured from command line arguments.  New test files should notdepend on this, but should explicitly include.CSeval tcltest::configure $::argv.CEto establish a configuration from command line arguments..SH "KNOWN ISSUES"There are two known issues related to nested evaluations of [\fBtest\fR].  The first issue relates to the stack level in which test scripts areexecuted.  Tests nested within other tests may be executed at the samestack level as the outermost test.  For example, in the following code: .CStest level-1.1 {level 1} {    -body {        test level-2.1 {level 2} {        }    }}.CEany script executed in level-2.1 may be executed at the same stacklevel as the script defined for level-1.1.  .PPIn addition, while two [\fBtest\fR]s have been run, results will onlybe reported by [\fBcleanupTests\fR] for tests at the same level astest level-1.1.  However, test results for all tests run prior tolevel-1.1 will be available when test level-2.1 runs.  What thismeans is that if you try to access the test results for test level-2.1,it will may say that 'm' tests have run, 'n' tests havebeen skipped, 'o' tests have passed and 'p' tests have failed,where 'm', 'n', 'o', and 'p' refer to tests that were run at thesame test level as test level-1.1. .PPImplementation of output and error comparison in the test commanddepends on usage of puts in your application code.  Output isintercepted by redefining the puts command while the defined testscript is being run.  Errors thrown by C procedures or printeddirectly from C applications will not be caught by the test command.Therefore, usage of the \fB-output\fR and \fB-errorOuput\fRoptions to [\fBtest\fR] is useful only for pure Tcl applicationsthat use [\fBputs\fR] to produce output. .SH KEYWORDStest, test harness, test suite

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?