2017-09-05 85 views
1

我想获取当前在Tcl脚本中执行的tcltesttest的名称。例如,下面的代码应该是打印测试的名称:正在执行的tcltest测试的名称

#!/usr/bin/tclsh 
package require tcltest 

tcltest::test testFunction {} -body { 
    puts [lindex [info level 0] 0] 
    set a 1 
} -result 1 

但是,它提供了以下错误:

==== testFunction FAILED 
==== Contents of test case: 

    puts [lindex [info level 0] 0] 
    set a 1 

---- Test generated error; Return code was: 1 
---- Return code should have been one of: 0 2 
---- errorInfo: bad level "0" 
    while executing 
"info level 0" 
    ("uplevel" body line 2) 
    invoked from within 
"uplevel 1 $script" 
---- errorCode: TCL LOOKUP STACK_LEVEL 0 
==== testFunction FAILED 

必须有一些基本的错误,我做的,任何帮助对此非常感谢。

+0

用的是什么情况下通过内省得到这个?在我看来,一个好的测试机构不需要知道(在tcltest中有临时文件和目录的其他工具,所以这些命名是不存在问题的)。 –

+0

@DonalFellows:你可能是对的,内省似乎没有多大用处。实际上,测试用例需要知道它的名字,因为我想生成一个报告,其中测试用例的名称将是一个参数,实际上,报告可能包含从测试套件运行生成的多个测试用例信息。由于测试用例的名称不会经常更改(至少对于这个问题至少会改变一次),所以我认为对名称进行硬编码是一个更好的选择。 –

回答

0

我一直没能找到任何这样的内省(看起来不太难)。我看过,因为我想记录运行测试并标记从一个测试到另一个测试的过渡。

当然,没有什么能够阻止你知道测试名称。

namespace eval ::testname { 
    variable name foo 
    variable major 1 
    variable minor 0 
    proc curname {} { 
     variable name 
     variable major 
     variable minor 
     return $name-$major.$minor 
    } 
} 

set ::testname::name testfunction 

::tcltest::test [::testname::curname] {} -body { 
    puts [::testname::curname] 
} -cleanup { 
    incr ::testname::minor 
} 

::tcltest::test [::testname::curname] {} -body { 
    puts [::testname::curname] 
} -cleanup { 
    incr ::testname::minor 
} 

oo::object create testname 
oo::objdefine testname { 
    variable name major minor 
    method init {nam {maj 1} {min 0}} { 
     set name $nam 
     set major $maj 
     set minor $min 
    } 
    method name val {set name $val} 
    method major val {set minor 0 ; set major $val} 
    method minor val {set minor $val} 
    method bump {} {incr minor} 
    method curname {} {return $name-$major.$minor} 
} 

testname init testfunction 

::tcltest::test [testname curname] {} -body { 
    puts [testname curname] 
} -cleanup { 
    testname bump 
} 

::tcltest::test [testname curname] {} -body { 
    puts [testname curname] 
} -cleanup { 
    testname bump 
} 

(这是即兴代码,不是我实际使用,但它是一次测试,并做至少运行。)

文档: create (method of oo::class)incrmethod (object configuration subcommand), namespace, oo::objdefine, oo::objectpackageprocputsreturnsettcltest (package)variable (object slot subcommand)variable

+0

我从来没有为我写的测试使用自动生成的名称。这是因为我希望能够将测试名称用作我在处理失败时可能会遇到的问题。 –

+0

@DonalFellows:基本上这就是我为什么不使用它们的原因。在我回答中提到的情况下,我最终使用了重复测试名称的日志行。内省测试名称的能力会有帮助。 –

+0

@PeterLewerin:这确实是自动生成测试名称的一个很好的例子,但我不能说我正在寻找这样的东西。我看到它的方式并不完全是对测试名称的内省的例子,而是像从第一个用来提供名称的相同源中获取名称。它有点类似于在两个地方对名称进行硬编码(因为我们不需要自动生成的名称),这可能是我的场景中最简单的解决方案。 –