How to do assert isequal with a multiple returns function
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
dpb
am 6 Jul. 2021
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.
Akzeptierte Antwort
Steven Lord
am 6 Jul. 2021
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!
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Run Unit Tests finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!