创建一个供交互测试的测试用例。
import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.RelativeTolerance
import matlab.unittest.constraints.PublicPropertyComparator
testCase = TestCase.forInteractiveUse;
定义实际和预期 timeseries 对象。将一个实际数据点扰动 1%。
expected = timeseries(1:10);
actual = expected;
actual.Data(7) = 1.01*actual.Data(7);
测试是否实际值和预期值在 2% 的相对误差界限内相等。
testCase.verifyThat(actual, IsEqualTo(expected,...
'Within', RelativeTolerance(.02)))
Verification failed.
---------------------
Framework Diagnostic:
---------------------
IsEqualTo failed.
--> ObjectComparator failed.
--> The objects are not equal using "isequal".
--> The tolerance was ignored. The tolerance as specified does not support comparisons of timeseries values.
Actual timeseries:
timeseries
Common Properties:
Name: 'unnamed'
Time: [10x1 double]
TimeInfo: [1x1 tsdata.timemetadata]
Data: [1x1x10 double]
DataInfo: [1x1 tsdata.datametadata]
More properties, Methods
Expected timeseries:
timeseries
Common Properties:
Name: 'unnamed'
Time: [10x1 double]
TimeInfo: [1x1 tsdata.timemetadata]
Data: [1x1x10 double]
DataInfo: [1x1 tsdata.datametadata]
More properties, Methods
在构造约束时使用 PublicPropertyComparator。
testCase.verifyThat(actual, IsEqualTo(expected,...
'Within', RelativeTolerance(.02),...
'Using', PublicPropertyComparator.supportingAllValues))
Interactive verification passed.
测试通过,原因是 PublicPropertyComparator 分别比较每个公共属性而不是一次性比较该对象。在前一测试中,ObjectComparator 比较 timeseries 对象,因此依赖于 timeseries 类的 isequal 方法。由于实际 timeseries 中的扰动,isequal 返回 false。比较器不应用该容差,原因是双精度值容差不能直接应用于 timeseries 对象。在后一测试中,比较器将容差应用于包含双精度值数据的每个公共属性。