필터 지우기
필터 지우기

How to call a function if unit test fails?

조회 수: 7 (최근 30일)
John Doe
John Doe 2020년 1월 30일
댓글: John Doe 2020년 1월 30일
If this is possible in a script unit test, that would be great. I want to do the following:
%% Unit test
a = 1;
b = 2;
fail = assert(a == b)
if fail
fail_function;
end
fuction fail_function
disp('Test failed');
end
I've also tried finding something to do this using class-based unit testing, but I haven't found anything. Thank you.
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 1월 30일
assert() does not permit output assignment.

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

채택된 답변

Andy Campbell
Andy Campbell 2020년 1월 30일
편집: Andy Campbell 2020년 1월 30일
Hi there,
If you really want to stay in a script based test, the best way to do this would to be to use a try-catch
%% Unit test
a = 1;
b = 2;
try
assert(a == b)
catch ex
fail_function;
rethrow(ex);
end
function fail_function
disp('Test failed');
end
However, I think that you can do something pretty cleanly using function based tests, which might be more approachable for your use case than classes, but has a bit of extra nice-ness. In this case you can just pass the function handle to what you want to do as a diagnostic argument to the verification function:
function tests = my_test
tests = functiontests(localfunctions);
end
function testMyCode(testCase)
%% Unit test
a = 1;
b = 2;
verifyEqual(testCase, a, b, @fail_function)
end
function fail_function
disp('Test failed');
end
Note that you also get access to verifyEqual and many other functions for qualification. In the case of verifyEqual vs assert(a==b), verifyEqual actually provides a lot of testing specific strictness and better diagnostics when things fail. It should handle edge cases for you.
  댓글 수: 1
John Doe
John Doe 2020년 1월 30일
Thanks Andy, both these solutions work well for me and I appreciate being provided with alternatives. I had overlooked a function based test, but it does look like a solid compromise between scripts/classes. Thank you!

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

추가 답변 (1개)

Steven Lord
Steven Lord 2020년 1월 30일
편집: Steven Lord 2020년 1월 30일
If all you want your function to do is display a diagnostic message, just modify your call to assert slightly.
%% Unit test
a = 1;
b = 2;
assert(a == b, 'Test failed')
If you want your function to do something else there may be a way to do what you want. One possibility would be to use the diagnostic input to the qualification functions/methods. But it would be difficult to give a more informative answer without more information about what you want to do.
[Forgot assert can't have an output argument when I copied and pasted the example. Removing the output.]
  댓글 수: 1
John Doe
John Doe 2020년 1월 30일
Thank you, I did want to actually run a function, but I appreciate the response and the information on the assert function!

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

카테고리

Help CenterFile Exchange에서 Testing Frameworks에 대해 자세히 알아보기

태그

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by