Home > Articles > Programming

Random Testing: Monkeys and Gorillas

  • Print
  • + Share This
To help you find bugs, you can use tools and automation for the purpose of helping you run your test cases or, ideally, run your test cases automatically without the need for constant attention.
From the author of

From the author of

While automation tools do your regression testing, you have more time to plan new tests and design new and interesting cases to try. Another type of automated testing, though, isn't designed to help run or automatically run test cases. Its goal is to simulate what your users might do. That type of automation tool is called a test monkey.

The term test monkey comes from the idea that if you had a million monkeys typing on a million keyboards for a million years, statistically, they might eventually write a Shakespearean play, Curious George, or some other great work. All that random pounding of keys could accidentally hit the right combination of letters and the monkeys would, for a moment, look brilliant—much like the one in Figure 1.

Figure 1

Test monkeys will test forever as long as they have electricity and the occasional banana.

When your software is released to the public, it will have thousands or possibly millions of people randomly pounding away on it. Despite your best efforts at designing test cases to find bugs, some bugs will slip by and be found by those users. What if you could supplement your test case approach with a simulation of what all those users would do, before you released your product? You could potentially find bugs that would have otherwise made it past your testing. That's what a test monkey can do.

Dumb Monkeys

The easiest and most straightforward type of test monkey is a dumb monkey. A dumb monkey doesn't know anything about the software being tested; it just clicks or types randomly. Listing 1 shows an example of Visual Test code that will randomly click and type 10,000 times.

Listing 1: Just a Few Lines of Code Can Create a Dumb Monkey

1: RANDOMIZE TIMER
2: FOR i=1 TO 10000
3: PLAY "{CLICK "+STR$(INT(RND*640))+", "+STR$(INT(RND*480))+" }"
4: PLAY CHR$(RND*256)
5: NEXT i

Line 1 initializes the random numbers. Line 2 starts looping from 1 to 10,000 times. Line 3 selects a random point onscreen between 0,0 and 640,480 (VGA resolution) and clicks it. Line 4 picks a random character between 0 and 255 and types it in.

The software running on the PC doesn't know the difference between this program and a real person—except that it happens much more quickly. On a reasonably speedy PC it'll run in just a few seconds. Imagine how many random inputs you'd get if it ran all night!

Remember, this monkey is doing absolutely no verification. It just clicks and types until one of two things happens—either it finishes its loop or the software or the operating system crashes.

NOTE

If you don't believe that a dumb monkey can possibly find a serious bug, try running one on your favorite computer game or multimedia program. It's very likely that it won't last more than a few hours before crashing.

It doesn't seem to make sense that simple random clicking and typing could find a bug, but it does for a couple reasons:

  • Given enough time and attempts, just like the monkeys writing Shakespeare, the random inputs will eventually stumble onto a magic sequence that the programmers and testers didn't think of. Maybe the monkey enters some data and immediately deletes it or types in a huge string where a short one was expected. Who knows? It will find it, though.

  • A dumb monkey, with its continuous repetition and use, can expose bugs such as memory leaks that might not occur until many hours or days of normal use. If you've ever used software that seemed to become less and less stable the longer you used it, you've seen a problem that could have been found with a dumb monkey.

  • + Share This
  • 🔖 Save To Your Account