Hi all,
I'm a test-beginner, what i say might be stupid/naive.
Test::Class is what people use for OO testing, and it seems very good at it (i barely used it, but read Ovid's instructions carefully).
Test::Spec is used for testing scripts, and i love its sugar functions.
My question is :
would there be any way to use Test::Spec with Test::Class?
(I'm almost sure that the answer is "no", but wish someone could show me the contrary.)
Or any Test::* framework which would go in that direction ?
Here is a small example of what Test::Spec does (PS : i just realized that the example in the POD documentation is simpler and clearer - just click the link). I very much like it's simple, descriptive, and nested structure. Which i prefer to Test::Class's $test variable, which caries all the variables that i will use in different tests, and doesn't read well.
#!/usr/bin/perl
use 5.012003; # Perl version requirement
+
use strict;
use warnings;
use Game::Bowling;
use Test::Spec 0.45;
describe "A Game::Bowling" => sub {
my $game;
my @rolls;
my $expected_score;
before each => sub {
$game = new Game::Bowling;
@rolls = (2)x20;
$expected_score = 40;
};
describe 'counts that the score is' => sub {
it '0, if 0 pins are shot' => sub {
@rolls = (0)x20;
is $game->played_with_rolls(\@rolls)->score(), 0;
};
it '40, if 2 pins are shot at each roll' => sub {
is $game->played_with_rolls(\@rolls)->score(), $expected_s
+core;
};
it '41, if 1 more pin is shot at one roll' => sub {
$rolls[5] = 3;
$expected_score += (-2+3);
is $game->played_with_rolls(\@rolls)->score(), $expected_s
+core;
};
};
describe 'takes into account a spare' => sub {
it 'in the 2nd frame' => sub {
@rolls[3,4] = (8,5);
$expected_score += (-2+8+5) + (-2+5);
is $game->played_with_rolls(\@rolls)->score(), $expected_s
+core;
};
it 'in the last frame' => sub {
$rolls[19] = 8;
push @rolls, 3;
$expected_score += (-2+8+3) + 3;
is $game->played_with_rolls(\@rolls)->score(), $expected_s
+core;
};
};
describe 'takes into account a strike' => sub {
it 'in the 2nd frame' => sub {
@rolls[2,3,4] = (10,7,1);
$expected_score += (-2-2+10+7+1) + (-2+7) + (-2+1);
pop @rolls;
is $game->played_with_rolls(\@rolls)->score(), $expected_s
+core;
};
it 'in the last frame' => sub {
@rolls[18,19] = (10,7);
push @rolls, 2;
$expected_score += (-2-2+10+7+2) + (+7) + (+2);
is $game->played_with_rolls(\@rolls)->score(), $expected_s
+core;
};
};
describe 'with zero players' => sub {
it 'is not interesting' => sub {
ok(1);
};
};
};
runtests unless caller;
produces this output :
ok 1 - A Game::Bowling counts that the score is 0, if 0 pins are shot
ok 2 - A Game::Bowling counts that the score is 40, if 2 pins are shot
+ at each roll
ok 3 - A Game::Bowling counts that the score is 41, if 1 more pin is s
+hot at one roll
ok 4 - A Game::Bowling takes into account a spare in the 2nd frame
ok 5 - A Game::Bowling takes into account a spare in the last frame
ok 6 - A Game::Bowling takes into account a strike in the 2nd frame
ok 7 - A Game::Bowling takes into account a strike in the last frame
ok 8 - A Game::Bowling with zero players is not interesting
1..8
I've tried to do the same with Test::Class and it's a lot less friendly to read. Yes, i know, you wouldn't use Test::Class for such a simple example. But still, when i will need it on bigger examples, i'd like it to be reader-friendly.
~ ~ ~
A few hours later, i ate, relaxed, and searched a bit more on the Tinterweb.
(i tend to do this, late, and thus answer my own questions myself)
Test::More allows nested subtests, with the "subtest" keyword. But it's not "sweet" enough : i still prefer Test::Spec sugar. But it might make my variables declaration nicer, i have to try on monday.
I really don't like the $test hash in Test::Class : @{$test->{rolls}} is a lot less nice to read than @rolls. 1nd i'm lazy, to have to copy the variables that i need from $test, at the beginning of each test.
Test::Group enables to group subtests together, and if a whole subtests-group passes, then only one line is displayed, to say that the test was passed. I might try it. It's nice for the output, but doesn't help to make the test code sweeter.
I'm wondering if by subclassing Test::Class with My::Test::Class, by grouping tests in "Tests" subgroups, and creating my own simple sugar functions, i could write something that feels a bit like Test::Spec...