http://www.perlmonks.org?node_id=1090287

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 PortTry.pl 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 (Bishop) 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 (Priest) 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 (Vicar) 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;