Standalone Test Roles

The real power of Test::Roo is decomposing test behaviors into roles that can be reused.

Imagine we want to test a file-finder module like Path::Iterator::Rule. We could put tests for it into a role, then run the tests from a file that composes that role. For example, here would be the test file:

MODIFIERS ON TESTS

Global modifiers with each_test

Modifying each_test triggers methods before or after every test block defined with the test function. Because this affects all tests, whether from the test class or composed from roles, it needs to be used thoughtfully.

Here is an example that ensures that every test block is run in its own separate temporary directory.

Wrapping tests

As a middle ground between global and individual modifiers, if you need to call some code repeatedly for some, but not all all tests, you can create a custom test function. This might make sense for only a few tests, but could be helpful if there are many that need similar behavior, but you can't make it global by modifying each_test.

The following example clears the fixture before tests defined with the fresh_test function.