0
votes

I am using tcltest to write some simple unit tests for my TCL procs.
I'm wondering if there is a way to make a variable from the -setup script visible inside the -body script? Maybe using upvar/uplevel somehow?
So far I have tried the naive approach which does produce an error ("Test file error: can't read "N": no such variable"):

tcltest::test equal4_test {
} -setup {
    set N 4
} -body {
    equal4 $N
} -result 1
1
Can't reproduce: the code runs fine here. This is the typical way to prepare a variable for a test. There must be some other problem in your code. - Peter Lewerin

1 Answers

1
votes

Variables in tcltest -setup and -body scripts are evaluated in the same context, which is the exactly same context as the context that calls tcltest::test. Any defined -cleanup script is also evaluated in that context. Internally, Tcl's uplevel command is used to enforce this, and lots of Tcl's own test suite depends on this so we're pretty sure that it works.

Something else is going on. Are you running this code in a namespace? If you are, you probably want to create the variable with:

variable N 4

instead of:

set N 4

to avoid a horrible misfeature of Tcl's (arcane-in-this-respect) variable resolution rules.