Making Test::Class More Like xUnit

Ovid on 2009-02-22T15:09:36

In xUnit style tests, this is an entire test:

sub first_name : Tests(tests => 3) {
    my $test   = shift;
    my $person = $test->class->new;
    can_ok $person, 'first_name';
    ok !defined $person->first_name,
      '... and first_name should start out undefined';
    $person->first_name('John');
    is $person->first_name, 'John', '... and setting its value should succeed';
}

In the TAP world, we would look at this as three tests, but xUnit says we have three asserts to validate one feature, thus we have one test. Now TAP-based tests have a long way to go before working for xUnit users, but there's one thing we can do. Let's say that you have a test with 30 asserts and the fourth assert fails. Many xUnit programmers argue that once an assert fails, the rest of the information in the test is unreliable. Thus, the tests should be halted. Now regardless of whether or not you agree with this (I hate the fact that, for example, junit requires the test method to stop), you can get this behavior with Test::Class. Just use Test::Most instead of Test::More and put this in your test base class:

BEGIN { $ENV{DIE_ON_FAIL} = 1 }

Because each test method in Test::Class is wrapped in an eval, that test method will stop running and the tests will resume with the next test method.


Nice...

Adrian on 2009-02-22T15:34:17

Patches to the non-existent Test::Class::Cookbook are welcome :-)

Re:Nice...

Ovid on 2009-02-22T15:55:01

The reason I'm posting this is because I'm rewriting my Test::Class article. I never published it and now am trying to figure out where. I've also considered putting out a Test::Class::Base module which uses the features I'm describing and makes life just a tad easier for Test::Class users.

Re:Nice...

Adrian on 2009-02-22T16:06:47

Super!

One of the things I'd like to do at the qa-hackathon (assuming nothing more important shows up). Is clear up the T::C internals which... y'know.... suck...

If the Beeb decides you can go - we should chat.

Re:Nice...

Ovid on 2009-02-22T16:26:10

It's now been approved that I can go. See you in Birmingham :)

Re:Nice...

chromatic on 2009-02-22T18:00:38

I never published it and now am trying to figure out where.

If you're still looking for a place, Modern Perl Books is happy to feature guest authors.

Re:Nice...

Ovid on 2009-02-22T18:13:35

Is it OK if it's published elsewhere? I've already had someone offer to publish it in another language. I'll need to get their approval if I go that route, though.

I should probably rewrite the intro if it's for Moderl Perl Books. I'd want to better tailor it for what you're trying to do with that site.