in reply to Re: writing tests on modules
in thread writing tests on modules

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.