开发者

Prompt user during unit test in Perl

开发者 https://www.devze.com 2023-04-10 08:01 出处:网络
I\'m writing a module which has unit tests that require a certain external server program to be running, and, if it is, the hostname and port need to be known.

I'm writing a module which has unit tests that require a certain external server program to be running, and, if it is, the hostname and port need to be known.

I would like to prompt f开发者_如何学编程or this information when running the test suite, and skip those tests if the user declines to provide it.

What's the best way to handle this?

Thanks


Are you looking for ExtUtils::MakeMaker::prompt?

Other Handy Functions

prompt

my $value = prompt($message);
my $value = prompt($message, $default);

The prompt() function provides an easy way to request user input used to write a makefile. It displays the $message as a prompt for input. If a $default is provided it will be used as a default. The function returns the $value selected by the user.

If prompt() detects that it is not running interactively and there is nothing on STDIN or if the PERL_MM_USE_DEFAULT environment variable is set to true, the $default will be used without prompting. This prevents automated processes from blocking on user input.

If no $default is provided an empty string will be used instead.


I'll take a different tack on this. Why exactly do you need the user as a middleman for this - especially for automated tests? A significantly better approach (from my own experience) is to do one of the following:

  • Make the server (hostname/port) somehow discoverable by the test. It can be either a genuine discoverability, or having the server register itself, or, heck, some config service. Considering that the server's clients in real world would need to be able to connect to the server, such a discovery logic (again, worst case scenario being some config file) should already exist.

  • For a really advanced case, allow the test harness the ability to start the test instance of a server on a test port if one is not running.

Mind you, if your server for some reason can not be instrumented to allow either of the approaches above, then socket puppet's answer is the best approach.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号