我使用matlab脚本在测试manager.And中创建测试文件(包括测试套件和测试用例)当我完成测试时,我需要使用test.If的结果,测试用例都通过了,退出代码为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,但是我在测试file.So中做了一个失败的测试,它应该是1。
提前感谢您的帮助!
发布于 2019-08-20 05:16:26
您可以考虑使用MATLAB单元测试框架来运行测试并获得测试结果。这将为您提供一个results对象,您可以轻松地查询该对象以控制MATLAB的退出代码。如果您要运行Simulink测试文件,如下所示:
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]));这应该能起到作用。您可以根据自己的喜好对其进行修改,以保存测试套件或测试用例。
您还可以考虑使用与Jenkins很好地集成的matlab.unittest.plugins.TAPPlugin来发布TAP格式的测试结果。这里提到的所有插件和其他API都提供了MathWorks文档。这里有一篇很好的文章,告诉你如何利用MATLAB Unit Test Framework来运行Simulink测试:https://www.mathworks.com/help/sltest/ug/tests-for-continuous-integration.html
此外,MathWorks最近发布了一个可能对您有帮助的Jenkins MATLAB插件:https://plugins.jenkins.io/matlab
希望这能有所帮助!
https://stackoverflow.com/questions/57407506
复制相似问题