<?xml version="1.0" encoding="windows-1252"?>
<node id="923392" title="Order of startup methods with Test::Class" created="2011-08-31 06:19:40" updated="2011-08-31 06:19:40">
<type id="115">
perlquestion</type>
<author id="869476">
chrestomanci</author>
<data>
<field name="doctext">
&lt;p&gt;Greetings brothers.&lt;/p&gt;

&lt;p&gt;I am currently writing a test suite for a [mod://DBIx::Class] based project using [mod://Test::Class]. I am having trouble with the order that Test::Class calls the startup methods I have defined.&lt;/p&gt;

&lt;c&gt;
package Test::Setup;
use base 'Test::Class';
use Test::Most;	# Pulls in strict, warnings, T::More, T::Exception etc.

use Schema;			# The base of my DBIx::Class classes.

sub db_connect : Test(startup)
{
  my $self = shift;
  my $schema = Schema-&gt;connect('dbi:SQLite:dbname=:memory:');
  $schema-&gt;deploy();
  $self-&gt;{'schema'} = $schema;
};

package Test::Schema::Result::Organisation;
use base 'Test::Setup';
use Test::Most;

# This does not work!
# I expected it to get called after the parent class startup method, but instead it gets called first,
# before the startup method in Test::Setup so there is no DB connection and the schema is not defined.
#
# Test::Schema::Result::Organisation::create_organisation
sub create_organisation : Test(startup)
{
  my $self = shift;
  my $schema = $self-&gt;{'schema'};
    
  $self-&gt;{'Organisation'} = $schema-&gt;resultset('Organisation')-&gt;create({
    'name'         =&gt;  'BigCheeseIndustries',
    'description'  =&gt;  'Test Organisation for the test suite',
  });
  return;
};
&lt;/c&gt;

&lt;p&gt;In the example above, the &lt;c&gt;create_organisation&lt;/c&gt; method gets called before the &lt;c&gt;db_connect&lt;/c&gt; method that it depends on. I had expected Test::Class to respect the inheritance order of my test classes, and call the parent startup methods before the children, but that does not appear to be the case.&lt;/p&gt;

&lt;p&gt;I have read in the [http://search.cpan.org/~adie/Test-Class-0.36/lib/Test/Class.pm#RUNNING_ORDER_OF_METHODS|Test::Class docs], that startup methods are run in alphabetical order, and I am able to fix the order by prefixing the method names with A_, B_ etc, but this feels like a crude hack. Is there a smarter way?&lt;/p&gt;</field>
</data>
</node>
