- Provide a set with specific input data and compare the results with known outputs
- Check if the output is very sensitive to a tiny variation of the inputs. If so, the function is an expensive random number generator.
- Call the function with irregulare values and check, if it throws error messages
- Check for NaN, +/-Inf, empty input, wrong file names, cell arrays instead of matrices etc.
How can I test a long function in Matlab?
2 views (last 30 days)
Show older comments
Gabriela Garaizabal Etxeberria
on 8 Apr 2021
Commented: Jan
on 13 Apr 2021
Hello community,
I have to test certain functions and some of them are very long, how can I generate a test that verifies it?
Thank you so much,
Gabriela
0 Comments
Accepted Answer
Jan
on 8 Apr 2021
You test them the same way as you test short functions:
Because it might be really hard to obtain some valid output data without using the original function, a validation of code usually requires to split the code to smaller functions, which can be tested independently. If the code is e.g. an integrator, you could compare the results with other integrators.
The base of unit-testing is to split the functions into units, whose output can be checked with controllable methods.
3 Comments
More Answers (1)
John D'Errico
on 12 Apr 2021
Don't test your long function. Because you should be writing SHORT functions. Then you can test each piece, verify it works as designed. And only then put it all together.
0 Comments
See Also
Categories
Find more on Generate Tests 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!