Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Setting up a test db with MakeMaker

by danmcb (Monk)
on Feb 23, 2013 at 09:37 UTC ( #1020292=perlquestion: print w/ replies, xml ) Need Help??
danmcb has asked for the wisdom of the Perl Monks concerning the following question:

I am working on a catalyst project, and want to cause some actions to be automatically executed on execution of "make test" or "prove". (This would be setting an ENV variable to cause the app to connect to a test database, initialising the db, and so on.

A dirty solution would be to simply put these actions in a script and call it at the top of every *.t file. (Actually maybe this is not so bas as it ensures that each test file starts with a clean test database, and gets rid of interaction between test files.) But is there a better way?

Comment on Setting up a test db with MakeMaker
Re: Setting up a test db with MakeMaker
by choroba (Abbot) on Feb 23, 2013 at 09:46 UTC
    If you need a code to be called from each .t file, what about wrapping it into a module? You can even place it into the t/ directory and just add the following to each .t file:
    use TestSetup;
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      what about wrapping it into a module? You can even place it into the t/ directory and just add the following to each .t file: use TestSetup;

      Does that work on 5.6?

        Does that work on 5.6?

        Why should it not work? Why don't you try it?

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      indeed, this works fine. Some code for those that are curious:

      in t/test_setup.pm:

      package test_setup; sub setup { # setup your db or whatever here } 1;

      In a test module

      require t::test_setup; test_setup::setup();

      Rather simple in fact. Works fine on 5.10

      UPDATE: in fact - I am using this.

      package test_setup; sub setup { # ... } BEGIN { # must be after the sub definition setup(); $ENV{USE_TEST_DB} = 1; } 1;

      And in my Catalyst MyApp.pm class:

      if ( $ENV{USE_TEST_DB} ) { __PACKAGE__->config( 'Plugin::ConfigLoader' => { file => 'myapp_test +.yml'} ); }

      Now it is only necessary to "use" test_setup.pm. The setup is executed automatically, and the ENV variable causes the Catalyst app to use another config, which in turn connects to the test database in place of the normal development one.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (14)
As of 2014-11-25 20:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (158 votes), past polls