How to debug a module created by Module :: Starter?

I am training to create a new module using Module :: Starter. I wrote some test cases for the package, and they sometimes work fine.

However, I noticed that there are two problems:

  • When test cases fail, I want to put some print statements in the function we are testing. I started make test, and he only informs me that my test cases failed, he does not display my print output, despite the fact that I am really sure that the print statements have been reached.

  • Let's say I have three test cases to test one function, I put a print statement inside the function, when the test cases start, it reports that only 1 out of three test cases was executed. If I delete the print statement, all three test cases will run. Why is this?

Here is my code:

# package declaration and stuff...
sub get_in {
  my ( $hash, @path ) = @_;
  my $ref = $hash;
  print 'lol'; # This is the troublesome print statement. Remove this statement and all three test cases will run and pass
  foreach (@path) {
    if ( ref($ref) eq 'HASH' && exists $ref->{$_} ) {
      $ref = $ref->{$_};
    } else {
      return undef;
    }
  }
  return $ref;
}

These are test cases:

use Test::More tests => 3;
use strict;
use warnings;
use diagnostics;
require_ok('Foo::Doc');
ok( Foo::Doc::get_in( { 'a' => { 'b' => { 'c' => 101 } } }, 'a', 'b', 'c' ) == 101 );
ok( @{ Foo::Doc::get_in( { 'a' => { 'b' => { 'c' => [ 1, 2, 3 ] } } }, 'a', 'b', 'c' ) } == @{ [ 1, 2, 3 ] } );
+5
source share
2 answers

There are several problems with your tests that need to be addressed, as well as your question. First your question:

If you want the output to appear in tests, you need to explicitly print the standard error. As a best practice, you also need your way out #. The Test :: More module provides tools that you can use easily for this.

my $got = Foo::Doc::get_in( { 'a' => { 'b' => { 'c' => 101 } } }, 'a', 'b', 'c' );
ok($got == 101); # you probably want is() instead, see below
diag("GOT $got"); # outputs "# GOT 101" or whatever to STDERR

, , note:

note("GOT $got");

, prove -v :

prove -l -v t/test.t

explain, :

diag explain $got;
# OR
note explain $got;

. is() - ok():

is($got, 101); # gives slightly more readable output on error

, is_deeply() :

is_deeply($got, [1, 2, 3]);

Test:: More, .

+7

, , . Test::More , , ok 5 not ok 6 - disgronificator enabled. "lol" , , "lolok 9 - it works" . ( print "not "; ).

, zostay, , diag , Test::More.

+2

All Articles