Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

How to write test code to test the perl script

by gjoshi (Sexton)
on Jun 18, 2014 at 10:29 UTC ( #1090287=perlquestion: print w/replies, xml ) Need Help??
gjoshi has asked for the wisdom of the Perl Monks concerning the following question:

Need help in writing test code to test perl script.

I have code as below which sends command through the serial port and gets the reply. Now I want to write a test script which will test script. Anyone how to achive above. Thanks
use strict; use Win32::SerialPort; my $ob = Win32::SerialPort->new ('COM7') || die; $ob->user_msg(1); # misc. warnings $ob->error_msg(1); # hardware and data errors $ob->baudrate(38400); $ob->parity("none"); $ob->parity_enable(1); # for any parity except "none" $ob->databits(8); $ob->stopbits(1); $ob->handshake('rts'); $ob->write_settings; #$ob->save("tpj4.cfg"); print "wrote configuration file tpj4.cfg\n"; #$ob->write("ATE0X4\r"); #sleep 1; #my $result = $ob->input; #print "result = $result\n"; $ob->write("AT&V\r"); sleep 2; my $result = $ob->input; print "result = $result\n"; $ob->write("AT+CGDCONT?"); sleep 2; my $result = $ob->input; print "result = $result\n"; undef $ob;

Replies are listed 'Best First'.
Re: How to write test code to test the perl script
by hippo (Monsignor) on Jun 18, 2014 at 12:46 UTC

    For the most easy, basic testing have a look at Test::Simple - you should be able to get up and running with that in next to no time. As you progress and want to perform more elaborate tests you can start to look at Test::More and ultimately Test::Harness.

    Once you've read the Test::Simple docs you can reply here if you need help with any of the specifics.

    Good luck.

Re: How to write test code to test the perl script
by wjw (Curate) on Jun 18, 2014 at 12:00 UTC

    Exactly what is it you want to test your Perl script for? Need to know a bit more about what you are trying to accomplish.

    ...the majority is always wrong, and always the last to know about it...

    Insanity: Doing the same thing over and over again and expecting different results...

    A solution is nothing more than a clearly stated problem...otherwise, the problem is not a problem, it is a facct

Re: How to write test code to test the perl script
by perlfan (Curate) on Jun 18, 2014 at 19:38 UTC
    I generally write my scripts as modulinos because they allow me to do proper unit testing. It doesn't take away from their nature as a scripts and it allows them to act as packages if need be.

    The take away here, of course, is that you can use testing methods that work well for packages if you went with this approach. It's already been mentioned that you should look at Test::Simple and Test::More.

Re: How to write test code to test the perl script
by Khen1950fx (Canon) on Jun 20, 2014 at 05:38 UTC
    A good module to start your tests with is Test::Device::SerialPort. For example:
    #!/usr/bin/perl -l use strict; use warnings; use Test::Device::SerialPort qw( :STAT 0.05 ); use Data::Dumper::Concise; use Test::More; plan tests => 13; my $cfgname = "tpj4.cfg"; Test::Device::SerialPort->set_test_mode_active(1); my $ob = Test::Device::SerialPort->new('COM7'); die unless ($ob); ok( $ob->user_msg(1), 'user_msg on' ); ok( $ob->error_msg(1), 'error_msg on' ); ok( $ob->can_baud, 'can_baud' ); ok( $ob->can_databits, 'can databits' ); ok( $ob->can_parity_enable, 'can parity_enable' ); ok( scalar $ob->debug("T"), 'T' ); ok( scalar $ob->debug(), 'read debug state' ); ok( $ob->can_stopbits(1), 'can_stopbits' ); ok( $ob->can_handshake("rts"), 'set can_handshake' ); ok( $ob->write_settings, 'write settings' ); ok( $ob->save($cfgname), 'save settings' ); ok( $ob->write("AT&V\r") ); my ($result1) = $ob->input; print Dumper ("result = $result1"); ok( $ob->write("AT+CGDCONT?") ); my ($result2) = $ob->input; print Dumper ("result = $result2"); undef $ob;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1090287]
Approved by marto
[arunks]: do you have any sample code .please share it. am looking for before opening excel file prompt should ask for password to open the excel file.
[marto]: which part don't you know how to do?
[marto]: oh
[Corion]: arunks: Maybe How do I make password prompts not echo back the user?? Or simply read it from STDIN.
[Corion]: arunks: Please do not ask us to write your program. You have to write your program yourself.

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2017-01-20 14:23 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (174 votes). Check out past polls.