addPlugin
クラス: matlab.unittest.TestRunner
名前空間: matlab.unittest
テスト ランナーにプラグインを追加
入力引数
テスト ランナー。matlab.unittest.TestRunner オブジェクトとして指定します。
プラグイン。matlab.unittest.plugins.TestRunnerPlugin オブジェクトとして指定します。
例
テスト ランナーに XMLPlugin インスタンスを追加して、JUnit スタイルのテスト結果を生成します。
現在のフォルダー内の eyeTest.m という名前のファイルに、関数 eye をテストする関数ベースのテストを作成します。
function tests = eyeTest tests = functiontests(localfunctions); end function doubleClassTest(testCase) actual = eye; verifyClass(testCase,actual,"double") end function singleClassTest(testCase) actual = eye("single"); verifyClass(testCase,actual,"single") end function uint16ClassTest(testCase) actual = eye("uint16"); verifyClass(testCase,actual,"uint16") end function sizeTest(testCase) expected = [7 13]; actual = eye(expected); verifySize(testCase,actual,expected) end function valueTest(testCase) actual = eye(42); verifyEqual(testCase,unique(diag(actual)),1) % Diagonal values must be 1 verifyEqual(testCase,unique(triu(actual,1)),0) % Upper triangular values must be 0 verifyEqual(testCase,unique(tril(actual,-1)),0) % Lower triangular values must be 0 end
テストを実行するために、まずこの例で使用するクラスをインポートします。
import matlab.unittest.TestRunner import matlab.unittest.plugins.XMLPlugin
eyeTest.m のテストからテスト スイートを作成します。
suite = testsuite("eyeTest.m");プラグインなしでテスト ランナーを作成します。次のコードで、出力を生成しないサイレント ランナーを作成します。
runner = matlab.unittest.TestRunner.withNoPlugins;
次に、任意のプラグインを選択して追加できます。現在のフォルダー内のファイル myTestResults.xml に JUnit スタイルの XML 出力を書き込むプラグインを作成します。その後、テスト ランナーにプラグインを追加します。
xmlFile = "myTestResults.xml";
p = XMLPlugin.producingJUnitFormat(xmlFile);
addPlugin(runner,p)テストを実行します。この例では、すべてのテストがパスします。
results = run(runner,suite);
生成されたアーティファクトの内容を表示します。
disp(fileread(xmlFile))
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<testsuites>
<testsuite errors="0" failures="0" name="eyeTest" skipped="0" tests="5" time="0.25082">
<testcase classname="eyeTest" name="doubleClassTest" time="0.015066"/>
<testcase classname="eyeTest" name="singleClassTest" time="0.0042728"/>
<testcase classname="eyeTest" name="uint16ClassTest" time="0.0046594"/>
<testcase classname="eyeTest" name="sizeTest" time="0.013599"/>
<testcase classname="eyeTest" name="valueTest" time="0.21322"/>
</testsuite>
</testsuites>実行しているテストの名前を出力ストリームに送るプラグインを使用してテストを実行します。生成されるテキストを画面に送るように ToStandardOutput インスタンスをプラグインに渡します。
カスタム プラグインの作成
現在のフォルダー内の ExamplePlugin.m という名前のファイルに、TestRunnerPlugin の runTest メソッドをオーバーライドする ExamplePlugin クラスを作成します。このプラグインで、実行している各テストの名前をプラグインの作成時に指定した出力ストリームに送ります。
classdef ExamplePlugin < matlab.unittest.plugins.TestRunnerPlugin properties (SetAccess=immutable) Output end methods function plugin = ExamplePlugin(stream) arguments stream (1,1) matlab.automation.streams.OutputStream end plugin.Output = stream; end end methods (Access=protected) function runTest(plugin,pluginData) print(plugin.Output,"### Running test: %s\n",pluginData.Name) % Invoke the superclass method runTest@matlab.unittest.plugins.TestRunnerPlugin( ... plugin,pluginData) end end end
サンプル テスト クラスの作成
現在のフォルダー内の ZerosTest.m という名前のファイルに、関数 zeros をテストする ZerosTest クラスを作成します。
classdef ZerosTest < matlab.unittest.TestCase properties (TestParameter) type = {'single','double','uint16'}; size = struct("s2d",[3 3],"s3d",[2 5 4]); end methods (Test) function testClass(testCase,size,type) testCase.verifyClass(zeros(size,type),type) end function testSize(testCase,size) testCase.verifySize(zeros(size),size) end function testDefaultClass(testCase) testCase.verifyClass(zeros,"double") end function testDefaultSize(testCase) testCase.verifySize(zeros,[1 1]) end function testDefaultValue(testCase) testCase.verifyEqual(zeros,0) end end end
テスト ランナーへのプラグインの追加とテストの実行
テストを実行するために、まずこの例で使用するクラスをインポートします。
import matlab.unittest.TestRunner import matlab.automation.streams.ToStandardOutput
ZerosTest クラスからテスト スイートを作成します。
suite = testsuite("ZerosTest");プラグインなしでテスト ランナーを作成します。次のコードで、出力を生成しないサイレント ランナーを作成します。
runner = testrunner("minimal");次に、任意のプラグインを選択して追加できます。テキスト出力を画面に送る ExamplePlugin インスタンスを作成します。
plugin = ExamplePlugin(ToStandardOutput);
テスト ランナーにプラグインを追加し、テストを実行します。テストを実行すると、テストの名前が画面に表示されます。
runner.addPlugin(plugin) results = runner.run(suite);
### Running test: ZerosTest/testClass(size=s2d,type=single) ### Running test: ZerosTest/testClass(size=s2d,type=double) ### Running test: ZerosTest/testClass(size=s2d,type=uint16) ### Running test: ZerosTest/testClass(size=s3d,type=single) ### Running test: ZerosTest/testClass(size=s3d,type=double) ### Running test: ZerosTest/testClass(size=s3d,type=uint16) ### Running test: ZerosTest/testSize(size=s2d) ### Running test: ZerosTest/testSize(size=s3d) ### Running test: ZerosTest/testDefaultClass ### Running test: ZerosTest/testDefaultSize ### Running test: ZerosTest/testDefaultValue
バージョン履歴
R2013a で導入
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
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)