Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Sharing data between tests?

by ghenry (Vicar)
on Jan 31, 2012 at 21:06 UTC ( [id://951068]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,

I've almost completed the first version of the SureVoIP RESTful hypermedia API and wanted some advice on sharing data between separate tests.

I'm working on the create customer method (POST to /customers) and once I've returned "201 Created" I'll have the Location of the new customer resource. How should I save this and use it in all my other tests? Config::Tiny? Storable?

What do you recommend? I can't see anything apart from Temp databases in Perl Testing: A Developer's Notebook and nothing in Test::More

I'm using Catalyst, Catalyst::Controller::REST, PostgreSQL and DBIx::Class

Thanks, Gavin.

Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a yourname@perl.me.uk? Just ask!!!

Replies are listed 'Best First'.
Re: Sharing data between tests?
by chromatic (Archbishop) on Jan 31, 2012 at 21:24 UTC

    I use fixtures with a temporary database (DBICx::TestDatabase). I usually create a module under t/lib/ which performs bulk inserts of the relevant data.

    I don't create data in one test and use it in another because that implies an ordering between tests that can be fragile, and works against test parallelism.

    If you need more details, let me know; I can post code when I have more time.

      Thanks. Maybe I should just do the create user in one test and verify it's there and then use a known user in all the other tests.

      They don't have to be ordered, but it would be good to check the freshly created user and do everything the API allows using said user.

      It's things like update their own info, order telephone numbers, send a fax, send an SMS, email to fax, make a call etc. etc.

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!
        Maybe I should just do the create user in one test and verify it's there and then use a known user in all the other tests.

        That's what I do. Many of my hardcoded expectations eventually bite me later.

Re: Sharing data between tests?
by Util (Priest) on Jan 31, 2012 at 21:31 UTC

    Unless efficiency is truly critical, I would avoid Storable, along with any other binary format. Test suites (both code and data) are a form of documentation, and should be in a human-readable format.

    Even when the datasets are generated during one test, and (normally) deleted later toward the end of the test suite, a human-readable (and editable) datastore will save you much frustration when diagnosing a failed test.

    I have used Data::Dumper, YAML, and Config::Tiny on different projects. I think that Config::Tiny is better suited to true config files that a non-programmer will be editing. If I was saving shared test data today, JSON::XS would be my first choice.

    Note that you need not just bundle the data. You can also bundle helper subroutines, that might simply return the data, or might *generate* the data in some expanded format that your tests can easily use, rather than whatever compact format the data might actually live in.

Re: Sharing data between tests?
by JavaFan (Canon) on Jan 31, 2012 at 21:46 UTC
    If it's just a single URL? I'd write it in a plain text file. Why bother with a framework if all you need is a single piece of data?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://951068]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-19 20:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found