1

出于测试目的,我想创建一个包装脚本,它可以在不更改原始脚本的情况下向现有脚本添加功能。我的问题是,这在 perl 中是否可行,我该怎么做?

包装脚本将执行以下操作:

#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use Aspect;

run syntax checking on config files;
run unittesting stuff;
call production script;

生产脚本将保持不变并且完全独立:

#!/usr/bin/perl
use strict;
use warnings;

bunch of production code here;

当然,我希望诊断和方面编织能够处理生产代码。我已经尝试了一些简单的事情,但它们没有用,代码甚至没有运行。

eval { require "production.pl" };
do 'production.pl';
require 'production.pl';
4

1 回答 1

1

我会使用Test::Script::Run。这些块可以帮助您分离测试用例。

您可以通过这种方式调用您的程序来获取诊断消息:

perl -Mdiagnostics -MAspect your_script.pl [args]

这就是你如何集成这个测试:

use Test::More;
use Test::Script::Run;
### test 1
{
  note 'test1 is running...';
  note 'test 1 app_name running fine';
  run_ok( 'app_name', [ app's args ], 'app_name runs ok' );
  note 'test 1 does not throws errors and returns correct values';
  my ( $return, $stdout, $stderr ) = run_script( 'app_name', [ app's args ] );
  run_output_matches_unordered(
        'app_name', [ app's args ],
        [ 'out line 2', 'out line 1' ],
        [ 'err line 2', 'err line 1' ],
        'run_output_matches_unordered'
  );

};
### test 2
{
   ...
};
done_testing();
于 2014-04-28T10:56:46.180 回答