Class-Based Unit Tests: Running Specific Methods With Arguments

4 views (last 30 days)
I have a simple class:
MyTest < matlab.unittest.TestCase
I know that I can test specific methods of the class with the following:
runtests({'MyTest/method1', 'MyTest/method2'}) % etc
However, I want to run these specific tests with some kind of input parameter, e.g. an options struct. Ideally, I'd make "options" a property of MyTest (whether it's a static property or a property of an instance of MyTest) and set its value at runtime. I'd also be okay with passing the struct as an input to each of these methods.
How can I accomplish this? I read some stuff about TestParameterDefinition, but it didn't seem to give an example close enough to what I'm trying to do.

Accepted Answer

Dave B
Dave B on 30 Sep 2021
Edited: Dave B on 30 Sep 2021
In your previous question I suggested runtests, but if you look at the other answer @Steven Lord provided a more complex alternative which is to create a matlab.unittest.TestSuite.
Using the TestSuite object let's you add external parameters, via the ExternalParameters Property. There's an example here: https://www.mathworks.com/help/matlab/matlab_prog/use-external-parameters-in-parameterized-test.html
  10 Comments
Walter Roberson
Walter Roberson on 4 Oct 2021
Over the years, we have had multiple people claim that it is "rude" to reply anything other than the code to accomplish exactly the task asked about -- including it being "rude" to ask questions about what the task actually is . Most of the time in such cases, information is missing, but the posters have claimed that it was "rude" for people to have replied with code that accomplished what the volunteers felt was most likely the actual question: that the only acceptable answer was code that perfectly accomplished the task the poster had in mind (and failed to convey.)
So given that there are people for whom anything other than perfect code responses are perceived as "rude", and given that we cannot predict ahead of time who will feel that way, is the conclusion that we should draw that the volunteers should never reply with anything other than exact code, in case the poster might be of the opinion that anything else is rude?
But if so... then what to do about the posters who consider plain code responses that do not have explanation / discussion, to be rude ?
Doesn't it follow that in order to satisfy everyone, that we should shut the site down ?
Randy Price
Randy Price on 4 Oct 2021
I don't think you mean to, but you're attacking a straw man here. I made no such claim.
My claim is that the tone of the response was rude. Replying to a question without answering it isn't inherently rude. However, when the tone of the response already comes off as rude, the content and context of the reply can combine with the tone to make it seem even more rude.
I'll reiterate:
"Don't you use the search engine of the Matlab Documentation as your first alternative?"
The way in which this question is worded is condescending. Suppose I (somehow) didn't know that MATLAB has built-in documentation. Because the author paints it as such an obvious solution that I should already know about, I now feel incompetent for not knowing about it.
The same underlying question can be worded in a friendly/helpful tone. Here's an example:
"I noticed you're new here, and you mentioned that you only tried googling your problem - did you know that MATLAB has built-in documentation with a search engine?"
This "rewording exercise" I've done looks like it's straight out of an HR training exercise. And I guess that's my point - the tone in which you interact with people online matters. I flagged the response because, when I post a question on this site or any similar forum, I expect to be helped by its participants, not be made to feel like an idiot for possibly not knowing something.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!