如何从Perl的Test :: Simple重定向测试输出?

时间:2009-08-28 06:14:44

标签: perl

我正在编写一个简单的测试自动化套件,它会告诉我传递和失败的测试用例的数量。下面是一个描述代码的简单示例:

#! /usr/bin/perl -w

use Test::Simple tests => 1;

print "Enter the name of the Book: ";
$name = <STDIN>;
chomp($name);

print "You have entered $name \n";

$ori_name = "TextBook";

chomp($ori_name);

ok($name eq $ori_name, 'Checking name');

输入“TextBox”后输入的输出如下:

1..1
Enter the name of the Book: TextBook
You have entered TextBook
ok 1 - Checking name

我想将相同的输出重定向到一个看起来像

的文件
ok 1 - Checking name

如果我添加以下子程序

log_message (ok ($name eq $ori_name, 'Checking name');


sub log_message
{
    my $message = @_;

    open(DA, '>>PJ.txt') or die "Couldn't open file PJ.txt";

    print DA $message;

    close (DA);
}

然后我得到'1'或'0' - 而不是我想要的文字。

我应该如何处理,以便我的代码结果重定向到一个应该具有以下格式的文件:

ok 1 - Checking name

ok 2 - Checking others

等等?

3 个答案:

答案 0 :(得分:4)

Test::SimpleTest::Builder::Module子类;您可以通过获取Test::Builder对象($TestBuilder = Test::Simple::->builder())然后调用其输出方法来更改输出的目标(请参阅Test::Builder Output)。

答案 1 :(得分:4)

您可能希望使用内置的“prove”实用程序,该实用程序充当测试的线束。请注意“diag()”和“note()”之间的差异(在Test::More中定义):

use strict;
use warnings;
use Test::More tests => 3;

diag "My awesome test suite";
note "This message won't be visible when run via the harness";

ok(1, "test 1");
ok(1, "test 2");
ok(0, "test 3");

diag "all done!";

通过“perl mytest.pl”运行时,您会看到:

1..3
# My awesome test suite
# This message won't be visible when run via the harness
ok 1 - test 1
ok 2 - test 2
not ok 3 - test 3
#   Failed test 'test 3'
#   at foo.pl line 10.
# all done!
# Looks like you failed 1 test of 3.

当通过“prove mytest.pl”运行时,你会看到这个(带有一些生气的红色文字):

foo.pl .. # My awesome test suite
foo.pl .. 1/3
#   Failed test 'test 3'
#   at foo.pl line 10.
# all done!
# Looks like you failed 1 test of 3.
foo.pl .. Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/3 subtests

Test Summary Report
-------------------
foo.pl (Wstat: 256 Tests: 3 Failed: 1)
  Failed test:  3
  Non-zero exit status: 1
Files=1, Tests=3,  0 wallclock secs ( 0.03 usr  0.01 sys +  0.01 cusr  0.01 csys =  0.06 CPU)
Result: FAIL

prove mytest.pl > stdout.txt”的stderr输出将是:

# My awesome test suite

#   Failed test 'test 3'
#   at foo.pl line 10.
# all done!
# Looks like you failed 1 test of 3.

ALSO ,您可以创建自己的测试工具并收集有关您刚刚使用Test::Harness运行的测试的统计信息。让我们来看看当我们利用上面编写的测试脚本时会发生什么:

use strict;
use warnings;
use Test::Harness;
use Data::Dumper;

my @results = Test::Harness::execute_tests( tests => ["mytest.pl"] );
print Dumper(\@results);

产生输出:

# My awesome test suite

foo.pl .. 1/3


#   Failed test 'test 3'

#   at foo.pl line 10.

# all done!

# Looks like you failed 1 test of 3.


foo.pl ..
Dubious, test returned 1 (wstat 256, 0x100)


Failed 1/3 subtests


$VAR1 = [
          {
            'files' => 1,
            'max' => 3,
            'bonus' => 0,
            'skipped' => 0,
            'sub_skipped' => 0,
            'ok' => 2,
            'bad' => 1,
            'good' => 0,
            'tests' => 1,
            'bench' => bless( [
                                0,
                                '0.02',
                                '0.01',
                                '0.01',
                                '0.01',
                                0
                              ], 'Benchmark' ),
            'todo' => 0
          },
          {
            'foo.pl' => {
                          'name' => 'foo.pl',
                          'max' => 3,
                          'canon' => '3',
                          'wstat' => '256',
                          'failed' => 1,
                          'estat' => 1
                        }
          },
          {}
        ];

答案 2 :(得分:1)

我在代码中添加了以下内容以获取结果的输出:

use Test::More;

my $builder = Test::More->builder->output('>result.txt');

结果如下:

not ok 1 - Checking Upgrade
ok 2 - Checking Others
not ok 1 - Checking Upgrade
ok 2 - Checking Others

我需要旧的结果,所以我添加了

  

&GT;

在创建输出文件时,在result.txt前面

。如果您想收到新结果,请将代码设为:

my $builder = Test::More->builder->output('result.txt');