Can I access local functions for unit testing
显示 更早的评论
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?
采纳的回答
更多回答(1 个)
Mauro DiNuzzo
2025-7-24
0 个投票
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Get Started with MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!