简体   繁体   English

在Perl中进行单元测试时提示用户

[英]Prompt user during unit test in Perl

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 for 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 ? 你在寻找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. prompt()函数提供了一种简单的方法来请求用于编写makefile的用户输入。 It displays the $message as a prompt for input. 它将$ message显示为输入提示。 If a $default is provided it will be used as a default. 如果提供$ 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. 如果prompt()检测到它没有以交互方式运行且STDIN上没有任何内容,或者PERL_MM_USE_DEFAULT环境变量设置为true,则将使用$ default而不提示。 This prevents automated processes from blocking on user input. 这可以防止自动进程阻止用户输入。

If no $default is provided an empty string will be used instead. 如果没有提供$ default,则将使用空字符串。

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. 请注意,如果您的服务器出于某种原因无法通过上述任何一种方法进行检测,那么socket puppet的答案是最好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM