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!