Redirect test results for tcltest

后端 未结 2 481
醉话见心
醉话见心 2021-01-29 09:03

I like use tcltest in my daily testing tasks. But test results i can only see in console, so after each test run i need to switch to that console, to find results. Аccording to

2条回答
  •  独厮守ぢ
    2021-01-29 09:30

    You might want to simplify your setup and stay in Tcl? Redirecting stdout (which is used by tcltest under the hood) using Tcl's channel transforms is a convenient option; and has been covered here before.

    There are many variations to this theme, but you might want to get started by:

    Step 1: Define a Channel Interceptor

    oo::class create TcltestNotifier {
        variable buffer
        method initialize {handle mode} {
            if {$mode ne "write"} {error "can't handle reading"}
            return {finalize initialize write}
        }
        method finalize {handle} {
            # NOOP
        }
    
        method write {handle bytes} {
            append buffer $bytes
            return $bytes
        }
    
        method report {} {
            # sanitize the tcltest report for the notifier
            set buffer [string map {\" \\\"} $buffer]
            # dispatch to notifier (Mac OS X, change to your needs/ OS)
            append cmd "display notification \"$buffer\"" " "
            append cmd "with title \"Tcltest notification\""
            exec osascript -e $cmd
        }
    }
    

    The above snippet was derived/ stolen bluntly from Donal.

    Step 2: Register the interceptor with stdout around your tcltest suite

    package req tcltest
    namespace import ::tcltest::*
    
    set tn [TcltestNotifier new]
    chan push stdout $tn
    
    test mytest-0.1 "Fails!" -body {
        BARF!;
    } -result "?"
    
    chan pop stdout
    $tn report
    

    Some remarks

    • You can vary the granularity, request notification for each output line rather than the test report (then you have to dispatch to your notifier in the write method). But I doubt that this makes sense.
    • The example is built to run on Mac OS X (osascript), you have to modify it to your *nix tooling.

提交回复
热议问题