Testing

barbie on 2002-07-17T09:22:48

Seeing as much of the stuff I'm going to be doing in future involves testing, I thought I'd look for some tutorial and guides for writing tests.

I found Schwern's very good Test::Tutorial page, which goes a long way to what I wanted but not quite far enough. I basically wanted to write test suites not just for modules but for whole scripts. A mammoth task I know, but but worth it in the long run. But there isn't anything ... except I did discover that Schwern is talking at next week's OSCON and more specifically has a "Writing a Test Library" talk. This sounds exactly what I was after. Only one problem though. I'm can't afford £2,500 out of my own pocket to go to OSCON :(

I hope Schwern puts his talk online somewhere, or perhaps goes that bit further by writing a book about it all. I enjoyed his talk at last year's YAPC::Europe but at the time didn't have the time to persue it further. I note he's proposed some talks for this years YAPC::Europe, which I hope to be a bit more proactive about this time.

In the meantime, if anyone knows of some good tutorials or guides online, please let me know.


Testing whole scripts

cbrooks on 2002-07-17T12:48:07

Test::Tutorial talks primarily about writing unit tests for modules. If you have existing scripts that you want to test, you may want to look into regression testing. Regression testing is used when releasing bug fixes and extensions to existing functionality to ensure that the overall level of functionality does not "regress" -- i.e. those things that worked in version 1.0 should keep working in version 1.1. I also find regression testing useful for poorly written legacy scripts. (For example, it is difficult to write unit tests that test the output of a function if the script doesn't make use of any functions....)



There are two modules that you can use to automate regression testing for web-based functionality -- HTTP::Monkeywrench and HTTP::WebTest. (If you have mod_perl installed, you should also take a look at Apache::Recorder and HTTP::RecordedSession which make the process of generating configuration files for Monkeywrench and WebTest much simpler.) I have an article in progress for The Perl Review on regression testing if you're interested -- drop me a note (cbrooks at organiccodefarm dot com) if you'd like to take a look.

Re:Testing whole scripts

petdance on 2002-07-17T18:41:01

You mean The Perl Review, right? :-)

I'm about to roll out some HTML testing stuff today or tomorrow that might be useful, too, along those lines. It'll be wrappers around HTML::Lint.

Maybe we should put up sample code that shows how people use the testing stuff in the real world.

Re:Testing whole scripts

petdance on 2002-07-18T01:39:59

# Here's a sample of a Session.t file that
# corresponds to a Session.pm file I have.
# It could certainly be more robust, and test
# more options than just some simple scalars.

#!/usr/bin/perl -w

use strict;

use constant SAVEPATH=>'/tmp';

use Test::More tests=>7;

BEGIN {
    use_ok( 'TW::Session' );
}

my $saved_session_id;

CREATE: {
    my $session = new TW::Session( SavePath=>SAVEPATH );
    isa_ok( $session, 'TW::Session', 'Created first session' );
    $session->{yankee} = 'doodle';
    is( $session->{yankee}, 'doodle', 'Stored the value' );
    $saved_session_id = $session->id;
}

REREAD: {
    my $session = new TW::Session( SID => $saved_session_id, SavePath=>SAVEPATH );
    isa_ok( $session, 'TW::Session', 'Instantiated 2nd session instance' );
    is( $session->id, $saved_session_id, 'IDs match' );
    is( $session->{yankee}, 'doodle', 'Retrieved the value' );

    # Check the link method
    is( $session->link( 'Click', 'target' ),
        "<a href=\"target?SID=$saved_session_id\">Click</a>",
        'link() builds correctly' );

    my $sess_filename = SAVEPATH . "/sess_$saved_session_id";
    $session->delete;  # Can't really test this one, but let's clean up anyway.
    ok( ! -e $sess_filename, "File $sess_filename was deleted" );
}