Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Interactive Test

by ait (Friar)
on Sep 17, 2010 at 20:53 UTC ( #860559=perlquestion: print w/replies, xml ) Need Help??
ait has asked for the wisdom of the Perl Monks concerning the following question:

This has probably come up many times but I tried super searching with several word combinations but could not find what I was looking for. I also searched CPAN and nothing

So the question is, how the heck do I make an interactive test such as those I have seen sometimes, like "Do you want to run live tests (y/n)?". I'm guessing there is some magic with a SKIP block, but how do I get my question out to the user and how do I get the input back???

The Test::More POD says

For more details on the mechanics of skip and todo tests see Test::Harness.

But the latter doesn't really say much abot the matter :-(

Replies are listed 'Best First'.
Re: Interactive Test
by MidLifeXis (Monsignor) on Sep 17, 2010 at 21:43 UTC

    See also, which uses the environment variables AUTOMATED_TESTING and RELEASE_TESTING to establish the level of testing appropriate for the current environment.


      Thanks! Great reading!

      Thanks again for all who answered.

Re: Interactive Test
by chromatic (Archbishop) on Sep 17, 2010 at 21:31 UTC

    Rule #1 of interactive testing: Please don't.

    Rule #2 of interactive testing (for advanced users only). Use the prompt function in your build tool (EUMM, M::B), then set an environment variable before testing which you can check for so that you skip interactive tests in non-interactive test runs. Then in six months when you're tired of debugging the differences between test runs with and without interaction, see the first rule again.

Re: Interactive Test
by BrowserUk (Pope) on Sep 17, 2010 at 21:36 UTC

      Thanks! I looked at that but seemed overkill for a single y/n question!

Re: Interactive Test
by Anonymous Monk on Sep 17, 2010 at 21:22 UTC
    You might want to look at Term::ReadLine.

    But, if having to press enter is acceptable, here's one method without using that module:

    print "Do you want to run live tests (y/n)? "; my $answer = <STDIN>; # beware of trailing newlines if ($answer =~ /^n/i) { $foo = 0 } elsif ($answer =~ /^y/i { $foo = 1 } else { $foo = $default_foo } # default

    Or, you could repeat question with a goto LABEL or by wrapping it in a loop and only breaking out when an answer is acceptable.

      Thanks but it's a bit more complex than that. Tests run generally under prove or similar so your test script doesn't have easy access to the outside world. Nontheless, I have seen many times interactive tests, just haven't bothered to save the code after install to find out how, and I don't remember any specific module that does it, as to download and figure it out.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://860559]
Approved by lidden
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2018-08-17 01:12 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (174 votes). Check out past polls.