perl - Devel::Cover 合并 Perl 脚本和模块的覆盖率数据

标签 perl code-coverage devel-cover module-build test-more

我在合并数据以覆盖 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!";

库/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。

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

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

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

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

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

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

关于perl - Devel::Cover 合并 Perl 脚本和模块的覆盖率数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22721177/

相关文章:

perl - 我可以打开一个集群 MQ 队列来用 Perl 编写吗?

perl - 通过 Travis CI 使用 perl-helpers 时如何强制 Devel::Cover 忽略文件夹

perl - 需要安装 Spreadsheet::ParseExcel 方面的帮助

perl - 如何将依赖于另一个属性的属性约束从触发器提升到类型细化中?

c# - VS2015 代码覆盖率不适用于 ASP.NET Core 1.0(以前称为 ASP.NET 5)中的测试

code-coverage - clang++ 的代码覆盖率

perl - Devel::Cover 条件三元运算符的分支覆盖

bash - 在嵌套的单引号、反引号和引号中转义美元符号

java - 我可以从 JUnit 5 获取测试的代码覆盖率吗?