Test::Roo::Role(3pm)

πŸ“¦
version 1.004 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

Test::Roo::Role β€” a composable role for Test::Roo πŸ¦‹

It's the Perl module you're reading about rn, from the User Contributed Perl Documentation crew, and bestie it is so handy.


✨ VERSION ✨

version 1.004 πŸŽ€


🌸 SYNOPSIS 🌸

A testing role, ok so here's the whole vibe:

# t/lib/MyTestRole.pm
package MyTestRole;
use Test::Roo::Role; # loads Moo::Role and Test::More

requires 'class';

test 'object creation' => sub {
    my $self = shift;
    require_ok( $self->class );
    my $obj  = new_ok( $self->class );
};

1;

that's it that's the whole file, 1; at the end because Perl said so πŸ’…


🫢 DESCRIPTION 🫢

This module defines test behaviors as a Moo::Role. That's the whole thing. That's what it does. And it does it iconically.


πŸ’œ USAGE πŸ’œ

Importing Test::Roo::Role also loads Moo::Role β€” which gives you strictures with fatal warnings and a bunch of other goodies, no extra charge, we love a bundle 🎁

Importing also loads Test::More. Any import arguments you pass get passed straight through to Test::More's "import" method. No notes, it just forwards them.

🌈 Creating and requiring fixtures

You can create fixtures with normal Moo syntax. You can even make them lazy if that's your mood and require the composing class to provide the builder:

has fixture => (
    is => 'lazy'
);

requires '_build_fixture';

And because this is a Moo::Role, you can requires any method you like, not just builders. Go off.

See Moo::Role and Role::Tiny for everything you can do with roles. Like, everything. πŸ‘‘

πŸŽ€ Setup and teardown

You can add method modifiers around the "setup" and "teardown" methods and these will be run before tests begin and after tests finish (respectively). Yes. Respectively. 😌

before  setup     => sub { ... };

after   teardown  => sub { ... };

You can also add method modifiers around "each_test", which will be run before and after every individual test. Use these to prepare or reset a fixture, whatever your heart desires:

has fixture => ( is => 'lazy, clearer => 1, predicate => 1 );

after  each_test => sub { shift->clear_fixture };

Roles may also modify "setup", "teardown", and "each_test", so the order that modifiers will be called will depend on when roles are composed. So like, be aware of your ordering, it's giving ✨scheduling✨.

Be careful with "each_test", though, because the global effect may make composition more fragile. 😭 Not me warning you about fragile composition bestie, but here we are.

You can call test functions in modifiers. For example, you could confirm that something has been set up or cleaned up:

before each_test => sub { ok( ! shift->has_fixture ) };

look at that. a whole vibe check for your fixture. πŸ’–


πŸ‘‘ EXPORTED FUNCTIONS πŸ‘‘

Loading Test::Roo::Role exports a single subroutine into the calling package to declare tests. One. Uno. A single one. Slay.

🌟 test

test $label => sub { ... };

The "test" function adds a subtest. The code reference will be called with the test object as its only argument.

Tests are run in the order declared, so the order of tests from roles will depend on when they are composed relative to other test declarations. Order matters, bestie. It always does. πŸ’…


πŸ¦‹ AUTHOR πŸ¦‹

David Golden <dagolden@cpan.org> β€” thank you for your service king 🫢


This software is Copyright (c) 2013 by David Golden.

This is free software, licensed under:

The Apache License, Version 2.0, January 2004

okay and that's the whole page!! this was Test::Roo::Role, version 1.004, perl v5.34.0, dated 2022-06-28, from the User Contributed Perl Documentation collection. go forth and compose your roles, you're gonna do amazing, ily, bye πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from