tags:

views:

261

answers:

3

I am writing a simple test automation suite that will tell me the number of test cases passed and failed. Below is a simple example that will describe the code:

#! /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');

The output I am getting after entering 'TextBox' as input is as follows:

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

I would like to redirect the same output to a file which should look like

ok 1 - Checking name

If I add the following subroutine

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);
}

Then I get either '1' or '0' - not the text I would like.

How should I proceed so that the result of my code redirects to a file which should have the following format:

ok 1 - Checking name

ok 2 - Checking others

and so on?

+4  A: 

Test::Simple is a Test::Builder::Module subclass; you can change the destination of the output by getting the Test::Builder object ( $TestBuilder = Test::Simple::->builder() ) and then calling its output method (see Test::Builder Output).

ysth
A: 

I added the following in the code to get the output of the result:

use Test::More;

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

The result is as follows:

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

I need the old result so i added

>

in front of result.txt while creating the output file. In case if you want to receive the fresh result make the code as:

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

You may want to look at using the builtin "prove" utility which acts as a harness around your tests. Note the difference between "diag()" and "note()" (defined in 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!";

When run via "perl mytest.pl", you will see this:

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.

When run via "prove mytest.pl", you will see this (with some text in nice angry red):

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

The stderr output of "prove mytest.pl > stdout.txt" will be:

# My awesome test suite

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

ALSO, you can create your own test harness and gather statistics about the tests you just ran with Test::Harness. Let's look at what happens when we harness the test script we wrote above:

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

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

Yields the output:

# 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
                        }
          },
          {}
        ];
Ether