【发布时间】:2017-09-21 20:27:07
【问题描述】:
我正在编写一个 P4Perl 脚本来连接到 Perforce 服务器并自动执行 Perforce 命令。除了开发访问 Perforce 的子程序,我还在开发单元测试来验证它们。我是 Perl 和单元测试的新手。
这是我与 Perforce 建立连接的子程序。文件名为p4_connect.pl
use warnings;
use strict;
use P4;
my $clientname = "johndoe"
my $p4port = "icmanage:1667"
main();
sub main {
my $status;
$status = connect_perforce($clientname, $p4port);
};
sub connect_perforce {
my ($clientname, $p4port) = @_;
my $status;
my $p4 = new P4;
$p4->SetClient( $clientname );
$p4->SetPort( $p4port );
$status = $p4->Connect() or die( "Failed to connect to Perforce Server" );
return $status;
}
当我运行"perl p4_connect.pl" 时,Perl 脚本执行良好,没有抛出任何错误。
但是,当我将connect_perforce 子例程移动到包模块(Perforce.pm) 并为其编写单元测试(perforce.t) 时,我遇到了这些错误:
username@hostname% perl -Ilib t/perforce.t
ok 1 - use Perforce;
ok 2 - Perforce->can('connect_perforce')
Connect to server failed; check $P4PORT.
TCP connect to johndoe failed.
Servname not supported for ai_socktype
Failed to connect to Perforce Server at lib/Perforce.pm line 16.
这就是单元测试(perforce.t) 的样子:
use Perforce;
use warnings;
use strict;
use Test::More qw(no_plan);
use P4;
BEGIN { use_ok('Perforce'); } #package can be loaded
can_ok('Perforce', 'connect_perforce'); #subroutine connect_perforce exists
my $p4port = "icmanage:1667";
my $p4 = Perforce->connect_perforce(qw(johndoe $p4port)); #accessing the connect_perforce() subroutine
这就是我的包(Perforce.pm) 的样子:
package Perforce;
use warnings;
use strict;
use P4;
sub connect_perforce {
my ($clientname, $p4port) = @_;
my $status;
my $p4 = new P4;
$p4->SetClient( $clientname );
$p4->SetPort( $p4port );
$status = $p4->Connect() or die( "Failed to connect to Perforce Server" );
return $status;
}
我的单元测试哪里出错了?任何建议都有帮助。
【问题讨论】:
-
不确定这是否相关,但您的一个程序将
$p4port设置为icmanage.com:1667,而另一个将其设置为icmanage:1667。顺便说一句,“ai_socktype 不支持 Servname”消息使您的问题听起来与 stackoverflow.com/questions/23079017/… 非常相似。该消息让我认为某些网络软件库正在尝试在网络配置文件中查找“icmanage”服务? -
非常抱歉,这是一个错字。它已得到适当的纠正
-
new P4应该是带有箭头的P4->new。这是该代码中唯一应该有那个箭头的地方,因为间接对象表示法是模棱两可的。 :)
标签: perl unit-testing perforce