in reply to writing tests on modules

The usual set of test scripts are "regression tests," designed to make sure that nothing breaks when changes are made to your module or it's installed on another machine. They should test basic functionality, things that are likely to be incompatible on other systems, and things that are likely to break as you're making changes. An important part of maintaining these tests is making sure that as you fix bugs, you add tests to make sure the bugs don't come back.

As for how thorough you want to make your tests, that's up to you. If your bounds checking is essential to your module or you think it's likely to break, check it; otherwise skip it.

For testing random scripts, srand can be useful to make sure that rand will always return the same sequence while testing. You can also call the function you're testing several times, and make sure the results are consistent and not all the same.

Replies are listed 'Best First'.
Re^2: writing tests on modules
by xdg (Monsignor) on Nov 28, 2005 at 15:33 UTC
    For testing random scripts, srand can be useful to make sure that rand will always return the same sequence while testing. You can also call the function you're testing several times, and make sure the results are consistent and not all the same.

    Calling the function repeatedly isn't a very robust test and using srand isn't a good idea because the underlying random number generator isn't the same across all platforms. I wrote Test::MockRandom specifically to help testing code that relies on random numbers. It overloads CORE::GLOBAL::rand to return a number from a user-specified list. This isolates the randomness from the algorithm that utilitizes the random number, allowing for robust testing.

    As part of an article for The Perl Review last year, I wrote File::RandomLine as a simple example of how to use Test::MockRandom. Looking at the tests for that module might serve as a good "cookbook" tutorial.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.