Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Here's one way to get started quickly with testing, in about 10 lines of code.

I recently wrote my first tests to drive a Java CLI app, and the investment was repaid quickly when they were easily ported to a second project w/ a similar CLI. I spent less time manually testing, and was more confident about changing the code because I could quickly test it.

The program has a simple command-line menu of about 10 items. I tested the program manually using that menu, which became tedious during development. So I looked at the excellent book: Perl Testing, A Developer's Notebook (ISBN 9780596100926). The section on Testing Interactive Programs in chapter 9 worked for me. (I had previously read the book and written a few tests. If you are new to testing, start at the beginning instead of the end, if necessary). The man pages for the test modules are also helpful.

I needed Test::Expect for this solution. That required me to make a unique and consistent prompt string in the programs under test, which was a good thing.

My first test looked like this:

use strict; use warnings; use Test::More tests => 3; use Test::Expect; expect_run( command => "java -cp '.' src/TestRoster", prompt => " >: ", quit => "0\n", ); expect_send( '97', 'Ask for help'); expect_like( qr/-Help about the menu items.+Terminate.+/sm, '... show +s help text'); ...
Test 1: print the program's Help menu:
Initialize with expect_run(): the command to run the program under test; the expected prompt; and how to terminate it.
Send the request from the test program (97); see the response at the console (the program's help text); write a regex in the test program to match the response (qr/.../). You now have a test.

Run that Perl test program individually; or use 'prove' to run one or more tests, and to show a summary of the output.

Write a test before you write the code for the next action. Run it, see it fail; write the code; run the test; repeat until no failures. You are now doing test-driven development.

Thanks for the tip on 'done_testing()'.


In reply to Re^2: What is the largest number of tests you have created for a single project you have worked on? by clp
in thread What is the largest number of tests you have created for a single project you have worked on? by atcroft

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2024-04-19 18:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found