![]() |
|
more useful options | |
PerlMonks |
Test Technique: Self-removing test databy markjugg (Curate) |
on Jul 18, 2007 at 02:13 UTC ( #627174=perlmeditation: print w/replies, xml ) | Need Help?? |
The problem: automating tested of a data-driven application requires creating test data to run the test, and removing the test data again when the test is finished.
The goal: Make the data clean up process automatic. The data should be cleaned up without an explicit call to a clean up function, and it should work even if the test script dies half-way through. The solution: As part of creating the test data, keep track of the IDs of the data created in a package-scoped array. Use an END {} block to call the cleanup function, using the the package-scoped array to know what to clean up. The END block gets called when the script is exiting, even if it is "die"'ing. Some example pseudo-code:
There you have it. Now just by creating some test data, it will automatically be removed when test is finished. I suggest putting a pair of functions like this in a private testing module and then importing the test data creation function.
Back to
Meditations
|
|