|
||||||
SUMMARY: CHILDREN | PARAMETER | INSTPROC | INSTFILTER | INSTFORWARD | DETAIL: | INSTPROC |
::xotcl::Object | +--::xox::Node | +--::xounit::Test | +--::xounit::Assert
TestCase is a simple base class to be subclassed for your specific test. TestCase is a easy way to build an automated test for your code. Simply subclass TestCase as in the example below. Define some instprocs that start with test. Then override either setUp or tearDown instprocs. TestCase works by finding all the methods that start with "test" in your TestCase subclass. It then makes three method calls on your TestCase for each test instproc . First setUp, second testX (where X is the rest of your test instproc name), and third tearDown. TestCase catches any errors that your code might cause or assert failures that occur and builds a test report that can be printed as text, xml, or uploaded to a database. Example: package require XOTcl package require xounit namespace import -force ::xotcl::* Class SimpleTest -superclass ::xounit::TestCase SimpleTest instproc test1NotEquals0 {} { my assertNotEquals 1 0 } This can be run with the tcl commands: set aRunner [ ::xounit::TestRunnerTextUI new ] $aRunner runAllTests
Variables | |||
Name | Default Value | Class | Comment |
currentTestMethod |
::xounit::TestCase | name of the currently running test method. |
|
nodeName |
::xox::Node | ||
result |
::xounit::TestCase | the result for the current run. |
Methods | |
Name | Comment |
catchAndReport {script}
| catchAndReport is used to stop a failed assert from stopping the test altogether |
newResult {}
| Overrides newResult to save the result in the result parameter |
onFailure {testScript debugScript}
| Runs a debug script after a test script fails |
reportError {testResult errorResult test}
| Gathers the error information and adds information to a test result |
reportFailure {testResult errorResult test}
| Adds a failure to the test result with the AssertionError message |
run {testResult}
| run is used by the TestRunner to start the test methods in a TestCase |
runDependentTest {object test}
| Runs another test method on another object as a subtest to the currently running test |
runExternalTest {object test}
| Runs another test method on another object along with this test |
runIndependentTest {object test}
| Runs another test method on another object as as subtest of the current test |
runTearDown {testResult test}
| runs the tearDown method in a catch block and reports an error if one is caught |
runTest {testResult test}
| runTest is used by run to start a single test |
setUp {}
| setUp is called before each test method in your specific test case |
tearDown {}
| tearDown is called after each test method in your specific test case |
Methods from ::xotcl::Object |
#, ., ?, ?code, ?methods, ?object, abstract, copy, coverageFilter, defaultmethod, extractConfigureArg, filterappend, garbageCollect, get#, getClean#, hasclass, init, methodTag, mixinappend, move, profileFilter, self, setParameterDefaults, shell, tclcmd, traceFilter,
|
Instproc Detail |
catchAndReport is used to stop a failed assert from stopping the test altogether. catchAndReport forms blocks of the test that insulate the rest of the test from these blocks. Example: Class ATest -superclass ::xounit::TestCase ATest instproc testCatchAndReport { } { my assert 1 "I will pass" my catchAndReport { my assert 0 "I will fail, but not stop the test" } my assertEquals 2 2 "I will be run and pass" my fail "I stop the test not the my assert 0 ... " } #When this example is run it will report two failures, #not one.
script
::xounit::TestCase instproc catchAndReport {script} { set retValue [ catch "uplevel [ self callinglevel ] {$script}" result ] switch $retValue { 3 { error "break not supported in catchAndReport" } 4 { error "continue not supported in catchAndReport" } 2 { error "return not supported in catchAndReport" } 0 { return $result } 1 { if [ ::xoexception::Throwable isThrowable $result ::xounit::AssertionError ] { my reportFailure [ my result ] $result [ my currentTestMethod ] } else { my reportError [ my result ] $result [ my currentTestMethod ] } } default { error "$retValue not supported in catchAndReport" } } }
Overrides newResult to save the result in the result parameter.
::xounit::TestCase instproc newResult {} { set result [ ::xotcl::next ] ::xotcl::my result $result return $result }
Runs a debug script after a test script fails. Experimental
testScript
debugScript
::xounit::TestCase instproc onFailure {testScript debugScript} { set retCode [ catch { uplevel $testScript } result ] switch $retCode { 3 { error "break not supported in onFailure" } 4 { error "continue not supported in onFailure" } 2 { error "return not supported in onFailure" } 0 - 1 { uplevel $debugScript } } error $result }
Gathers the error information and adds information to a test result.
testResult
errorResult
test
::xounit::TestCase instproc reportError {testResult errorResult test} { global errorInfo set message [ ::xoexception::Throwable::extractMessage $errorResult ] append message "\n" append message $errorInfo $testResult addNewResult ::xounit::TestError -name [ $testResult name ] -test $test -error "$message" -testedMethod $test -testedClass [ ::xox::ObjectGraph findFirstImplementationClass [ self ] $test ] -testedObject [ self ] }
Adds a failure to the test result with the AssertionError message.
testResult
errorResult
test
::xounit::TestCase instproc reportFailure {testResult errorResult test} { set message [ $errorResult message ] $testResult addNewResult ::xounit::TestFailure -name [ $testResult name ] -test $test -error $message -testedMethod $test -testedClass [ ::xox::ObjectGraph findFirstImplementationClass [ self ] $test ] -testedObject [ self ] }
run is used by the TestRunner to start the test methods in a TestCase. run collects the pass, fail, error states of each test method and adds them to the TestResult object passed to run. This method is called automatically by the TestRunner so your TestCases should not call this method.
testResult
::xounit::TestCase instproc run {testResult} { set runTheseTests [ lsort [ ::xotcl::my info methods {test*} ] ] foreach test $runTheseTests { ::xotcl::my runTest $testResult $test } }
Runs another test method on another object as a subtest to the currently running test. If the subtest fails this test is stopped. The result of the subtest is recorded seperately in the result tree.
object
test
::xounit::TestCase instproc runDependentTest {object test} { my instvar result set passed 1 if { "$object" == "[ self ]" } { set passed [ my runTest $result $test ] } else { set newResult [ $object newResult ] set newResult [ $result addResult $newResult ] set passed [ $object runTest $newResult $test ] } my assert $passed "Dependent test [ $object name ] $test failed" }
Runs another test method on another object along with this test.
object
test
::xounit::TestCase instproc runExternalTest {object test} { my instvar result $object currentTestMethod $test $object result $result $object $test }
Runs another test method on another object as as subtest of the current test. The current test is run independently of pass or failure of the subtest. The result of the subtest is recorded seperately in the result tree.
object
test
::xounit::TestCase instproc runIndependentTest {object test} { my instvar result if { "$object" == "[ self ]" } { my runTest $result $test } else { set newResult [ $object newResult ] set newResult [ $result addResult $newResult ] $object runTest $newResult $test } return }
runs the tearDown method in a catch block and reports an error if one is caught.
testResult
test
::xounit::TestCase instproc runTearDown {testResult test} { if [ catch { ::xotcl::my tearDown } result ] { my reportError $testResult $result $test } }
runTest is used by run to start a single test. Again this should not be called by your TestCase.
testResult
test
::xounit::TestCase instproc runTest {testResult test} { set return 1 ::xoexception::try { ::xotcl::my currentTestMethod $test ::xotcl::my result $testResult ::xotcl::my setUp $testResult addNewResult ::xounit::TestPass -name [ $testResult name ] -test $test -return [ ::xotcl::my $test ] -testedMethod $test -testedClass [ ::xox::ObjectGraph findFirstImplementationClass [ self ] $test ] -testedObject [ self ] set return 1 } catch { ::xounit::AssertionError result } { my reportFailure $testResult $result $test set return 0 } catch { error result } { my reportError $testResult $result $test set return 0 } my runTearDown $testResult $test return $return }
setUp is called before each test method in your specific test case. This provides a clean test fixture for each test method. Over-ride setUp in your specific TestCase to configure your TestCase before each test. Example: SimpleTest instproc setUp {} { my set testValue 10 } SimpleTest instproc testValue {} { my assertEquals [ my set testValue ] 10 }
::xounit::TestCase instproc setUp {} { return }
tearDown is called after each test method in your specific test case. This allows you to clean up any resources that were used in your test methods. Override this method in your TestCase and it will automatically called after each test method.
::xounit::TestCase instproc tearDown {} { return }