Class-Based Unit Tests: Running Specific Methods With Arguments
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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.
採用された回答
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 件のコメント
Thanks - sometimes googling won't lead me to the right pages.
I now understand how to add external parameters (following that example) via making a TestStuite object, and how to run only specific tests from a test class using runtests, or by setting the "Tag" in a Test method.
Now, I need to combine these two. The "Tag" approach seems fine for static testing, but I need to dynamically choose which tests to run, meaning I need to either dynamically modify "Tag", or do something else entirely after adding my external "options" parameter.
Here's the context, if it helps: I'm making a GUI that allows the user to select families (classes) of tests to run, which tests (methods) to run for each family, and what options (parameter) to send to the test methods for each test family. So my inputs are test classes, methods for those test classes, and an options struct for each test class, and the output is running the specified methods for the specified family, using an options struct for each family.
I think you can do this all with TestSuite, just use selectIf with ProcedureName and an array of procedures. To expand on Steven's (spelling correct this time!) example, and put it all together:
classdef footest < matlab.unittest.TestCase
properties (TestParameter)
Data = struct('builtin',0);
end
methods(Test)
function test_onePlus(testcase,Data)
testcase.verifyEqual(1+Data, sum([1 Data]));
disp(Data)
end
function test_twoPlus(testcase,Data)
testcase.verifyEqual(2+Data, sum([2 Data]));
end
function test_threePlus(testcase,Data)
testcase.verifyEqual(3+Data, sum([3 Data]));
end
end
end
import matlab.unittest.parameters.Parameter
newData = {44};
param = Parameter.fromData('Data',newData);
s = matlab.unittest.TestSuite.fromClass(?footest,'ExternalParameters',param);
s_sub = selectIf(s,'ProcedureName', {'test_onePlus' 'test_twoPlus'});
s_sub.run
I see - this works! Thanks again.
"sometimes googling won't lead me to the right pages"
Don't you use the search engine of the Matlab Documentation as your first alternative?
Rik
2021 年 10 月 2 日
@Randy Price regarding your flag: what is unconstructive/rude about this comment? (without having read the rest of the thred) I would say it is actually valuable advice.
- The comment has nothing to do with the question itself.
- An answer had already been accepted. If the comment proposed an alternative solution, that would’ve been fine.
- The comment assumes that I have neither tried nor thought of searching for the solution to this specific problem and to other problems in MATLAB’s official documentation - which is very obviously the first place to look - and is inherently condescending in doing so, especially due to its wording (“Don’t you… ?” vs. “Hey, you only said you tried googling - did you know that you can hit F1 to open the Documenation browser?”). The purpose of my “googling” comment was to thank the commenter for providing me with the solution to a relatively simple problem, and to express that I had tried to find the solution on my own, but simply did not know enough about the topic at hand to type the right words into a search bar (whether through a web-based search engine or through MATLAB’s search engine) and click on the right link. I could read hundreds of pages of documentation and try to figure out what’s relevant, or I could ask someone to point me in the right direction. That’s what forums are for.
It is common for volunteers to contribute suggestions on how people can improve their MATLAB skills or get more out of available resources. Volunteers are absolutely not restricted to just answer the immediate question at hand. The purpose of the facility is to help people, and often the best way to help people is discuss matters that are not exactly what the person had been asking about.
The tone of the comment comes off as rude. I explained why it comes off that way, and provided an example of how the same "advice" could be expressed without condescension.
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 ?
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.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Write C Functions Callable from MATLAB (MEX Files) についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
