joewoodhouse30 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys, Think this is quite complicated to explain, but here goes!

I maintain an automation tool that basically runs standard Perl test scripts (TAP using Test::More). At the start of each script, the same operation is performed which is to connect to a set of servers that will be the target of the test. Basically something like this

use Test::More; use ServerConnection; my $connection = ServerConnection->new(); $connection->connect(); ok( 1==1, "A test"); # etc....
Im wondering if using Test::TAP::Model I can pass things to my test scripts - it seems impossible as I read the source.
Basically what I want to do is
# In the launch script my $connectionObject = ConnectionObject->new(); $connectionObject->connect(); my $model = Test::TAP::Model->new(); $model->run_tests( $connectionObject, @scripts_ptest );

Rather than doing the connection stuff at the top of each script (and wasting lots of time). So, as I probably should have said at the start, I want to re-use the same object across many different test scripts (I don't think it can be serialised as it contains ssh connections). Anyone have any ideas?

Many thanks, hope that made an ouce of sense! Joe

Replies are listed 'Best First'.
Re: Using Test::TAP::Model and a global object
by skx (Parson) on Oct 16, 2007 at 08:46 UTC

    I do something similar in the CMS that maintain, I have many tests which require a valid user account - and I create that on demand at the start of the tests.

    My solution was to created "user.utils" with content like this:

    BEGIN { use_ok( 'Yawns::User'); } require_ok( 'Yawns::User' ); BEGIN { use_ok( 'Yawns::Users'); } require_ok( 'Yawns::Users' ); sub createUser { ... } sub deleteUser { ... }

    Then each test which relies upon this:

    use Test::More qw( no_plan ); # # Utility functions for creating a new user. # require 'tests/user.utils'; # # Create a random new user. # my ($user, $username, $email, $password ) = setupNewUser(); #

    It doesn't solve the problem of repeating code in each test, but it does allow you to put the common code somewhere common even without being able to pass objects around properly.

    Steve
    --
      Hi steve, thanks for your reply

      Not sure if I made this clear, but I need to pass the same object because it contains connections (SSH and LDAP) that I want to re-use without re-connecting in every test script.

        Ahh I guess I wasn't being helpful then.

        I guess a potential fall-back plan would be to created a local singleton module to encapsulate your connection.

        Then each test could do something like:

        my $con = Singleton::Connection->instance();

        and this way only the first connection would connect - the latter calls would return the established object.

        Still just a hack instead of the real ability you want, but it might help.

        Steve
        --