Introducing Plack::Test::Agent

After a discussion with Zbigniew and Miyagawa on the subject of Plack::Test and Test:: Interfaces, I wrote Plack::Test::Agent as a proof of concept.

I really like the flexibility of Plack::Test, as testing an app in process or against a backend is trivial. I understand the callback interface—it definitely feels PSGIish, and it fits the interface that Test::TCP prefers. Yet there's always room to experiment.

Plack::Test::Agent offers the ability to run tests in process or over HTTP against a server. It relies on Test::TCP and Plack::Loader for the hard work, and borrows liberally from Plack::Test. Yet in doing so, it offers an OO interface:

my $agent = Plack::Test::Agent->new( app => $app );
my $res   = $agent->get( '/?have=foo;want=foo' );
ok $res->is_success, 'Request should succeed when values match';
is $res->decoded_content, 'ok', '... with descriptive success message';

Pass a server key/value pair, and it'll do its best to find and launch the appropriate server. All of your tests should continue to pass, modulo bugs in your assumptions or the handler/server interface.

Just for kicks, I added an experimental feature:

my $mech = Plack::Test::Agent->new( app    => $app,
                                    server => 'HTTP::Server::PSGI' )->get_mech;

$mech->get_ok( '/?have=foo;want=foo',
    'Request should succeed when values match' );
$mech->content_is( 'ok', '... with descriptive success message' );

... which returns a Test::WWW::Mechanize object bound to the started server such that relative URI requests go directly to the server. (Absolute URI requests remain unchanged.) The next obvious step is to return a Test::WWW::Mechanize::PSGI object for in process testing. That's the work of a few moments.

Perhaps that goes too far, but I do like how it's simplified my test code so far.

Modern Perl: The Book

cover image for Modern Perl: the book

The best Perl Programmers read Modern Perl: The Book.

sponsored by the How to Make a Smoothie guide

Categories

Pages

About this Entry

This page contains a single entry by chromatic published on September 23, 2011 1:21 PM.

Plack::Test and Great Test:: Interfaces was the previous entry in this blog.

Always TDD, Except When You Shouldn't is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.


Powered by the Perl programming language

what is programming?