How can I redirect test output from Perl Test :: Simple?

I am writing a simple test automation suite that will tell me how many tests passed and failed. The following is a simple example that describes 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 that I get after entering "TextBox" as the 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 that should look like

ok 1 - Checking name

If I add the following routine

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 can I continue so that the result of my code is redirected to a file, which should have the following format:

ok 1 - Checking name

ok 2 - Checking others

etc.?

+3
source share
3 answers

, 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

stderr "prove mytest.pl > stdout.txt" :

# My awesome test suite

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

, , Test::Harness. , , script, :

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
                        }
          },
          {}
        ];
+4

Test::Simple Test::Builder::Module ; , Test::Builder object ($TestBuilder = Test::Simple::->builder()), (. Test::Builder ).

+4

I added the following to the code to get the result 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 an old result, so I added

<P →

before result.txt when creating the output file. If you want a fresh result, enter the code as:

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

Source: https://habr.com/ru/post/1716273/


All Articles