How do I teardown fixture while using a TestCase obj in interactive mode?

3 次查看(过去 30 天)
I found matlab.unittest.TestCase.forInteractiveUse extremely useful for development and debugging of test cases written for matlab.unittest.TestCase.
But if I use applyFixture method of a test method of such a TestCase obj that is in interattive mode by forInteractiveUse, how do I teardown the fixture? Simply deleting the TestCase won't does it, will it?
For example, say I have a TestCase class like this:
classdef testcase1 < matlab.unittest.TestCase
methods (Test)
function testmethod1(testCase)
testCase.applyFixture(thisfixture);
% blah, blah, blah
end
end
end
Then, for development, I excute the following command:
testCase = testcase1; testCase.forInteractiveUse;
Then I can evaluate anything within the TestCase just like a script. However, in case a test method uses matlab.unittest.fixtures.Fixture for setup, I would like to do this:
testCase.applyFixture(fixtureforthis);
Then I can comfortablly evaluate/debug the rest of code in the test method. My question is how can I get out of this state. How can I invoke teardown method of the fixture thisfixture while in interactive mode?

采纳的回答

Kouichi Nakamura
Kouichi Nakamura 2015-4-22
Actually, deletion of the testcase1 object testCase does invoke the teadown method. Thus, combinatory use of forInteractiveUse and appyFixture is very efficient.

更多回答(2 个)

Andy Campbell
Andy Campbell 2015-4-22
Hello Kouichi,
Yes applyFixture ties the fixture teardown step to the lifecycle of the testCase upon which it is applied, so once the testCase is deleted the fixture will indeed get torn down as you have seen.
However, this line confuses me:
testCase = testcase1; testCase.forInteractiveUse;
What that is doing is creating a new instance of the testcase1 class which will not inform you of failures as you play with it on the command line. For example, testCase.verifyEqual(1,0) will not show you any diagnostics that desrcibe that 1 is not equal to 0.
Then, testCase.forInteractiveUse calls a static method (not using the testCase instance you created, it is equivalent to matlab.unittest.TEstCase.forInteractiveUse) that creates a new testCase instance which isn't even of type testcase1, but that has the correct configuration applied to show you diagnostics at the command line. It seems perhaps you are looking for something that neither the first line nor the second is really giving you. Can you explain more about what you need?
Thanks, Andy

Kouichi Nakamura
Kouichi Nakamura 2015-4-23
Thanks. For the question I raised, now I'm fine with deletion of testCase object. At least it does what I expected. But what you pointed out interested me, because I was not happy with how forInteractiveUse actually works.
It is understandable that in the following case, testCase has nothing to do with any of my custom class. It is a call of a static method.
import matlab.unittest.TestCase;
testCase = TestCase.forInteractiveUse;
However, I found that the following does not allow me to access properties of MyClass class, while I can use methods like testCase.verifyEqual(). Reading your post, it seems that this command still invokes static method of the parent class matlab.unittest.TestCase.
testCase = MyClass.forInteractiveUse
testCase =
TestCase with no properties.
I wanted to be able to access to properties of MyClass (if I can't, I can't really evaluate my test code at all) as well as to call methods like verifyEqual. I tried random shots and the following seems to work for me.
testCase = MyClass;
testCase.forInteractiveUse;
I noticed when I run the above commands, the testCase object of MyClass with properties and a ans object that is another instance of matlab.unittest.TestCase with no property.
Interestingly, even after I delete the ans object of matlab.unittest.TestCase class, I can still execute the following command. How is it possible to use testCase in Interactive mode in this situation? It's weird.
>> clear ans
>> testCase.verifyEqual(1,1)
Interactive verification passed.
>> testCase.applyFixture(myfixture);
Setup of myfixture works. It generates ans object of 1x1 myfixture class. Deletion of this ans does not invoke teardown, while deletion of testCase does. I'm using R2014a.
  3 个评论
Kouichi Nakamura
Kouichi Nakamura 2015-5-13
Sorry, I didn't realize you had replied to my comment. Indeed, it seems that I completely mistook the point of using forInteractiveUse! Just creating an instance of myClass ( <matlab.unittest.TestCase ) sufficed. I can use methods including verifyEqual and applyFixture and access to properties as well. Thanks for correcting my confusion.
I'm looking forward to future release, but rather hesitant to go to R2014b onwards due to the radical change in graphic object handles...
Andy Campbell
Andy Campbell 2015-10-19
编辑:Andy Campbell 2015-10-19
Note, as of R2015b you can get interactive testCase instances for your own subclasses by passing the desired meta.class as an input argument to the forInteractiveUse static method:
tc = matlab.unittest.TestCase.forInteractiveUse(?MyClass)
This allows you to have your interactive listeners placed on your own sublcass of TestCase so that you can also call any helper methods or properties of your TestCase class.
Enjoy! Andy

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Write Unit Tests 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by