Getting Started with the MATLAB Unit Test Framework
Learn how to use the MATLAB® Unit Test Framework to write and run tests and speed up your development and testing workflows using the interactive Test Browser.
For further learning on the Test Browser, check out the video "What is the Test Browser in MATLAB?"
Published: 9 Aug 2023
So you've written some code that you want to share with someone else. Great. But are you sure that code still works? Hello, and welcome. Today we're talking about the MATLAB unit test framework, an xUnit-style test framework for MATLAB, which has various tools you can use to run tests on code that you've written.
Testing ensures that your code actually works. Let's review the possibilities for testing MATLAB code. One simple way that everyone tests their code is by interactively calling it from the command window or executing it using the Run button. This is great for a quick spot check. But this can become very expensive and impractical as your code grows.
An easy way to automate some of your code testing is to create a script with all of your commands you've used to interactively test your code. While this seems great at first, you'll find that it doesn't scale very well as your code gets longer. As soon as one command fails, none of the other commands are allowed to run. You don't have any way to know how many of your commands ran successfully or how many additional commands would have failed if the script was allowed to continue. It's also difficult to measure just how much of your actual code your commands exercised. This is where the MATLAB unit test framework comes in.
To use the MATLAB unit test framework, go to the Home tab of the tool strip, and click New Test Class. Then create the test that you want to run on the system you are testing as methods of this new class, which we'll call Test1.
Let's create a single test to make sure that our function quadraticSolver.m here returns the correct answer for a case where the solution is real. In Test1, under the method Test, let's change the function to be testRealSolution, which we will use to test the function's accuracy.
For example, if you input the coefficients 1, negative 3, and 2, you expect the solution to be x equals 2 and 1. Once you have the actual output of the function under Test, you need to compare it to the expected output. In this case, you can use the verifyEqual qualification method to test if the actual output equals the expected output. If you want to allow some tolerance, the method has an optional input argument that allows you to specify that as well.
Using the Insert Methods button, we can also add another test of the output for an imaginary solution to the quadratic equation. Now it's time to run these tests on our quadraticSolver function. They can be run either from the Run Test button in the Editor tab or from the Test Browser panel. By using the Run Test button, we open the Test Browser.
For further learning on the Test Browser and how you can use it to streamline all your testing needs, check out the video "What is the Test Browser in MATLAB" linked in the description.
Now that we've run our tests, you can see that both the tests passed with no failures or incomplete tasks. Now that we have a grasp of how to create and run a test, let's put our knowledge to the test by introducing a bug into the code and running our test suite again. You can see that testRealSolution has passed. But due to the bug, testImaginarySolution has failed.
After fixing the code, you can then filter the test in the Test Browser to only rerun those failed tests to check to see if they now pass. This way, you don't have to rerun the entire suite, which is very useful if the test suite is large. By looking at the methods of matlab.unittest, you can see a full list of possible qualification methods you can use to test your algorithms in MATLAB.
These can be broken into four different types. The first and most widely used type are verify methods. Using these, if a qualification fails, the test continues to execution. This is also known as a soft failure. For assert methods, if the qualification fails, it halts the current test and continues to the next test. The test is marked as being both failed and incomplete.
For fatal methods, if the qualification fails, it halts the entire test suite. And lastly, using assume methods, you can avoid running tests in some scenarios where it doesn't make sense to do so. For example, it doesn't make sense to run a test that requires a special GPU on a machine without that special GPU. Whenever the assume method fails its check, it will mark the test as filtered instead of marking it as failed.
All of these qualification methods give you the capability to create powerful, robust, and flexible suites of tests. In addition to all these types of qualification, the unit test framework has lots of other capabilities, like test fixtures, tagging one individual test, test suites, and more. Plus, while class-based testing provides you with all the capabilities you need, for ease of use, we also offer function-based and script-based testing.
In this video, we have covered many of the ways you can create and run tests on your code in MATLAB. Now that we have a deeper understanding of the testing tools available in MATLAB, you can be more confident in the code you share after thoroughly testing it using the MATLAB unit test framework. You can try this framework now, or check out other MathWorks testing frameworks, including the mocking framework, app testing framework, and performance testing framework, or other testing capabilities like MATLAB Test.