Main Content

カスタムのテスト出力形式を生成するプラグイン

この例では、カスタム形式を使用してテストの最終結果を出力ストリームに書き込むプラグインの作成方法を説明します。

プラグインの作成

作業フォルダーのファイルで、matlab.unittest.plugins.TestRunnerPlugin クラスから継承されたクラス ExampleCustomPlugin を作成します。プラグインのクラスで、次を行います。

  • OutputStream インスタンスを格納するプラグインに Stream プロパティを定義する。既定では、プラグインは標準出力に書き込みます。

  • TestRunnerPlugin の既定の runTestSuite メソッドをオーバーライドして、テスト ランナーが新しいテスト セッションを実行していることを示すテキストを出力する。この情報は、これによってテストの実行を区別できることから、書き込むログ ファイルが 1 つの場合に特に有用です。

  • TestRunnerPlugin の既定の reportFinalizedResult メソッドをオーバーライドして、テストの最終結果を出力ストリームに書き込む。print メソッドを変更して、テスト ログまたは継続的インテグレーション システムに適切な形式でテスト結果を出力できます。

classdef ExampleCustomPlugin < matlab.unittest.plugins.TestRunnerPlugin
    properties (Access=private)
        Stream
    end
    
    methods
        function p = ExampleCustomPlugin(stream)
            if ~nargin
                stream = matlab.automation.streams.ToStandardOutput;
            end
            validateattributes(stream, ...
                {'matlab.automation.streams.OutputStream'},{})
            p.Stream = stream;
        end
    end
    
    methods (Access=protected)
        function runTestSuite(plugin,pluginData)
            plugin.Stream.print('\n--- NEW TEST SESSION at %s ---\n',...
                char(datetime))
            runTestSuite@...
                matlab.unittest.plugins.TestRunnerPlugin(plugin,pluginData);
        end
        
        function reportFinalizedResult(plugin,pluginData)
            thisResult = pluginData.TestResult;
            if thisResult.Passed
                status = 'PASSED';
            elseif thisResult.Failed
                status = 'FAILED';
            elseif thisResult.Incomplete
                status = 'SKIPPED';
            end
            plugin.Stream.print(...
                '### YPS Company - Test %s ### - %s in %f seconds.\n',...
                status,thisResult.Name,thisResult.Duration)
            
            reportFinalizedResult@...
                matlab.unittest.plugins.TestRunnerPlugin(plugin,pluginData)
        end
    end
end

テスト クラスの作成

作業フォルダーに、以下のテスト クラスを含むファイル ExampleTest.m を作成します。このテスト クラスでは、2 つのテストが合格し、その他は検証または仮定エラーとなります。

classdef ExampleTest < matlab.unittest.TestCase
    methods(Test)
        function testOne(testCase)
            testCase.assertGreaterThan(5,1)
        end
        function testTwo(testCase)
            wrongAnswer = 'wrong';
            testCase.verifyEmpty(wrongAnswer,'Not Empty')
            testCase.verifyClass(wrongAnswer,'double','Not double')
        end
        function testThree(testCase)
            testCase.assumeEqual(7*2,13,'Values not equal')
        end
        function testFour(testCase)
            testCase.verifyEqual(3+2,5)
        end
    end
end

テスト ランナーへのプラグインの追加およびテストの実行

コマンド プロンプトで ExampleTest クラスからテスト スイートを作成し、テスト ランナーを作成します。

import matlab.unittest.TestSuite
import matlab.unittest.TestRunner

suite = TestSuite.fromClass(?ExampleTest);
runner = TestRunner.withNoPlugins;

ExampleCustomPlugin のインスタンスを作成し、テスト ランナーに追加します。テストを実行します。

import matlab.automation.streams.ToFile
fname = 'YPS_test_results.txt';
p = ExampleCustomPlugin(ToFile(fname));

runner.addPlugin(p)
result = runner.run(suite);

出力ファイルの内容を表示します。

type(fname)
--- NEW TEST SESSION at 15-Oct-2022 20:30:15 ---
### YPS Company - Test PASSED ### - ExampleTest/testOne in 0.014881 seconds.
### YPS Company - Test FAILED ### - ExampleTest/testTwo in 0.099099 seconds.
### YPS Company - Test SKIPPED ### - ExampleTest/testThree in 0.073080 seconds.
### YPS Company - Test PASSED ### - ExampleTest/testFour in 0.006351 seconds.

同じテスト ランナーを使用して Incomplete テストを再実行します。出力ファイルの内容を表示します。

suiteFiltered = suite([result.Incomplete]);
result2 = runner.run(suiteFiltered);

type(fname)
--- NEW TEST SESSION at 15-Oct-2022 20:30:15 ---
### YPS Company - Test PASSED ### - ExampleTest/testOne in 0.014881 seconds.
### YPS Company - Test FAILED ### - ExampleTest/testTwo in 0.099099 seconds.
### YPS Company - Test SKIPPED ### - ExampleTest/testThree in 0.073080 seconds.
### YPS Company - Test PASSED ### - ExampleTest/testFour in 0.006351 seconds.

--- NEW TEST SESSION at 15-Oct-2022 20:31:00 ---
### YPS Company - Test SKIPPED ### - ExampleTest/testThree in 0.018080 seconds.

参考

| | |

関連するトピック