2014-06-10 72 views
1

我有很多* .t文件,每个文件都有测试编号 我正在通过Test :: Harness执行所有* .t文件。 我怎么能写单独的测试状态(通过/失败的DB)个人测试结果到数据库

例如

use Test::More ; 
use strict; 
ok($foo eq $bar, "TestCase1 ") ? &subUpdateResult(pass) : &subUpdateResult(Fail) ; 
ok($1 eq $2, 'test case 2'); 
ok($3 eq $4, 'test case 3'); 
sub subUpdateResult 
{ 
#now a only dummy code I will update this code to connect DB later 
my $val=sfift; 
print "val is $val\n"; 
} 
done_testing(); 

,但我得到的结果一样

ok 1 - TestCase1 
ok 2 
val is sfift 
ok 3 - test case 2 
ok 4 - test case 3 
1..4 

查询:为什么我收到的打印测试用例2后的结果?以及如何获得单独的测试状态,这样我可以更新数据库或写入到Excel文件

+3

'sfift'?顺便说一句,这甚至不会在'strict'下编译,因为你声称是这样的。 –

+0

,你是对我有手工编写这些代码。如果我用shift然后还是我收到错误,如确定1个 VAL为1 OK 2 - 1 确定3 - 测试用例2 OK 4 - 测试用例3 1 .4 #你给你的测试命名为'1'。您不应该使用数字作为测试名称。 #很混乱。 – user3714347

+1

sfift是5.20.0中新增的:-) –

回答

1
  1. shiftsfift

  2. 它给你错误的部分是

    sub ok { 
    my($self, $test, $name) = @_; 
    
    # $test might contain an object which we don't want to accidentally 
    # store, so we turn it into a boolean. 
    $test = $test ? 1 : 0; 
    
    unless($Have_Plan) { 
        require Carp; 
        Carp::croak("You tried to run a test without a plan! Gotta have a plan."); 
    } 
    
    lock $Curr_Test; 
    $Curr_Test++; 
    
    $self->diag(<<ERR) if defined $name and $name =~ /^[\d\s]+$/; 
    You named your test '$name'. You shouldn't use numbers for your test names. 
    

这是错误是来自,$name论证未来不能只有数字和空格。您需要更正脚本的第3行,使用连接运算符。 (请参阅:How do I interpolate a line number from __LINE__ into the name of a test in Perl?

您的代码甚至编译不好,我认为您提供不同的数据为了不公开您的代码。我修改它,如下所示,它的工作原理。你必须做类似的事情。我也不知道$1$2是为了什么?

 #!/usr/local/bin/perl 
     use warnings; 
     use Test::More ; 
     use strict; 
     my $foo = "something"; 
     my $bar = "something"; 
     ok($foo eq $bar, "TestCase1 ") ? &subUpdateResult('pass') : &subUpdateResult('fail') ; 
     ok($1 eq $2, 'test case 2'); 
     ok($3 eq $4, 'test case 3'); 
     sub subUpdateResult 
     { 
     #now a only dummy code I will update this code to connect DB later 
     my $val=shift; 
     print "val is $val\n"; 
     } 
     done_testing(); 
+0

'$ name参数不能包含数字' - 它可以包含数字。它不能只是数字和空格。 – RobEarl

+0

哎呀,是的。编辑。谢谢。 –

相关问题