仅当安装了所需的模块时,如何才能在 Perl 模块的测试套件中运行测试?
我想向我的 Perl 发行版添加一个需要模块 Foo 的测试,但我的发行版不需要 Foo;只有测试需要 Foo。所以我不想将模块添加到依赖项中,而是如果 Foo 在构建时不可用,我只想跳过需要 Foo 的测试。
执行此操作的正确方法是什么?我是否应该将 Foo 测试与 use Foo;
一起包装在 eval 块中,以便在加载 Foo 失败时测试不会运行?或者有更优雅的方法吗?
I want to add a test to my Perl distribution that requires a module Foo, but my distribution does not require Foo; only the test requires Foo. So I don't want to add the module to the dependencies, but instead I just want to skip the tests that require Foo if Foo is not available at build time.
What is the proper way to do this? Should I just wrap my Foo tests in an eval block along with use Foo;
, so that the tests will not run if loading Foo fails? Or is there a more elegant way of doing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果需要
Some::Module
的所有测试都在一个文件中,那么很容易做到:(如果您使用像
use Test::Moretests => 这样的测试计数; 42;
那么您还需要安排进行计划测试 => 42;
(如果要求成功)。)包含其他内容的文件中的测试,然后你可以这样做:
If all of the tests that require
Some::Module
are in a single file, it's easy to do:(If you're using a test count like
use Test::More tests => 42;
then you need to also arrange to doplan tests => 42;
if the require does succeed.)If they're a smaller number of tests in a file that contains other stuff, then you could do something like:
如果不满足某些条件,Test::More 可以选择跳过,请参见下文
Test::More has an option to skip if some condition is not satisfied, see below
从 Test::More 的文档中:
From the documentation of Test::More:
另外,声明您的测试步骤要求或建议(存在差异)发行版元文件。这将由执行安装的客户端获取。在安装时,用户可以决定是永久安装这样的需求还是放弃它,因为它仅用于测试。
Also, declare your test step requirement or recommendation (there's a difference) in the distro meta file. This will be picked up by a client performing the installation. At install time, the user can decide whether to install such a requirement permanently or discard it because it was only used for testing.