【问题标题】:Devel::Cover merging coverage data for Perl scripts and modulesDevel::Cover 合并 Perl 脚本和模块的覆盖率数据
【发布时间】:2014-05-08 09:46:51
【问题描述】:

我在合并数据以覆盖 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 code-coverage devel-cover module-build test-more


    【解决方案1】:

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

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

    这对您在实践中的意义在于,每当您将覆盖率放入覆盖率数据库时,您都应确保从同一目录执行此操作,以便 Devel::Cover 可以匹配并定位覆盖率数据库中的文件。

    我认为这是您遇到的问题。

    我的建议是在 t/doPLtest.sh 中不要 cd 进入 lib。你可以运行类似的东西:

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

    (顺便说一句,为什么该文件在 lib 中?)

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

    【讨论】:

    • 将文件移出 lib 目录有效.. 但是,据我了解并根据我所见的tutorials,使用Devel::Cover查看我需要的 Perl 模块的覆盖率数据使用Module::Build,它要求模块位于lib 目录中......我知道你写了Devel::Cover(顺便说一句做得很好),有没有其他方法可以解决这个问题?
    • 您不需要需要使用Module:Build,但是使用它或Makefile.PL 通常是开发Perl 模块的好主意。所以是的,模块应该进入 lib/ 目录,但脚本不应该。您通常会将它们放入 bin/ 或 scripts/ 目录中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    • 2019-09-25
    相关资源
    最近更新 更多