gpt4 book ai didi

perl - 仅当安装了所需的模块时,如何才能在我的 Perl 模块的测试套件中运行测试?

转载 作者:行者123 更新时间:2023-11-28 19:47:57 24 4
gpt4 key购买 nike

我想在我的 Perl 发行版中添加一个需要模块 Foo 的测试,但我的发行版不需要 Foo;只有测试需要 Foo。所以我不想将模块添加到依赖项中,而是如果 Foo 在构建时不可用,我只想跳过需要 Foo 的测试。

执行此操作的正确方法是什么?我是否应该将我的 Foo 测试与 use Foo; 一起包装在一个 eval block 中,以便在加载 Foo 失败时测试不会运行?或者有更优雅的方法吗?

最佳答案

如果所有需要 Some::Module 的测试都在一个文件中,那么很容易做到:

use Test::More;

BEGIN {
eval {
require Some::Module;
1;
} or do {
plan skip_all => "Some::Module is not available";
};
}

(如果您正在使用像 use Test::More tests => 42; 这样的测试计数,那么您还需要安排做 plan tests => 42; 如果要求确实成功。)

如果它们是包含其他内容的文件中的少量测试,那么您可以执行以下操作:

our $HAVE_SOME_MODULE = 0;

BEGIN {
eval {
require Some::Module;
$HAVE_SOME_MODULE = 1;
};
}

# ... some other tests here

SKIP: {
skip "Some::Module is not available", $num_skipped unless $HAVE_SOME_MODULE;
# ... tests using Some::Module here
}

关于perl - 仅当安装了所需的模块时,如何才能在我的 Perl 模块的测试套件中运行测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8484843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com