http://www.perlmonks.org?node_id=1020292

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?

Replies are listed 'Best First'.
Re: Setting up a test db with MakeMaker
by choroba (Cardinal) 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;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      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.

      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". ;-)