How can I get 100% testing coverage in a Perl module that uses DBI?

I am a little new to the Devel :: Cover module, but found it very useful, making sure that I don't skip tests.

The problem I am facing is understanding the report from Devel :: Cover. I looked through the documentation, but I can’t understand what I need for testing in order to get 100% coverage.

Change . I must clearly state that I am not saying that I need 100% coverage, because, as many people point out, 100% coverage is a free term, does not mean that my code does not contain errors, and may not always be completely necessary . Since I am new to Devel :: Cover, I am interested to know why my code does not cover 100% in case I miss some important tests.

Here is the result of the cover report:

line  err   stmt   bran   cond    sub    pod   time   code
...
36                                                    sub connect_database {
37             3                    3      1   1126       my $self = shift;
38             3    100                          24       if ( !$self->{dsn} ) {
39             1                                  7           croak 'dsn not supplied - cannot connect';
40                                                        }
41    ***      2            33                   21       $self->{dbh} = DBI->connect( $self->{dsn}, q{}, q{} )
42                                                          || croak "$DBI::errstr";
43             1                                 11       return $self;
44                                                    }
...
line  err      %      l  !l&&r !l&&!r   expr
----- --- ------ ------ ------ ------   ----
41    ***     33      1      0      0   'DBI'->connect($$self{'dsn'}, '', '') || croak("$DBI::errstr")

And here is an example of my code that checks for this particular line:

my $database = MyModule::Database->new( { dsn => 'Invalid DSN' });
throws_ok( sub { $database->connect_database() }, 
   qr/Can't connect to data source/, 
   'Test connection exception (invalid dsn)' );

This test passes - the connection throws an error and runs my throws_ok test.

I have several tests that verify successful communication, so I think I have 33% coverage, but if I read it correctly, the cover thinks that I am not testing part of the "|| croak" expression. I thought I was with the throws_ok test, but obviously something is missing.

Does anyone have any tips on how I can successfully test the DBI-> trunk?

Thank!

Edit:

HTML , , # 41 . , , . , ​​ , .

:

LINE # %  # coverage    # condition
41   # 33 # A | B | dec # 'DBI'->connect($$self{'dsn'}, '', '') || croak("$DBI::errstr")
     #    # 0 | 0 | 0   #
     #    # 0 | 1 | 1   #
     #    # 1 | X | 1   # (THIS LINE IS Green - the rest are red)

- , . , , , - , .

!

+5
2

, 100% - . , , Devel:: Cover. . Ovid :

, , , . , ||. 33%, . HTML Devel:: Cover .

, . A 1 , , 0 , , X , . , . || , , :

 0 || 1     connect to database fails and croak succeeds
 0 || 0     connect to database fails and croak fails (unlikely)
 1 || X     connect to database succeeds, so short circuit

, , . Perl - , , Perl. , . , , . . , , .

+9

DBI ( ), .

Test:: MockObject (, Perl mocking).

. , , , DBI .

, , Devel:: Cover. - 3 , unit test, , , unit test, Devel:: Cover, . "A || B".

+5

All Articles