有没有人经历过失败的单元测试,当他们尝试调试它以找出失败发生的位置时,在调试器中运行代码时单元测试成功?
我正在使用带有 EPIC 0.6.35 和 ActiveState ActivePerl 5.10.0 的 Eclipse 3.5.1。我用多个例程编写了模块 A 和模块 B。模块 B 中的一个例程从模块 A 调用一堆例程。我将模拟对象添加到我的模块 B 单元测试文件中,以尝试在模块 B 上获得更完整的代码覆盖率,其中模块 B 中的代码测试以查看所有调用模块 As 例程失败或成功。所以我在我的单元测试中添加了一些模拟对象来强制一些模块 A 例程返回失败,但我没有得到预期的失败。当我调试我的单元测试文件时,对模块 A 例程的调用确实按预期失败(并且我的单元测试成功)。当我在没有调试的情况下正常运行单元测试文件时,对模拟模块 A 例程的调用没有按预期失败(并且我的单元测试失败)。
这里会发生什么?如果我可以使用一小组简单代码使其失败,我将尝试发布我的问题的工作示例。
附录: 我把我的代码缩减到一个能证明我的问题的最低限度的集合。该问题的详细信息和工作示例如下:
我的 Eclipse 项目包含一个“lib”目录,其中包含两个模块 ... MainModule.pm 和 UtilityModule.pm。我的 Eclipse 项目在顶层还包含一个名为 MainModuleTest.t 的单元测试文件和一个名为 input_file.txt 的文本文件,它只包含一些垃圾文本。
EclipseProject/
MainModuleTest.t
input_file.txt
lib/
MainModule.pm
UtilityModule.pm
MainModuleTest.t 文件的内容:
use Test::More qw(no_plan);
use Test::MockModule;
use MainModule qw( mainModuleRoutine );
$testName = "force the Utility Module call to fail";
# set up mock utility routine that fails
my $mocked = new Test::MockModule('UtilityModule');
$mocked->mock( 'slurpFile', undef );
# call the routine under test
my $return_value = mainModuleRoutine( 'input_file.txt' );
if ( defined($return_value) ) {
# failure; actually expected undefined return value
fail($testName);
}
else {
# this is what we expect to occur
pass($testName);
}
MainModule.pm 文件的内容:
package MainModule;
use strict;
use warnings;
use Exporter;
use base qw(Exporter);
use UtilityModule qw( slurpFile );
our @EXPORT_OK = qw( mainModuleRoutine );
sub mainModuleRoutine {
my ( $file_name ) = @_;
my $file_contents = slurpFile($file_name);
if( !defined($file_contents) ) {
# failure
print STDERR "slurpFile() encountered a problem!\n";
return;
}
print "slurpFile() was successful!\n";
return $file_contents;
}
1;
UtilityModule.pm 文件的内容:
package UtilityModule;
use strict;
use warnings;
use Exporter;
use base qw(Exporter);
our @EXPORT_OK = qw( slurpFile );
sub slurpFile {
my ( $file_name ) = @_;
my $filehandle;
my $file_contents = "";
if ( open( $filehandle, '<', $file_name ) ) {
local $/=undef;
$file_contents = <$filehandle>;
local $/='\n';
close( $filehandle );
}
else {
print STDERR "Unable to open $file_name for read: $!";
return;
}
return $file_contents;
}
1;
当我在 Eclipse 中右键单击 MainModuleTest.t 并选择Run As | Perl Local,它给了我以下输出:
slurpFile() was successful!
not ok 1 - force the Utility Module call to fail
1..1
# Failed test 'force the Utility Module call to fail'
# at D:/Documents and Settings/[SNIP]/MainModuleTest.t line 13.
# Looks like you failed 1 test of 1.
当我右键单击同一个单元测试文件并选择Debug As | Perl Local,它给了我以下输出:
slurpFile() encountered a problem!
ok 1 - force the Utility Module call to fail
1..1
所以,这显然是个问题。运行方式和调试方式应该给出相同的结果,对吧?!?!?