in reply to Test::Class with Test::Spec's sugar ?

I haven't used either Test::Spec or Test::Class previously. I managed to get these interacting at a very basic level: calling Test::Class::->runtests; from within a Test::Spec it() function.

The main script:

ken@ganymede: ~/tmp $ cat pm_class_spec_comb.pl #!/usr/bin/env perl use strict; use warnings; use Test::Spec; use Test::Class::Example::Hello::Tests; it 'runs Test::Spec and Test::Class tests' => sub { diag('Hello, world! (from Test::More::diag() as: diag())'); print qq{Hello, world! (from Test::Spec::it() as: it CODE\n}; Test::Class::->runtests; diag('Last it() statement.'); }; runtests unless caller;

The Test::Class::Example::Hello::Tests module:

ken@ganymede: ~/tmp/Test/Class/Example/Hello $ cat Tests.pm package Test::Class::Example::Hello::Tests; use base 'Test::Class'; sub test_Hello : Test { print qq{Hello, world! (from Test::Class as: sub test_Hello : Test +\n}; return 1; } 1;

Sample run:

$ pm_class_spec_comb.pl # Hello, world! (from Test::More::diag() as: diag()) Hello, world! (from Test::Spec::it() as: it CODE 1..1 Hello, world! (from Test::Class as: sub test_Hello : Test ok 1 # skip 1 # Last it() statement.

I'll leave you to experiment further.

-- Ken

Replies are listed 'Best First'.
Re^2: Test::Class with Test::Spec's sugar ?
by mascip (Pilgrim) on Jul 14, 2012 at 08:05 UTC

    Thank you :o)

    It's quite unexpected, but also not quite what i expected.
    What you did here is run a Test::Class inside a Test::Spec.
    What i would like would be to write a Test::Class in Test::Spec style, with the "describe/it" nested structure.

    I still tried to add a "describe" assertion in the Test::Spec file, and a Test::More::ok() assertion into the Test.pm file, hoping that the TAP would produce a mixed output (i'm not sure how it would be useful, but i'm still interested by the experiment). But it didn't.
    I added in the pm_class_spec_comb.pl file :

    describe 'A mixed Test' => sub { it 'runs Test::Spec and Test::Class tests' => sub { [...] }; };
    and in the Test.pm file :
    use Test::More;
    and
    ok(1);
    and it printed
    ok 1 - test Hello
    which is the normal Test::Class output. While i was hoping for it to write something mixed, like
    "A mixed Test runs Test::Spec and Test::Class tests test Hello"

    But anyway, this would still not be what i want.

    Let me try to rephrase what i want.
    In Test::Spec i can build "nested statements". For example,

    describe 'A Fruit' => { [general tests on a Fruit, here] describe 'attached to its branch' => { [tests on Fruits which are still on their branch] [example :] it 'grows' => sub { ok(1); } }; describe 'fallen on the floor' => { [tests on Fruits which have fallen on the floor] }; describe 'on a pie' => sub { [tests on Fruits which are on a pie] }; };
    And in each section, the output for each assertion will start with the corresponding context. For example i will read :
    ok 1 - A fruit attached to its branch grows
    This nested structure also enables nested declaration of the variables that i need for my tests. So, all the tests that are grouped together share the same variables, which can be re-initialized before each test, with the "before each" function. This way i don't need a $test variable to reinitialise and carry variables that are used in several tests : i can directly use my variables in several tests

    See the @roll variable in my Game::Bowling example, for example. With Test::Class i would need to use $test->{roll} all the time, and i think i wouldn't be able to initialize it differently for different groups of tests.

    I don't know if such a thing can be achieved with Test::Class.
    I should maybe write my Game::Bowling example with Test::Class, to give a comparizon.