2014-06-10 77 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

哎呀,是的。編輯。謝謝。 –

相關問題