Unittest: Verify whether the function prints correct error message
Mostra commenti meno recenti
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.
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Write Unit Tests in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!