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.
- MATLAB xUnit: new version available (2010-06-16)
- MTEST - A unit test harness for MATLAB code (2009-02-03)
- Category:
- Picks
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.