# MATLAB xUnit Test Framework

NOTE! As of R2013a, MATLAB has a fully supported built-in unit testing framework. If you are just getting started, please investigate it first!

help round2
 ROUND2 rounds number to nearest multiple of arbitrary precision.
Z = ROUND2(X,Y) rounds X to nearest multiple of Y.

Example 1: round PI to 2 decimal places
>> round2(pi,0.01)
ans =
3.14

Example 2: round PI to 4 decimal places
>> round2(pi,1e-4)
ans =
3.1416

Example 3: round PI to 8-bit fraction
>> round2(pi,2^-8)
ans =
3.1406

Examples 4-6: round PI to other multiples
>> round2(pi,0.05)
ans =
3.15
>> round2(pi,2)
ans =
4
>> round2(pi,5)
ans =
5


Examples usually make excellent test cases. Turning them into test points was a simple matter of codifying function calls to return actual values, and providing expected values for comparison.
dbtype test_round2
1     function test_suite = test_round2
2     initTestSuite
3
4     function testExample1
5     assertEqual(round2(pi,0.01),3.14)
6
7     function testExample2
8     assertEqual(round2(pi,1e-4),3.1416)
9
10    function testExample3
11    assertElementsAlmostEqual(round2(pi,2^-8),3.1406,'absolute',0.0001)
12
13    function testExample4
14    assertElementsAlmostEqual(round2(pi,0.05),3.15,'absolute',0.01)
15
16    function testExample5
17    assertEqual(round2(pi,2),4)
18
19    function testExample6
20    assertEqual(round2(pi,5),5)


Note that I also specified tolerances where appropriate. Now, running this test suite is straightforward.
test_round2
Starting test run with 6 test cases.
......
PASSED in 0.010 seconds.

Be sure to check out his blog, Steve on Image Processing in general, and two posts in particular. You can also read his IEEE Computing in Science and Engineering journal article, Automated Software Testing for MATLAB for more background. That touches on many facets of testing such as choosing good test cases to ensure code coverage. As one of the Quality Engineers that Steve mentioned, I found xunit both powerful and easy to use. Since learning some things about Agile and Test-Driven development processes I see xunit being useful from small projects to large. From first learning MATLAB as an EE student in 1985 until I left the world of applied R&D to join The MathWorks in 2001, I wrote a lot of MATLAB code, and I used what I knew: quick and dirty development. As an application engineer, like Brett and Jiro, I met customers in many industries, and saw a wide user spectrum from novice nonprogrammers to seasoned CS experts. Having moved along that spectrum myself, I really appreciate how much the bar has been lowered. With MATLAB and xunit automated testing is not just for specialists. Thanks, Steve! In this day and age software can change rapidly. This is the new normal. Enhancements may be demanded. Some bug fixes can break prior workarounds. Programs can be really complex and rely on code from many different programmers. Maybe a colleague changes a network shared file. Programs that worked once upon a time can break someday. These are modern facts of life. Automated testing provides regression insurance in a world of change. How prepared are you?

Published with MATLAB® 7.10

|