Using Load in a test methods makes the test pass

The following test Passed! Why is that?
classdef minExample < matlab.unittest.TestCase
properties
end
methods(Test)
function test1(testCase)
% comment
save matlab
load matlab
verifyEqual(testCase,1,2);
end
end
end

댓글 수: 1

Run it using:
test = minExample
test.run
the outcome I get is:
ans =
TestResult with properties:
Name: 'minExample/test1'
Passed: 1
Failed: 0
Incomplete: 0
Duration: 0.1692
Totals:
1 Passed, 0 Failed, 0 Incomplete.
0.1692 seconds testing time.

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

답변 (2개)

Andy Campbell
Andy Campbell 2019년 10월 21일

1 개 추천

Hi Erik,
The problem here is indeed the fact that the testCase is getting overwritten. Hence why if you assign the output of the load command to a structure the issue is not encountered. These steps are similar to something like the following:
classdef SomethingNotATestCase
methods
function verifyEqual(obj,varargin)
% do something else
end
end
end
Then inside the test, doing something like this:
classdef minExample < matlab.unittest.TestCase
methods(Test)
function test1(testCase)
testCase = SomethingNotATestCase
verifyEqual(testCase,1,2);
end
end
end
In both of these cases you have overwritten the testCase with something that is not the right thing to produce a test failure. This is perfectly allowable by the language. This is part of the reason why it is suggested to use the functional form of the load command, including with the output argument, because when you dont use the output argument you "poof" variables into the workspace, and this can overwrite existing variables. If you assign it to an output argument then you explicitly name your variable, and are much less likely to overwrite another variable unknowingly.
Hope that helps!
Andy
per isakson
per isakson 2019년 10월 18일

0 개 추천

Looks like a bug to me, however in programs one ought to use the functional form
S = load('matlab.mat');
In that case the test fails as expected.

댓글 수: 3

That is true, though, I don't want the variable of my 'old' workspace (saved in matlab.mat) to be loaded as a structure 'S'. So I changed the line you suggested to:
load('matlab.mat')\
which again makes this test Pass.
Problem is probably that 'testCase' is destroyed/overwritten when saving and loading it. Thought, if this is the reason, I expect any failure or error other than letting a test pass which clearly should not.
Erik
per isakson
per isakson 2019년 10월 18일
편집: per isakson 2019년 10월 18일
My memory fails me. However, when the current Matlab oop-stuff was introduced 10+ years ago there was a great deal of discussion about saving and loading objects.
Maybe this is of interest to you General-use object copy
Did you contact the Mathworks' tech-support?

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

제품

릴리스

R2013b

질문:

2019년 10월 15일

답변:

2019년 10월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by