gpt4 book ai didi

perl - Test::Most - 使用堆栈跟踪报告失败的测试

转载 作者:行者123 更新时间:2023-12-04 23:48:48 25 4
gpt4 key购买 nike

我正在修复一个大型测试脚本(> 1000 行),该脚本使用一些实用方法(也> 1000 行)对各种初始数据设置执行重复测试。这有助于整合代码。但是,当测试失败时,它会报告实用程序方法内部的行号,从而很难追踪哪个测试失败了。

是否可以配置 Test::Most 当测试失败时给出一个堆栈跟踪而不是一个单一的行号?

#!/usr/bin/env perl
use strict;
use warnings;
use autodie;

use Test::Most tests => 3;

ok(1, 'first test');

note "The following includes a failed test, but a stack trace would be more helpful";

helper_sub_with_test(); # Line 13

ok(1, 'third test');

sub helper_sub_with_test {
ok(0, "second test"); # Line 17
}

输出:
$ perl scratch.pl 
1..3
ok 1 - first test
# The following includes a failed test, but a stack trace would be more helpful
not ok 2 - second test
# Failed test 'second test'
# at scratch.pl line 17.
ok 3 - third test
# Looks like you failed 1 test of 3.

如您所见,如果失败的测试在多次调用实用程序方法时报告第 17 行和第 13 行,将会很有帮助。

最佳答案

我不相信 Test::More 基础设施提供了这样的野兽,但你真的需要堆栈跟踪吗?如果您为测试提供描述性名称,仅报告第 13 行就足够了。

要报告第 13 行而不是第 17 行,只需将以下内容添加到您的子文件中:

local $Test::Builder::Level = $Test::Builder::Level + 1;

更长的例子:
#!/usr/bin/env perl
use strict;
use warnings;
use autodie;

use Test::Most tests => 3;

ok(1, 'first test');

note "The following includes a failed test, but a stack trace would be more helpful";

helper_sub_with_test(); # Line 13

ok(1, 'third test');

sub helper_sub_with_test {
local $Test::Builder::Level = $Test::Builder::Level + 1;
ok(0, sprintf "second test (look at line %d)", __LINE__); # Line 18
}

关于perl - Test::Most - 使用堆栈跟踪报告失败的测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27750141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com