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

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I'm attempting to Do The Right Thing (tm), i.e. unit test my programs. I've been reading about unit testing, and it sounds like a very good idea -- I'm sold on the concept! Unfortunately, I've been having problems figuring out how to do it. Nearly all of the programming I do involves database and CGI stuff.

Most of the information I've found about Test::Harness, Test::Simple, Test::More, etc, seem to focus on examples like this:

is ( add_numbers(2, 2), 4); is ( get_initials("John F. Kennedy"), "JFK");

I can understand how to write that kind of test, but I'm having trouble figuring out how to write a test for code that:

  1. Retrieves data from a web form (via CGI.pm).
  2. Retrieves the corresponding row from the database.
  3. Fills in the template (via HTML::Template).
  4. Returns the HTML output (for display by CGI::Application).

My suspicion is that I need to modify my program to make testing easier.

Suppose the original code is like this:

sub show_record { my $self = shift; my $cgi = $self->query(); my $emp_no = $cgi->param("emp_no"); ... # code to access the database omitted ... my $emp_rec = $sth->fetchrow_hashref; ... # code to fiddle with the data omitted ... my $template = $self->load_tmpl(emp_detail.tmpl.html'); $template->param( $emp_rec ); return $template->output; }

As it is now, I can't figure out how to write a test that would verify that the web page displayed for employee number "12345" is correct. How would I cause $cgi->param("emp_no") to return "12345"? How would I compare the web page to the expected value? When someone changes the format of the web page, the test would break. Etc.

As I thought about it, I realized that testing for the value of the web page would not be Doing The Right Thing. Instead, I should probably have a subroutine that gets the data ready for display, then test that routine instead. In other words, something more like this:

sub show_record { my $self = shift; my $cgi = $self->query(); my $emp_no = $cgi->param("emp_no"); my $emp_rec = get_emp_data($emp_no); my $template = $self->load_tmpl(emp_detail.tmpl.html'); $template->param( $emp_rec ); return $template->output; } sub get_emp_data { my ($emp_no) = (@_); ... # code to access the database omitted ... my $emp_rec = $sth->fetchrow_hashref; ... # code to fiddle with the data omitted ... return $emp_rec; }

With the above changes, I wouldn't do unit tests of show_record(), on the theory that it is pretty much all presentation code, not logic (although if anyone has suggestions on how to write unit tests for that, I'm interested). Instead, I would just test get_emp_rec().

Does that sound like Doing The Right Thing, or is there some nifty trick out there that I'm missing?

Wally Hartshorn

(Plug: Visit JavaJunkies, PerlMonks for Java)


In reply to Unit Testing CGI Programs by Wally Hartshorn

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (4)
    As of 2014-09-18 04:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (108 votes), past polls