Check the equality of custom Classes using Matlab Unit Test suite

1 回表示 (過去 30 日間)
Jeno Boka
Jeno Boka 2017 年 4 月 26 日
回答済み: Stephen Carter 2017 年 4 月 28 日
I am currently developing a class based Matlab Unit Test project, I am using the following code to verify the equality of two structures
verifyThat(testCase,struct1,IsEqualTo(struct2,'Within', RelativeTolerance(2*eps)))
But unfortunately I get an error message
IsEqualTo failed.
--> Path to failure: <Value>.req_results
--> ObjectComparator failed.
--> The objects are not equal using "isequal".
--> The tolerance was ignored. The tolerance as specified does not support comparisons of Result values.
How to implement a method for testing the equality of two structures, consisting of elements belonging to different classes? Even when some of of them are custom classes.
I want a method compatible with verifyThat
  2 件のコメント
Stephen Carter
Stephen Carter 2017 年 4 月 26 日
編集済み: Stephen Carter 2017 年 4 月 26 日
I see that ObjectComparator is the one that is failing here.
You might be able to leverage the PublicPropertyComparator.supportingAllValues() static method here and use in place of object comparator.
Here is a workaround:
import matlab.unittest.constraints.IsEqualTo;
import matlab.unittest.constraints.PublicPropertyComparator;
% get access to the comparators that IsEqualTo uses by default:
tmp=IsEqualTo([]);
comparators = tmp.Comparator;
% add PublicPropertyComparator to the front (to be picked up before ObjectComparator does)
comparators = [PublicPropertyComparator.supportingAllValues(), comparators];
% perform qualification
verifyThat(testCase,struct1,IsEqualTo(struct2,...
'Using',comparators,'Within', RelativeTolerance(2*eps)))
Note that PublicPropertyComparator will behave much differently than ObjectComparator in that it only supports MATLAB objects and only inspects the public properties.
Let me know if that helps.
Jeno Boka
Jeno Boka 2017 年 4 月 28 日
Thank your for the fast, and exceptionally clear answer! That answer was really helpful! Thanks!

サインインしてコメントする。

採用された回答

Stephen Carter
Stephen Carter 2017 年 4 月 28 日
See answer in comment above.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWrite Unit Tests についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by