覆盖Perl脚本和模块的合并覆盖率数据



我在合并Perl脚本和模块覆盖的数据时遇到了问题。单独运行Devel::Cover工作得很好,但是当我试图组合数据时,我只丢失了Perl脚本的统计数据,而不是模块。

让我解释一下……

我有一个目录树,看起来像这样…

Code_Coverage_Test
 |
 |---->lib
 |
 |---->t
 |

在根Code_Coverage_Test目录中,我有Build.pl文件,该文件为模块和脚本构建测试,启动另外两个脚本,为我自动执行一些命令。

。/Build.pl

#!/usr/bin/perl -w
use strict;
use Module::Build;
my $buildTests = Module::Build->new(
        module_name             => 'testPMCoverage',
        license                 => 'perl',
        dist_abstract           => 'Perl .pm Test Code Coverage',
        dist_author             => 'me@myEmail.com',
        build_requires  => {
           'Test::More' => '0.10',
        },
);
$buildTests->create_build_script();

。/startTests.sh

#!/bin/sh
cd t
./doPMtest.sh
./doPLtest.sh
cd ../
perl Build testcover

在lib目录中,我有我试图运行代码覆盖的文件。

lib/testPLCoverage.pl

#!/usr/bin/perl -w
use strict;
print "Ok!";
lib/testPMCoverage.pm

use strict;
use warnings;
package testPMCoverage;
sub hello {
    return "Hello";
}
sub bye {
    return "Bye";
}

1;

在t目录中,我有模块的.t测试文件和为我启动测试的2个脚本。它们都由根目录

中的startTests.sh调用。

t/testPMCoverage.t

#!/usr/bin/perl -w

use strict;
use Test::More;
require_ok( 'testPMCoverage' );
my $test = testPMCoverage::hello();
is($test, "Hello", "hello() test");
done_testing();

t/doPLtest.sh

#!/bin/sh
#Test 1
cd ../
cd lib
perl -MDevel::Cover=-db,../cover_db testPLCoverage.pl

t/doPMtest.sh

#!/bin/bash
cd ../
perl Build.pl
perl Build test

我遇到的问题是,当doPLtests.sh脚本运行时,我得到覆盖率数据,没有问题。

---------------------------- ------ ------ ------ ------ ------ ------ ------
File                           STMT   Bran   Cond    Sub    pod   Time  total
---------------------------- ------ ------ ------ ------ ------ ------ ------
testPLCoverage.pl             100.0    n/a    n/a  100.0    n/a  100.0  100.0
Total                         100.0    n/a    n/a  100.0    n/a  100.0  100.0
---------------------------- ------ ------ ------ ------ ------ ------ ------

然而,当doPMtest.sh脚本完成并且startTests.sh脚本启动Build testcover命令时,我丢失了沿途的数据,并且我得到这些消息…

Reading database path/Code_Coverage_Tests/cover_db
Devel::Cover: Warning: can't open testPLCoverage.pl for MD5 digest: No such file or directory
Devel::Cover: Warning: can't locate structure for statement in testPLCoverage.pl
Devel::Cover: Warning: can't locate structure for subroutine in testPLCoverage.pl
Devel::Cover: Warning: can't locate structure for time in testPLCoverage.pl

. .然后我丢失了数据

---------------------------- ------ ------ ------ ------ ------ ------ ------
File                           STMT   Bran   Cond    Sub    pod   Time  total
---------------------------- ------ ------ ------ ------ ------ ------ ------
blib/lib/testPMCoverage.pm     87.5    n/a    n/a   75.0    0.0  100.0   71.4
testPLCoverage.pl               n/a    n/a    n/a    n/a    n/a    n/a    n/a
Total                          87.5    n/a    n/a   75.0    0.0  100.0   71.4
---------------------------- ------ ------ ------ ------ ------ ------ ------

我如何结合Perl模块和Perl脚本测试在一个文件中获得有效的代码覆盖率?

Perl不存储它使用的文件的完整路径。如果它通过相对路径找到文件,则只存储相对路径。您可以在perl在这些文件的警告和错误消息中显示的路径中看到这一点。

当Devel::Cover处理文件时,它使用perl给出的路径。你可以在Devel::Cover的报告中看到这一点,你有testPLCoverage.pl和blib/lib/testPMCoverage.pm。

在实践中,这意味着无论何时你将coverage放入一个coverage DB中,你都应该确保你是从同一个目录中做的,这样Devel::Cover就可以匹配并定位到coverage DB中的文件。

我想这就是你要解决的问题。

我的建议是,在t/doPLtest.sh你不cd到lib。您可以运行如下命令:

perl -Mblib -MDevel::Cover=-db,../cover_db lib/testPLCoverage.pl

(顺便问一下,为什么这个文件在lib中?)

我认为这意味着Devel::Cover将在每个情况下从项目根目录运行,因此应该允许它匹配并找到文件。

最新更新