如何从脚本获取测试用例结果?

时间:2019-08-08 07:38:02

标签: matlab unit-testing automated-tests

我使用matlab脚本在测试管理器中创建测试文件(包括测试套件和测试用例)。完成测试后,我需要使用测试结果。如果测试用例全部通过,则退出代码为0;如果其中一个测试用例失败,则退出代码为1。我想在我的脚本中实现它。

我的matlab版本是2016b。 下面是我的脚本:

try
    %some code to create my test cases in test manager.I didn't post here.

    ro = run(ts); %run the test suite
    saveToFile(tf); %save the test file

    % Get the results set object from Test Manager
    result = sltest.testmanager.getResultSets;
    % Export the results set object to a file
    sltest.testmanager.exportResults(result,'C:\result.mldatx');

    % Clear results from Test Manager
    sltest.testmanager.clearResults;
    % Close Test Manager
    sltest.testmanager.close;


    %-----This part is what I want to achieve my goal----
    totalfailures = 0;
    totalfailures = sum(vertcat(ro(:).Failed));   
    if totalfailures == 0
        exit(0); 
    else
        exit(1);
    end
    %----------but it couldn't work----------------------


catch e
    disp(getReport(e,'extended'));
    exit(1);
end
exit(totalfailures>0);

我在詹金斯(Jenkins)中检查我的退出状态为0,但是在测试文件中测试失败,因此应该为1。

在此先感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您可以考虑使用MATLAB单元测试框架来运行测试并获取测试结果。这将为您提供一个结果对象,您可以轻松查询该对象以控制MATLAB的退出代码。如果您要这样运行Simulink Test文件:

import matlab.unittest.TestRunner
import matlab.unittest.TestSuite
import sltest.plugins.TestManagerResultsPlugin

try
    suite = TestSuite.fromFolder('<path to folder with Simulink Tests>');
    % Create a typical runner with text output
    runner = TestRunner.withTextOutput();
    % Add the Simulink Test Results plugin and direct its output to a file
    sltestresults = fullfile(getenv('WORKSPACE'), 'sltestresults.mldatx');
    runner.addPlugin(TestManagerResultsPlugin('ExportToFile', sltestresults));
    % Run the tests
    results = runner.run(suite);
    display(results);
catch e
    disp(getReport(e,'extended'));
    exit(1);
end
exit(any([results.Failed]));

应该可以解决问题。您可以根据需要进行其他修改,以节省测试套件或测试用例。

您也可以考虑使用matlab.unittest.plugins.TAPPlugin,它与Jenkins很好地集成在一起以发布TAP格式测试结果。这里提到的所有插件和其他API都有MathWorks文档。这是一篇很好的文章,告诉您如何利用MATLAB单元测试框架来运行Simulink测试:https://www.mathworks.com/help/sltest/ug/tests-for-continuous-integration.html

此外,MathWorks最近还发布了Jenkins MATLAB插件,可能对您有所帮助:https://plugins.jenkins.io/matlab

希望这会有所帮助!

答案 1 :(得分:0)

我认为您需要在运行作业后检查Jenkins中的日志以查看错误。因为在Jenkins中,我们需要像运行的机器一样设置环境差异。