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

Hi all

Is there any difference between the following snippets of code and if so what is it and what would be recommended practice?

use Test::More; BEGIN { use_ok 'Module::A' }; BEGIN { use_ok 'Module::B' }; done_testing;

VS
use Test::More; BEGIN { use_ok 'Module::A'; use_ok 'Module::B'; }; done_testing;

Thanks :)


Smoothie, smoothie, hundre prosent naturlig!

Replies are listed 'Best First'.
Re: Multiple BEGIN blocks in Test::More
by Corion (Patriarch) on Nov 23, 2011 at 14:24 UTC

    I recommend to avoid use_ok at all. If they fail, all subsequent tests make no sense anway, so it is easier to just have the test bail out instead of continuing with bogus results.

Re: Multiple BEGIN blocks in Test::More
by ikegami (Patriarch) on Nov 23, 2011 at 17:18 UTC

    The two BEGIN blocks in your second snippet will do the same thing as the three BEGIN blocks in your first snippet, but they are not equivalent.

    1. Compile first BEGIN block.
      1. Compile require Test::More;.
      2. Compile import Test::More;.
    2. Execute first BEGIN block.
      1. Execute require Test::More;.
      2. Execute import Test::More;.
    3. Compile second BEGIN block.
      1. Compile use_ok 'Module::A'.
    4. Execute second BEGIN block.
      1. Execute use_ok 'Module::A'.
    5. Compile third BEGIN block.
      1. Compile use_ok 'Module::B'.
    6. Execute third BEGIN block.
      1. Execute use_ok 'Module::B'.
    7. Compile done_testing;.
    8. End of compile phase.
    9. Execute done_testing;.

    vs

    1. Compile first BEGIN block.
      1. Compile require Test::More;.
      2. Compile import Test::More;.
    2. Execute first BEGIN block.
      1. Execute require Test::More;.
      2. Execute import Test::More;.
    3. Compile second BEGIN block.
      1. Compile use_ok 'Module::A'.
      2. Compile use_ok 'Module::B'.
    4. Execute second BEGIN block.
      1. Execute use_ok 'Module::A'.
      2. Execute use_ok 'Module::B'.
    5. Compile done_testing;.
    6. End of compile phase.
    7. Execute done_testing;.

    Or in short,

    1. ...
    2. Compile use_ok 'Module::A'.
    3. Execute use_ok 'Module::A'.
    4. Compile use_ok 'Module::B'.
    5. Execute use_ok 'Module::B'.
    6. ...

    vs

    1. ...
    2. Compile use_ok 'Module::A'.
    3. Compile use_ok 'Module::B'.
    4. Execute use_ok 'Module::A'.
    5. Execute use_ok 'Module::B'.
    6. ...

    In the first, use_ok 'Module::A' could have an effect on how use_ok 'Module::B' is compiled. In the second, it can't.

    I agree with Corion, though. There's no reason to use use_ok.

    use Test::More tests => 1; use Module::A; use Module::B; pass("Loading modules"); 1;
Re: Multiple BEGIN blocks in Test::More
by j1n3l0 (Friar) on Nov 24, 2011 at 11:54 UTC
    Thank you both for your comments :)


    Smoothie, smoothie, hundre prosent naturlig!