如何从 TCL 中的测试文件中获取测试文件的名称

How to get name of test file from within test file in TCL

如何从测试中获取正在测试的测试文件的名称运行?检查 Google 后,我似乎找不到答案。澄清一下:

test mytest-1.1 {
   my test tests ... 
} -body {
    # Command to get name of file we're currently in.
} -result 0

我去获取我当前所在评论所在的.test 文件的名称。

注意:这不是重复问题,因为我不需要测试的名称,而是测试文件的名称。

这是对 . Study the info frame documentation 中已经涵盖的内容的轻微扩展,请注意 file 源类型框架的记录:

[dict get $frameInfo file]

扩展示例:

package req tcltest

proc example {} {
    for {set i 1} {$i<=[info frame]} {incr i} {
    set frameInfo [info frame $i]
    set frameType [dict get $frameInfo type]
    set cmd [dict get $frameInfo cmd]

    if {$frameType eq "source" && [lindex $cmd 0] eq "tcltest::test"} {
        puts $frameInfo
        puts "Called from [lindex $cmd 1] defined in '[dict get $frameInfo file]'"
        return ok
    }
    }

    return notok

}

tcltest::test foo-1.1 {testing the foo} {
    example
} ok

更新

为了单独打印出当前最内层评估的(source'd)文件,您也可以只调用 [info script] 或使用从 [info frame 0] 获得的帧信息来自测试体内。

tcltest::test foo-1.1 {testing the foo} {
    puts [info script]
    # puts [dict get [info frame 0] file]; # context dependent
} ok