Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Needing help on testing

by davido (Cardinal)
on Oct 30, 2018 at 00:25 UTC ( [id://1224895]=note: print w/replies, xml ) Need Help??


in reply to Needing help on testing

It doesn't take a tremendous amount of work to convert a script into a modulino. And by so doing, if there are any subroutines within those scripts, they would become testable more in isolation. I wouldn't put the level of work into the category of rewrite, more in the category of refactor, and often it's a pretty simple refactor.

Consider the following script:

#!/usr/bin/env perl use strict; use warnings; use IO::Prompt::Tiny qw(prompt); my $user = prompt("What is your name? "); greet($user); exit(0); sub make_hello { return "Hello"; } sub make_subject { return shift; } sub greeting { my $who = shift; return make_hello() . ' ' . make_subject($who) } sub greet { print greeting(shift), "\n"; }

In its current form you would have to invoke the script, and this can be done from within a test, capturing the output and even providing input. But it's not ideal. Let's convert it to a modulino.

#!/usr/bin/env perl package MyScript; use strict; use warnings; use IO::Prompt::Tiny qw(prompt); main() if !caller(); exit(0); sub main { my $user = prompt("What is your name? "); greet($user); } sub make_hello { return "Hello"; } sub make_subject { return shift; } sub greeting { my $who = shift; return make_hello() . ' ' . make_subject($who) } sub greet { print greeting(shift), "\n"; } 1;

Now you can test it more easily:

use strict; use warnings; use Test::More; require '/path/to/myscript.pl'; can_ok MyScript => qw(greet greeting make_subject make_hello main); is MyScript::make_hello, 'Hello', 'Got Hello'; is MyScript::make_subject('Monk'), 'Monk', 'Got correct subject.'; is MyScript::greeting('Monk'), 'Hello Monk', 'Got a correct greeting.' +; use Capture::Tiny qw(capture_stdout); my $resp = capture_stdout {MyScript::greet('Monk')}; is $resp, "Hello Monk\n", 'Got correct output in STDOUT.'; done_testing();

The testing code is untested. ;)

Now you've tested at least minimally each of the main components except for main() itself, and if you wanted to, you could do that too. And all it required was adding the main() unless caller();, wrapping the top level code in a main subroutine, and putting a 1; at the end of the modulino. It really adds about four or five lines of code, and some indenting. You could almost write a script to apply it to all your scripts, and then to do a compile-check to verify they still compile. Keep backups of the original, unaltered scripts, though. ;)


Dave

Replies are listed 'Best First'.
Re^2: Needing help on testing
by Zenzizenzizenzic (Pilgrim) on Oct 30, 2018 at 21:01 UTC
    I did some reading on this as well today, will also try some of the other suggestions below. Thanks one and all!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1224895]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-25 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found