Rya*_*son 5 testing perl perl-module
我想在我的Perl发行版中添加一个需要模块Foo的测试,但是我的发行版不需要Foo; 只有测试需要Foo.所以我不想将模块添加到依赖项中,而是我只想跳过需要Foo的测试,如果Foo在构建时不可用.
这样做的正确方法是什么?我是否应该将我的Foo测试与eval块一起包装use Foo;,以便在加载Foo失败时测试不会运行?或者有更优雅的方式吗?
如果所有需要的测试Some::Module都在一个文件中,那么很容易做到:
use Test::More;
BEGIN {
eval {
require Some::Module;
1;
} or do {
plan skip_all => "Some::Module is not available";
};
}
Run Code Online (Sandbox Code Playgroud)
(如果您使用的是测试计数一样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
}
Run Code Online (Sandbox Code Playgroud)
如果不满足某些条件,Test :: More有一个跳过选项,见下文
SKIP: {
eval { require Foo };
skip "Foo not installed", 2 if $@;
## do something if Foo is installed
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
450 次 |
| 最近记录: |