我想添加选项来使用命令行选项有条件地测试我的程序,例如--test执行&test 。
不幸的是,即使我在&test子例程中添加了use Test::Simple tests => 1 ,Perl也认为我想在其他地方进行测试。
是否可以在需要时使用Test::Simple甚至Test::More ?
I would like to add the option to test my Program conditionally with a command line option like --test which execute &test.
Unfortunately even if I add use Test::Simple tests => 1 inside the &test subroutine, Perl think I want to do the tests everywhere else.
Is it possible to use Test::Simple or even Test::More only when needed?
最满意答案
如果您希望仅在某些条件下加载整个Test::More语句,您可以执行以下操作:
if( $condition ) { require Test::More ; import Test::More ; plan tests => 1 ; # Place your tests here... }这将导致与use Test::More tests => 1;相同的use Test::More tests => 1; 。
您的问题导致use是一个编译时语句,它将在if语句(在运行时计算)内执行或不执行。
If you want the whole Test::More statement only loaded under certain conditions you can do the following:
if( $condition ) { require Test::More ; import Test::More ; plan tests => 1 ; # Place your tests here... }This will result in the same as use Test::More tests => 1;.
Your problem results in the fact that use is a compile time statement that will be executed wether inside a if statement (evaluated at runtime) or not.
更多推荐
发布评论