::xounit
Class TestCase

Heritage:
::xotcl::Object
  |
  +--::xox::Node
        |
        +--::xounit::Test
              |
              +--::xounit::Assert
Direct Known Subclasses:
::xodocument::test::TestHtmlDocument, ::xodocument::test::TestTclHtmlFormatter, ::xoexception::test::TestException, ::xoexception::test::TestExtractMessage, ::xoexception::test::TestIsException, ::xoexception::test::TestProcTry, ::xoexception::test::TestThrowable, ::xoexception::test::TestTry, ::xounit::Application, ::xounit::test::ErrorTest, ::xounit::test::FailureTest, ::xounit::test::NamespaceTest, ::xounit::test::TestAction, ::xounit::test::TestApplication, ::xounit::test::TestAssert, ::xounit::test::TestAssertError, ::xounit::test::TestAssertFindIn, ::xounit::test::TestAssertRegex, ::xounit::test::TestCatchAndReport, ::xounit::test::TestCatchException, ::xounit::test::TestNewResult, ::xounit::test::TestTest, ::xounit::test::TestTestCase, ::xounit::test::TestTestCaseHierarchy, ::xounit::test::TestTestResult, ::xounit::test::TestTestResultPersistence, ::xounit::test::TestTestResultsTextFormatter, ::xounit::test::TestTestRunner, ::xounit::test::TestTestSuite, ::xounit::test::TestTestSuiteContinuous, ::xounit::test::TestWebFormatter, ::xounit::test::namespace::NamespaceTest2, ::xox::test::TestClass, ::xox::test::TestClassParameter, ::xox::test::TestClassParameterArray, ::xox::test::TestDebugging, ::xox::test::TestEnvironment, ::xox::test::TestGenerateVariable, ::xox::test::TestHigherOrderTcl, ::xox::test::TestLogger, ::xox::test::TestNode, ::xox::test::TestObject, ::xox::test::TestObjectGraph, ::xox::test::TestPackage, ::xox::test::TestParseArgs, ::xox::test::TestReload, ::xox::test::TestSimpleXmlNodeWriter, ::xox::test::TestSpy, ::xox::test::TestTrace, ::xox::test::TestXmlNodeReader, ::xox::test::TestXmlNodeWriter,

Associated Test:
::xounit::test::TestTestCase

Class TestCase
superclass ::xounit::Assert,::xounit::Test,::xox::Node,
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
NameDefault ValueClassComment
currentTestMethod    ::xounit::TestCase
 name of the currently running test method.
nodeName    ::xox::Node
 
result    ::xounit::TestCase
 the result for the current run.
 
Methods
NameComment
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

Description:
 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.
Arguments:
Code:
  ::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" }
        }
    
}

newResult

Description:
 Overrides newResult to save the result in the
 result parameter.
Overrides:
newResult in ::xounit::Test
Code:
  ::xounit::TestCase instproc newResult {}  {
   

        set result [ ::xotcl::next ]
        ::xotcl::my result $result
        return $result
    
}

onFailure

Description:
 Runs a debug script after a test script fails.

 Experimental
Arguments:
Code:
  ::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
    
}

reportError

Description:
 Gathers the error information and adds information
 to a test result.
Arguments:
Code:
  ::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 ] 

    
}

reportFailure

Description:
 Adds a failure to the test result with the 
 AssertionError message.
Arguments:
Code:
  ::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

Description:
 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.
Arguments:
Overrides:
run in ::xounit::Test
Code:
  ::xounit::TestCase instproc run {testResult}  {
   

        set runTheseTests [ lsort [ ::xotcl::my info methods {test*} ] ]

        foreach test $runTheseTests  {

            ::xotcl::my runTest $testResult $test
        }
    
}

runDependentTest

Description:
 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.
Arguments:
Code:
  ::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"
    
}

runExternalTest

Description:
 Runs another test method on another object along 
 with this test.
Arguments:
Code:
  ::xounit::TestCase instproc runExternalTest {object test}  {
   

        my instvar result 
        $object currentTestMethod $test
        $object result $result
        $object $test
    
}

runIndependentTest

Description:
 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.
Arguments:
Code:
  ::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 
    
}

runTearDown

Description:
 runs the tearDown method in a catch block
 and reports an error if one is caught.
Arguments:
Code:
  ::xounit::TestCase instproc runTearDown {testResult test}  {
   

        if [ catch {

            ::xotcl::my tearDown

        } result ] {
            
            my reportError $testResult $result $test
        }
    
}

runTest

Description:
 runTest is used by run to start a single test.
 Again this should not be called by your TestCase.
Arguments:
Code:
  ::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

Description:
 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
 }
Code:
  ::xounit::TestCase instproc setUp {}  {
    return 
}

tearDown

Description:
 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.
Code:
  ::xounit::TestCase instproc tearDown {}  {
    return 
}