Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 调用Test::Output::stdout_like()时,是否可以在代码引用中包含参数_Perl_Unit Testing_Code Coverage - Fatal编程技术网

Perl 调用Test::Output::stdout_like()时,是否可以在代码引用中包含参数

Perl 调用Test::Output::stdout_like()时,是否可以在代码引用中包含参数,perl,unit-testing,code-coverage,Perl,Unit Testing,Code Coverage,我正在使用Test::More和Test::Output编写单元测试。我使用Test::More来验证返回值,并计划使用Test::Output来验证子例程生成的stdout 我试图为一个子例程编写测试用例,该子例程的stdout依赖于发送的参数。Test::Output::stdout_like(代码引用、regexp、测试描述)看起来具有我想要的功能,但是我正在努力构造一个包含参数的代码引用 我认为这是Perl单元测试脚本中的常见做法。有人能举个例子吗 另请注意,感谢Kurt W.Leuch

我正在使用Test::More和Test::Output编写单元测试。我使用Test::More来验证返回值,并计划使用Test::Output来验证子例程生成的stdout

我试图为一个子例程编写测试用例,该子例程的stdout依赖于发送的参数。Test::Output::stdout_like(代码引用、regexp、测试描述)看起来具有我想要的功能,但是我正在努力构造一个包含参数的代码引用

我认为这是Perl单元测试脚本中的常见做法。有人能举个例子吗


另请注意,感谢Kurt W.Leucht对Perl单元测试的介绍:

不,您不能在coderef中直接包含arg

要将arg传递给coderef,您需要实际调用它:

mysub( $arg );      # the usual way to call the sub
$coderef = \&mysub; # get the reference to the sub
$coderef->( $arg ); # call the coderef with an arg (or &$coderef($arg))
但是,要使用
Test::Output
工作,可以将对要在另一个子例程中测试的子例程的调用包装起来:

use Test::Output;
sub callmysubwitharg { mysub($arg) }
stdout_like \&callmysubwitharg, qr/$expecting/, 'description';
使用匿名子例程也可以做同样的事情:

use Test::Output;
sub callmysubwitharg { mysub($arg) }
stdout_like \&callmysubwitharg, qr/$expecting/, 'description';

你能提供一个你尝试过的例子吗?谢谢你的洞察力,这正是我希望看到的。