How can I test a long function in Matlab?

조회 수: 4 (최근 30일)
Gabriela Garaizabal Etxeberria
Gabriela Garaizabal Etxeberria 2021년 4월 8일
댓글: Jan 2021년 4월 13일
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

채택된 답변

Jan
Jan 2021년 4월 8일
You test them the same way as you test short functions:
  • 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.
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
Gabriela Garaizabal Etxeberria
Gabriela Garaizabal Etxeberria 2021년 4월 12일
And, another doubt:
I have a function that collects some parameters of an interface and whose result is in an Excel. How can I test that Excel is generated correctly?
Hear is the code that I have started:
classdef Test1 < matlab.unittest.TestCase
properties
end
methods (Test)
function graphicsTest(testCase)
actSolution=graphics(2,0,700,320);
% expSolution=?
% testCase.verifyEqual(?)
end
end
end
Jan
Jan 2021년 4월 13일
I'm not sure, what you want to test here.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

John D'Errico
John D'Errico 2021년 4월 12일
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.

카테고리

Help CenterFile Exchange에서 MATLAB Report Generator에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by