Can I access local functions for unit testing
Show older comments
I want to unit test local functions that appear after the main function of a file, whose name is not the file name. After all it's not really unit testing if I just test the final outcome of the program. Is there any way to call them from a test file? Do I really have to make a new file for every little helper function?
Accepted Answer
More Answers (1)
Mauro DiNuzzo
on 24 Jul 2025
0 votes
I completely understand your frustration, as I've often run into the same limitation and found it quite restricting that MATLAB doesn't allow a straightforward way of calling local functions outside their normal context.
After running into this issue again today, I set out to find a practical workaround for this limitation and ended up creating a small utility that might be exactly what you were looking for. It allows you to "export" local functions from a MATLAB file so that they can be tested independently, without having to restructure your codebase or alter the main function's interface. It’s just a lighweight fix and not a perfect solution (the language imposes some constraints), but for simple and practical use cases, it works as expected.
You can find it here: https://www.mathworks.com/matlabcentral/fileexchange/181583-exports
Hopefully this is still useful to you (even though some time has passed), and I hope it proves helpful for others running into the same issue, at least until Mathworks eventually addresses this and craft a built-in solution.
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!