How to do assert isequal with a multiple returns function

조회 수: 18 (최근 30일)
Gpilz
Gpilz 2021년 7월 6일
댓글: dpb 2021년 7월 6일
Hi,
I would like to know if anybody knows how to do to assert isequal with a function that has many returns?
example:
assert(isequal(function_name([table_a], [table_b], [table_c]), [return_a], [return_b], [return_c]))
Thanks!
  댓글 수: 1
dpb
dpb 2021년 7월 6일
Only way I would see would be to save the outputs to a local variable first; MATLAB can't return multiple outputs inside a function call.

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

채택된 답변

Steven Lord
Steven Lord 2021년 7월 6일
I agree with dpb. I would also break this into one assert per output argument. While you could wrap the outputs in a cell array (in case they are not compatible sizes or types to be concatenated into one array) with the one assert per output you could tailor the message for each assert call to indicate which output argument does not match your expected value.
[out1, out2, out3] = deal(1+1, 2^3, 25/5);
assert(isequal(out1, 2), 'Expected 1+1 to return 2')
assert(isequal(out2, 9), 'Expected 2^3 to return 9') % Whoops!
Expected 2^3 to return 9
assert(isequal(out3, -999), 'Expected 25/5 to return -999')
So there's a problem with the second output, either the expected value should have been 8 or the calculations performed should have been 3^2 instead of 2^3.
If you're using this to create script-based unit tests making each test case its own assert means that the test runner can report which test cases failed, running each in turn even if one of the earlier test cases failed and threw an error. With my example above, the last assert call never ran. But with runtests both the second and third cases could be reported as test failures.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Run Unit Tests에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by