如何有条理地使用Test :: Simple?(How to use Test::Simple conditionally?)

编程入门 行业动态 更新时间:2024-10-25 01:35:26
如何有条理地使用Test :: Simple?(How to use Test::Simple conditionally?)

我想添加选项来使用命令行选项有条件地测试我的程序,例如--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.

更多推荐

本文发布于:2023-08-04 09:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1414092.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有条理   Test   conditionally   Simple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!