相关疑难解决方法(0)

如果在没有换行符的情况下写入stdout,则测试失败

当我使用该prove实用程序运行perl测试时,如果测试中的方法包含print未被换行符终止的语句,则它将失败.

use Test::More tests=>1;

ok(foo(), "calling foo");

sub foo{
    print "A";
    1;
}
Run Code Online (Sandbox Code Playgroud)

这导致了

Bad plan.  You planned 1 tests but ran 0.
Run Code Online (Sandbox Code Playgroud)

如果我附加换行符:print "A\n";测试通过.

(注意,如果我只是执行测试perl mytest.t而不是使用prove它通过任何一种方式).

任何想法为什么会这样,以及如何解决它?

testing perl perl-prove

5
推荐指数
1
解决办法
313
查看次数

单元测试带有打印到屏幕的参数的Perl函数

如何使用打印到屏幕的参数对perl函数进行单元测试?

sub test {
  my $var = shift;
  if( $var eq "hello") {
    print "Hello";
  }
  else {
    print "World";
  }
} 
Run Code Online (Sandbox Code Playgroud)

我想完全覆盖打印到屏幕的功能中的所有条件,但我不知道如何...

我在stackoverflow上看到了这个答案如何单元测试打印到屏幕的Perl函数?

是的答案可以单元测试一个输出字符串的函数,但只有在所需的函数中没有参数时...在我的情况下我可以使用:

stdout_is(\&test, "World", 'test() return World');
Run Code Online (Sandbox Code Playgroud)

但我怎么测试print "Hello";?

编辑

我尝试使用这个测试用例:

should_print_hello();
should_print_world();


sub should_print_world
{
  stdout_is(\&test, "World", "should_print_world");
}

sub should_print_hello
{
  # this does not work and outputs error
  stdout_is(\&test("hello"), "Hello", "should_print_hello");
}
Run Code Online (Sandbox Code Playgroud)

因为stdout_is'函数的参数只是对函数的代码引用(如果我没有弄错),它没有function(variables_here).

我还阅读了perl Test :: Output手册,但我仍然无法找到解决方案..有没有其他方法或者我错过了什么?

所以我的主要问题是: 我如何单独测试只打印到屏幕(stdout)的perl函数(带参数)?

perl unit-testing

2
推荐指数
1
解决办法
357
查看次数

标签 统计

perl ×2

perl-prove ×1

testing ×1

unit-testing ×1