File Exchange Pick of the Week

Our best user submissions

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!


Bob's pick this week is MATLAB xUnit Test Framework by Steve Eddins and now maintained by Paul Sexton.   Do you use MATLAB to develop algorithms? Does your code rely on functions created by others? What kind of testing do you do? For many users the answer may be something like this. "I tried a problem where I knew the solution. Since the right answer was returned, I trust it." For some, that may be good enough. For others, a more systematic and comprehensive approach may be required. I know customers in some industries need to follow a strict process, and automated test suites allow a battery of test cases to be exercised. So for example when any changes need to be made, the tests can be repeated to ensure no inadvertent bugs were introduced in your code while adding a new feature. Now think beyond your own code. Maybe you just received a new version of your favorite third party toolbox. Your boss wants you to check it out before deploying across the organization. Maybe your software compliance or certification procedures dictate that new releases of all computing platforms, including MATLAB, must be qualified before you can upgrade. This is where automated testing pays dividends. Steve's test harness delivers! The detailed documentation is well organized from getting started to advanced usage. To illustrate, I used xunit to test one of my submissions, round2. The function help includes some example problems.
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 
 
  See also ROUND.

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

|
  • print

댓글

댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.