Unittest: Verify whether the function prints correct error message
显示 更早的评论
Hello all,
I am writing class based unittests, using matlab.unittest.TestCase. In some of my functions, at some conditions, we print error message using the error function. For example:
function ret = myFunc(input1, input2)
if length(unique(input1)) ~= 2 & S_ntream == 2
error("Your input doesn't satisfy minimum unique input requirement")
end
%Other stuff the function will do if it meets the above condition goes here.
end
Now, in my unittests I want to verify that my program is displaying/returning this error message, when it should. I tried using the verifyError method like following. However, the test failed. (I have designed the emv.input1, emv.input2 such that the function will reach the error branch.)
classdef myFunc_Test < matlab.unittest.TestCase
properties(TestParameter)
emv = generateAllInputsForErrorMessageVerification();
end
methods(Test, ParameterCombination = 'sequential')
function testErrorMessages(testCase, emv)
%I have designed the emv.input1, emv.input2 such that the function
%will reach the error branch.
testCase.verifyError(@()myFunc(emv.input1, emv.input2), ... ,
"Your input doesn't satisfy minimum unique input requirement");
end
end
end
Also, using verifyEqual doesn't make sense as myFunc is not "returning" the error message ("Your input....").
Any suggestions on how this can be tested will be appreciated. Thanks in anticipation.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Write Unit Tests 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!