Automated Software Testing For MATLAB (2009)
Automated Software Testing For MATLAB (2009)
engineering
A
lthough they wouldn’t describe them- understand the need for software testing, but
selves as programmers, most science might not know how to go about it. This observa-
and engineering researchers routinely tion agrees with other reports that scientists and
write software to perform their work. engineers tend to pick up computing knowledge
Indeed, in one survey, scientists reportedly on their own and have relatively little software en-
spent about 30 percent of their work time writ- gineering experience.6–8
ing software.1 Software bugs, therefore, have se- The practice of unit testing can help improve
rious implications for the reliability of scientific the quality of science and engineering software.
and engineering research. One study comparing Unit tests focus on small units of code, such as a
nine seismic data processing packages showed that class, module, method, or function. To be most
seismic traces were frequently shifted incorrectly effective, unit tests should be automated so that
by off-by-one indexing errors. Even after these entire test suites can be run quickly and easily
faults were corrected, the various program out- and so that the developer doesn’t have to review
puts sometimes agreed to within only a single the program output to determine whether the
significant digit, mostly because of bugs.2 Fur- tests passed or failed. Here, I illustrate these ideas
ther, several journal papers have been retracted and how to practically apply them using Matlab
recently because of bugs.3,4 For example, authors xUnit, a testing framework available for free
had to retract a widely cited protein-structure pa- download at www.mathworks.com/matlabcentral/
per in Science (as well as several related papers) fileexchange/22846.
because of a column-swapping bug in a data-
analysis program.5 The paper’s incorrect results Test Writing Basics
confused other researchers and apparently even To get started with Matlab xUnit, you first cre-
some grant panels. ate a test file containing one or more test cases.
In professional programming practice, soft- Figure 1 shows a test file, test_upslope_area.m,
ware testing is a fundamental tool for improv- that tests the function upslope_area, which be-
ing software quality. From my own experience longs to a collection of hydrology functions avail-
interacting with Matlab users, people often able on the Matlab Central File Exchange. This
function collection includes a test suite written
1521-9615/09/$26.00 © 2009 IEEE using Matlab xUnit. (Conventional practice is to
Copublished by the IEEE CS and the AIP write tests after the software is written. Increas-
ingly, professional software developers are turning
Steven L. Eddins
this convention on its head; see the “Test-Driven
The MathWorks, Inc. Development” sidebar.)
Test Files
As lines 1-2 of Figure 1 show, the first function in 1 function suite = test_upslope_area
initTestSuite;
a test file always has the same form. Line 1 shows 2
function finds all the test cases in all the test files
in your current working directory, gathers them Figure 1. Sample test file. The top function,
into a test suite, runs the test suite, and displays test_upslope_area, initializes the test suite.
The functions test_normalCase, test_
the results. Figure 2 shows the output of run emptyInput, and test_constantInput each
tests for Matlab xUnit’s own test suite. As the form one test case. (Used with permission from The
figure shows, runtests displays the number of MathWorks, Inc. All rights reserved.)
November/December 2009 49
and determine the expected outputs. For an ex-
ample, see Figure 1 lines 4–22. Next, consider
inputs at and near the end of the allowed data
range (that is, conduct a boundary analysis 9). These
test cases help identify off-by-one errors, such as
writing N instead of N-1 or vice versa.
With more experience, you can gain insight
into coding-error patterns and add test cases
that are likely to expose such errors. Experienced
Figure 2. Running tests. The function runtests Matlab quality engineers, for example, routinely
automatically runs all tests and determines whether
they pass or fail. (Used with permission from The
write test cases with empty input matrices or NaN
MathWorks, Inc. All rights reserved.) and Inf values. From my own experience with
image processing software, I anticipate possible
coding errors for constant-valued images or those
that have a single row or column. Lines 24–38 of
Figure 1 show a couple of error-guessing9 test
cases for the upslope_area function.
November/December 2009 51
program behavior. The first three test cases verify The actual difference between the two expres-
the correct output for three different inputs (ICC, sions is very small, but not zero:
d50, and d65). The fourth test case (lines 16-17)
verifies the inputs’ case insensitivity, and the fifth >> (3/14 + (3/14 + 15/14)) ...
test case (lines 19-20) verifies the correct default – ((3/14 + 3/14) + 15/14)
behavior. The five tests cases are easy to under- ans =
stand at a glance and have specific names that -2.2204e-016
clearly communicate the programmer’s intent.
Because the order of operations matters, two
Using Random Values Appropriately mathematically equivalent computational pro-
Novice test writers often use randomly generated cedures can produce different answers when im-
input data for their test cases. As Gerard Meszaros plemented using floating-point arithmetic. Even
notes, this kind of nondeterministic test case can identical code can produce different answers on
be difficult to debug because it can be hard to re- different computers, or when compiled using dif-
produce the failure.10 In the worst case, a particular ferent compilers, because of optimization varia-
test failure might never appear again despite nu- tions or differing versions of underlying runtime
merous test runs. To avoid frustration when using libraries, such as the basic linear algebra subpro-
randomly generated input data, write your test to grams (BLAS).
use the same set of randomly generated input data For testing, then, we should usually avoid exact
for every test run by using a fixed “seed” for the equality tests when checking floating-point results
random number generator. Alternatively, if you and instead use some sort of tolerance. There are
want to use a different seed for every test run, two types of floating-point tolerance tests com-
display or save the seed state so that if you have a monly used: absolute and relative. Absolute toler-
test failure, you can reproduce and debug it. ance tests for two values a and b:
November/December 2009 53
Adapting xUnit for Procedural Programming Chang and Roth, Science 293 (5536) 1793–1800,”
Most Matlab code today is procedural, and most Science, vol. 314, no. 5807, 2006, p. 1875; www.
scientists and engineers who use Matlab prefer sciencemag.org/cgi/content/full/314/5807/1875b.
procedural programming. Although implemented 4. B.G. Hall and S.J. Salipante, “Retraction: Measures of
in a fully object-oriented xUnit fashion, Matlab Clade Confidence Do Not Correlate with Accuracy
xUnit was designed from the start to be used by of Phylogenetic Trees,” PLoS Computational Biology,
procedural programmers and its documentation vol. 3, no. 3, 2007; www.ploscompbiol.org/article/
focuses on procedural test writing. info:doi%2F10.1371%2Fjournal.pcbi.0030051.
The FunctionHandleTestCase—which is 5. G. Miller, “A Scientist’s Nightmare: Software Problem
a TestCase subclass—supports the procedural Leads to Five Retractions,” Science, vol. 314, no. 5807,
testing style. The helper script initTestSuite 2006, pp. 1856–1857.
(see Figure 1 line 2) automatically turns each test 6. G. Wilson, “What Should Computer Scientists
function in the file into a FunctionHandleTest Teach to Physical Scientists and Engineers?” IEEE
Case object and returns the collection of them as Computational Science & Eng., vol. 3, no. 2, 1996,
a TestSuite object. pp. 46–65.
7. D.E. Stevenson, “A Critical Look at Quality in Large-
I
Scale Simulations,” Computing in Science & Eng.,
f you’re a scientist or engineering research- vol. 1, no. 3, 1999, pp. 53–63.
er, you probably don’t regard yourself as a 8. D. Kelly and R. Sanders, “The Challenge of Testing
professional software developer. Still, you Scientific Software,” Proc. Conf. Assoc. Software Testing:
probably write software to get your work Beyond the Boundaries (CAST 2008), Assoc. Software
done. That software’s quality significantly affects Testing, 2008; www.associationforsoftwaretesting.
the quality of your research. Matlab is widely org/documents/cast08/DianeKellyRebeccaSanders_
used for research in many science and engineer- TheChallengeOfTestingScientificSoftware_paper.pdf.
ing disciplines; the availability of an industry- 9. S. McConnell, Code Complete: A Practical Handbook of
standard testing approach for software created Software Construction, 2nd ed., Microsoft Press, 2004.
with Matlab can help you incorporate automated 10. G. Meszaros, xUnit Test Patterns: Refactoring Test
software testing into your regular practice. By do- Code, Pearson Education, 2007.
ing so, you can increase your confidence in both 11. C. Moler, “Floating Points: IEEE Standard Unifies
your software and in the accuracy of the research Arithmetic Model,” Cleve’s Corner, The MathWorks,
results based upon it. 1996; www.mathworks.com/company/newsletters/
news_notes/pdf/Fall96Cleve.pdf.
Acknowledgments 12. D. Goldberg, “What Every Computer Scientist
Special thanks to Greg Wilson and Gerard Meszaros Should Know About Floating-Point Arithmetic,”
for their helpful comments on Matlab xUnit’s design Computing Surveys, vol. 23, no. 1, 1991, pp. 5–48.
and architecture, as well as to Mara Yale, Rob Comer, 13. E. Gamma and K. Beck, “JUnit Test Infected: Pro-
and Bill McKeeman for their suggestions on this ar- grammers Love Writing Tests,” Source Forge, 1998;
ticle. I also thank the anonymous reviewers for their http://junit.sourceforge.net/doc/testinfected/testing.
helpful and constructive comments. Matlab is a reg- htm.
istered trademark of The MathWorks, Inc.
Steven L. Eddins manages the image processing and
References geospatial computing software development team at
1. J.E. Hannay et al., “How Do Scientists Develop and The MathWorks, Inc. He is coauthor of Digital Im-
Use Scientific Software?” Proc. 2nd Int’l Workshop age Processing Using Matlab (Gatesmark Publish-
Software Eng. Computational Science and Eng., IEEE CS ing, 2009). Eddins has a PhD in electrical engineering
Press, 2009, pp. 1–8. from the Georgia Institute of Technology. He’s a
2. L. Hatton, “The T Experiments: Errors in Scientific senior member of IEEE and a member of SPIE. Con-
Software,” IEEE Computational Science & Eng., vol. 4, tact him at steve.eddins@mathworks.com.
no. 2, 1997, pp. 27–38.
3. G. Chang et al., “Retraction of Pornillos et al., Sci- Selected articles and columns from IEEE Computer
ence 310 (5756) 1950–1953. Retraction of Reyes and Society publications are also available for free at
Chang, Science 308 (5724) 1028–1031. Retraction of http://ComputingNow.computer.org.