Zak*_*ani 7 matlab unit-testing gitlab-ci
我有gitlab CI运行测试一些脚本,我使用以下几行.gitlab-ci.yml来显示MATLAB构建的输出:
before_script:
test1:
script:
- matlab -nosplash -nodesktop -minimize -wait -logfile matlab-output.txt -r Model
- type matlab-output.txt
Run Code Online (Sandbox Code Playgroud)
这种方法在构建成功时非常有效,但是当它失败时却没有,因为第二个命令没有运行.我检查了gitlab-ci-runner,它没有'after_script'选项.你怎么解决这个问题?
注意:这是Windows.
我认为你的问题是双重的.部分原因是因为GITLAB不会调用您的type语句,而且MATLAB进程永远不会返回,因为脚本永远不会完成.
例如,在命令行键入以下内容:
# This one will fail and notice that it never ends
matlab -nodesktop -nosplash -minimize -wait -logfile log.txt -r 'disp(a); exit;'
Run Code Online (Sandbox Code Playgroud)
这是因为MATLAB永远不能执行exit命令.
另一方面,在成功的情况下,它能够到达exit并因此返回.
# This one will pass
matlab -nodesktop -nosplash -minimize -wait -logfile log.txt -r 'disp(1); exit;'
Run Code Online (Sandbox Code Playgroud)
我实际解决这个问题的方式是双重的.首先,我在try/catch语句中包装我试图调用的命令,然后将任何错误/异常转换为字符串格式并显示它们.
我在一个名为的文件中有这种东西 runtests.m
% runtests.m
exit_code = 0;
try
Model
catch ME
disp(getReport(ME))
exit_code = 1;
end
% Ensure that we ALWAYS call exit
exit(exit_code);
Run Code Online (Sandbox Code Playgroud)
然后我有一个bash脚本实际调用MATLAB并打印日志输出并返回从MATLAB返回的相同错误代码
# runtests.sh
LOGFILE=log.txt
matlab -nodesktop -nosplash -minimize -wait -logfile "$LOGFILE" -r 'runtests';
CODE=$?
cat "$LOGFILE"
exit $CODE
Run Code Online (Sandbox Code Playgroud)
这里的额外好处是我的用户可以完全按照GITLAB CI在自己的机器上运行测试来运行测试.
然后我的.gitlab-ci.yml文件很简单
test1:
script:
- "runtests.sh"
Run Code Online (Sandbox Code Playgroud)
抱歉耽误了这么长时间,并感谢其他人的帮助。我现在有一个使用 Suever 代码运行的系统。我对其进行了修改以适应 MATLAB 的 Unit 框架。
所以,最后我的 .gitlab-ci.yml 是:
before_script:
Model-01:
script:
- cd developers\testModel\01
- Gitlab_CI_Hook.cmd
Model-02:
script:
- cd developers\testModel\02
- Gitlab_CI_Hook.cmd
Run Code Online (Sandbox Code Playgroud)
其中 Gitlab_CI_Hook.cmd 是:
matlab -nosplash -nodesktop -minimize -wait -logfile matlab-output.txt -r Git_MATLAB_interface
type matlab-output.txt
set content=
for /f "delims=" %%i in (ExitCode.txt) do set content=%content% %%i
exit %content%
Run Code Online (Sandbox Code Playgroud)
和 Git_MATLAB_interface.m
% assume it always fails
exit_code = 1;
% MATLAB runs the test
result = runtests(pwd)
% check the result
if result.Passed ~= 0 && result.Failed == 0 && result.Incomplete == 0
exit_code = 0;
end
% write the ExitCode
fid = fopen('ExitCode.txt','w');
fprintf(fid,'%d',exit_code);
fclose(fid);
% Ensure that we ALWAYS call exit that is always a success so that CI doesn't stop
exit
Run Code Online (Sandbox Code Playgroud)