How to do assert isequal with a multiple returns function

12 vues (au cours des 30 derniers jours)
Gpilz
Gpilz le 6 Juil 2021
Commenté : dpb le 6 Juil 2021
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 commentaire
dpb
dpb le 6 Juil 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.

Connectez-vous pour commenter.

Réponse acceptée

Steven Lord
Steven Lord le 6 Juil 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!
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.

Plus de réponses (0)

Catégories

En savoir plus sur Run Unit Tests dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by