Michael G Schwern > Test-Simple-1.005000_006 > TB2::Tester



Annotate this POD


View/Report Bugs
Module Version: 1.005000_006   Source  


TB2::Tester - Testing a Test:: module


    use Test::More;
    use Your::Test::Module qw(thing_ok that_ok);
    use TB2::Tester;

    my $capture = capture {
        thing_ok $thing, "some name";
        that_ok $that;

    my $results = $capture->results;

    # The first one passed, and it has a name
    result_like shift @$capture, {
        is_pass => 1,
        name => "some name",

    # The second one failed, and it has no name
    result_like shift @$capture, {
        is_pass => 0,
        name => ''


This is a module for testing Test modules.


These are exported by default


    my $capture = capture { ...test code ... };

Captures all the events and results which happens inside the block.

Returns a TB2::History that you can reference later. This is disassociated from any other tests, so you do what you like to it without altering any other tests.


  event_like( $event, $want );
  event_like( $event, $want, $name );

Tests that a $result looks like what you $want.

$want is a hash ref of keys and values. Each of these will be checked against the $result's attributes. For example...

    result_like( $result, { name => "foo" } );

will check that $result->name eq "foo".

Values can also be regular expressions.

    result_like( $result, { name => qr/foo/ } );

will check that $result->name =~ /foo/.


  result_like( $result, $want );
  result_like( $result, $want, $name );

Works just as event_like but it also checks the $result is a result.


Avoid hardcoding the test sequence

    my $results = $history->results;
    result_like $results->[0], {
        is_pass => 0,
        name    => "this is that",
        file    => $0,
    result_like $results->[1], { ... }
    result_like $results->[2], { ... }

The drawback with using array indices to access individual results is that once you decide to add or remove a test from any place but the very end of the test list, your array indices will be wrong and you'll have to update them all.

To preclude this issue from arising, simply shift the individual results off the result list, like this:

    result_like shift @$results, { ... }; # check first result
    result_like shift @$results, { ... }; # next one, and so on

Now you only have to add checks symmetrically with your new tests - existing lines won't have to be edited. It's safe to modify $results because it is only the results for your captured tests.

syntax highlighting: