Using Randomization in Java Unit Testing, Page 3
Limitations and Gotchas
The QaRandomizer is not perfect; it's far from it. Many areas of the project will benefit from improvement. As such, there are some gotchas to consider when you use the tool:
- Iterations take time: Some tests are dedicated to performance: can your code run as expected within an expected time slice? Tests such as these are designed to run fast and furious. Thus, if you are writing tests that utilize randomization within an iterative scenario, particularly tests that iterate over code hundreds of times, you run the risk of polluting the validity of your other tests.
To avoid this hazard and still allow your tests to have the time required to run their iterations, isolate tests that use iteration into a suite in a separate file. Doing so will allow more granular control over the entire testing scenario.
- Address data is real world for city, state, zip only: As mentioned previously, the QaRandomzer uses address data based on all the United States zip codes. The QaRandomizer gets a random zip code from this list and then, when the zip code is identified, looks up the corresponding city and state in the USPS list. However, address1 and address2 construction is fictitious. Thus, it is entirely possible to generate the following address:
Be advised, there is no Elm Street in the zip code 10001. Thus, please no not use QaRandomizer.getAddress() with the expectation that you'll be able to deliver snail mail to that address.
123 Elm Street Suite 400 New York, NY 10001
- Randomization is useful, but there are times when you should use constant data: Using randomization in your unit tests is appropriate in most situations, but not in all situations. There will be very specific times when you should use constant data to make sure every base is covered. Then, by all means, do so.
Get Random for Your Code's Sake
Working with random data used to be more work than it was worth. However, using the QaRandomizer in conjunction with some of the practices that I've described in this article, you'll be able to bring the power of randomization to your testing practices easily. Hopefully, that will make your life easier and your code better.
So in the spirit of writing great code, go forward and get random!
For Further Reading
About the AuthorBob Reselman is a Senior Technical Writer and Technical Editor for Edmunds Inc. Edmunds Inc. is a leading publisher of high volume, high availability, state of the art, Java-based web sites dedicated to empowering the automotive consumer.
Page 3 of 3