【问题标题】:Unit test for testing Perl script/module exceptions and dieing用于测试 Perl 脚本/模块异常和死机的单元测试
【发布时间】:2015-07-15 11:53:18
【问题描述】:

我正在尝试使用 Test::MoreTest::Exception 库为我的脚本创建单元测试。
我已阅读这些文章How to test for exceptions in PerlTest::Exception
第一篇文章准确描述了我需要什么来测试我的子例程是否抛出异常或死机。
但我无法让它工作。考虑一些例子

    #!/usr/bin/env perl
    package My::SuperModule;
    use strict;
    use warnings;
    use Net::Ping;
    use Utils::Variables::Validator;

    sub new
    {
        die "Hello";
        #Getting class name from, stored in $_[0]
        my $class = shift;
        #Getting user name from arguments $_[1]
        my $user_name = shift;
    ........
}

还有我的测试文件

    use warnings;    # this warns you of bad practices
    use strict;      # this prevents silly errors
    use Test::More; # for the is() and isnt() functions
    use Test::Exception;
    do './My/SuperModule.pm';
    #Testing module loading 
    print "=================Testing module loading=================\n";
    use_ok ( 'My::SuperModule' );
    use_ok ( 'My::SuperModule', 'new' );
    #Testing module subroutines
    dies_ok { My::SuperModule->new() } "Died in class constructor";
     sub div {
    my ( $a, $b ) = @_;
    return $a / $b;
};
    dies_ok { div( 1, 0 ) } 'divide by zero detected';

它在任何情况下都会停止执行脚本,但我只需要处理如果死了,我需要对此进行测试,因为如果数据无效或其他情况我手动调用 die ,但它死了并且不会继续执行脚本更远。给我留言

Uncaught exception from user code:
    Hello at ../libs/My/SuperModule.pm line 31.
    My::SuperModule::new('My::SuperModule', '') called at SuperModule.t line 24
# Tests were run but no plan was declared and done_testing() was not seen.
# Looks like your test exited with 2 just after 8.

但是如果使用除以零,它就像我想要的那样工作

ok 16 - divide by zero detected

所以它失败但不会终止脚本的执行。

我是Perl的新手,所以不能自己解决问题,也许根本没有问题,只是没有办法做我想做的事。
请建议该怎么做或说我的错在哪里。

编辑

我刚刚尝试在我的模块新子例程中除以零,这是我得到的消息。

Illegal division by zero at ../libs/My/SuperModule.pm line 33 (#1)
    (F) You tried to divide a number by 0.  Either something was wrong in
    your logic, or you need to put a conditional in to guard against
    meaningless input.

我真的不知道发生了什么。请帮忙。

【问题讨论】:

  • 你为什么do你的模块?说一次use_ok 'My::SuperModule'; 就够了。其他所有use_okdo 都不需要。

标签: perl unit-testing exception perl-module die


【解决方案1】:

这是一个有效的最小示例:

package My::SuperModule;
use strict;
use warnings;

sub new {
    die "Hello";
}

package main;

run() unless caller;

use Test::More;
use Test::Exception;

sub run {
    dies_ok { My::SuperModule->new } "dies ok";
    done_testing;
}

输出:

C:\...\t> 证明 -v my.pl
我的.pl ..
好 1 - 好死
1..1
好的
所有测试成功。
文件=1,测试=1,0 挂钟秒(0.09 usr + 0.00 sys = 0.09 CPU)
结果:通过

请注意,为了提供一个独立的示例,我将模块代码和测试脚本合并到一个文件中。

另外请注意,我没有用不必要的print 语句使测试脚本混乱。

如果需要,可以使用diag 显示一些输出:

diag "Checking if Test::Exception::dies_ok will catch die in new";
dies_ok { My::SuperModule->new } "dies ok";
done_testing;

输出:

C:\...\t> 证明 my.pl
my.pl .. # 检查 Test::Exception::dies_ok 是否会在 new 中捕获死
我的.pl .. 好的
所有测试成功。
文件=1,测试=1,0 挂钟秒(0.05 usr + 0.02 sys = 0.06 CPU)
结果:通过

与普通的print 语句相反,diag 输出实际上会在从诸如prove 之类的工具运行时显示。

【讨论】:

    【解决方案2】:

    它对我有用。我需要做的就是:

    1. 注释掉测试未使用的两个模块(Net::Ping 和 Utils::Variables::Validator - 我没有安装它们)。
    2. 1; 添加为模块的最后一行 - 这样它就会返回一个真值。
    3. 从测试中删除多余的requireuse_ok
    4. 在测试结束时添加了done_testing; - 所以测试工具知道它已经到了测试的结束。

    您可能不关心该列表中的第一项,但如果您修复其他项,它应该对您有用。

    【讨论】:

      猜你喜欢
      • 2013-02-21
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      相关资源
      最近更新 更多