Random testing is a black-box software testing technique where programs are tested by generating random inputs. Results of the output are compared against software specifications to verify that the test output is pass or fail. In case of absence of specifications the exceptions of the language are used which means if an exception arises during test execution then it means there is a fault in the program.
Overview
Consider the following C++ function:
int myAbs(int x) {
if (x>0) {
return x;
}
else {
return x; // bug: should be '-x'
}
}
Now the random tests for this function could be {123, 36, -35, 48, 0}. Only the value '-35' triggers the bug. If there is no reference implementation to check the result, the bug still could be noticed. However, an assertion could be added to check the results, like:
void testAbs(int n) {
for (int i=0; i<n; i++) {
int x = getRandomInput();
int result = myAbs(x);
assert(result>=0);
}
}
The reference implementation is sometimes available, e.g. when implementing a simple algorithm in a much more complex way for better performance. For example, to test implementation of Schönhage–Strassen algorithm algorithm, standard "*" operation on integers can be used:
int getRandomInput() {
. . .
}
void testFastMultiplication(int n) {
for (int i=0; i<n; i++) {
long x = getRandomInput();
long y = getRandomInput();
long result = fastMultiplication(x, y);
assert(x*y==result);
}
}
See also
- Fuzz testing - a kind of random testing when invalid input is provided to tested program.
External links
- Random testing by Andrea Arcuri.
- Random Testing wiki at Cunningham & Cunningham, Inc.