问题描述
我在Unity项目中实施持续集成时遇到的这个问题让我大吃一惊。
我下载了GitLab CI Runner,并将我的台式PC注册为我的项目的特定Runner。我将执行器设置为shell
,并且没有为我的跑步器设置任何标记。然后我编写了一个简单的测试,其中包含一行内容为Assert.Fail()
。如果我进入Unity&>Window>;General&>Test Runner并运行我的测试-它失败:
然后我向我的项目添加了gitlab-ci.yml
:
stages:
- test
- build
- deploy
unit-test:
script:
- echo testing ; & "C:Program FilesUnityHubEditor2020.1.4f1EditorUnity.exe" -batchmode -projectPath "C:workspaceautomatedtestingSimple Lane Runner" -runTests -testPlatform editmode -testResults "C:workspaceautomatedtesting estresults
esults.xml" -logFile "C:workspaceautomatedtesting estresultslogfile.log"
stage: test
如果我在script
上本地运行行,我的计算机变得有点忙,会回显‘测试’。如果我随后转到我的logfile.log
或我的results.xml
,我可以看到它们刚刚创建,并且包含说明我的测试失败的信息。但是,管道作业只是简单地成功了。它不会引发任何错误。Running with gitlab-runner 13.6.0 (8fa89735)
on Kamiel-Desktop-Runner 69UDXkRe
Resolving secrets
00:00
Preparing the "shell" executor
00:00
Using Shell executor...
Preparing environment
00:00
Running on DESKTOP-ONQCMQA...
Getting source from Git repository
00:02
Fetching changes with git depth set to 50...
Reinitialized existing Git repository in C:/GitLab-Runner/builds/69UDXkRe/0/visserk18/automatedtesting/.git/
Checking out 7a897989 as master...
git-lfs/2.10.0 (GitHub; windows amd64; go 1.12.7; git a526ba6b)
Skipping Git submodules setup
Executing "step_script" stage of the job script
00:08
$ echo testing ; & "C:Program FilesUnityHubEditor2020.1.4f1EditorUnity.exe" -batchmode -projectPath "C:workspaceautomatedtestingSimple Lane Runner" -runTests -testPlatform editmode -testResults "C:workspaceautomatedtesting estresults
esults.xml" -logFile "C:workspaceautomatedtesting estresultslogfile.log"
testing
Cleaning up file based variables
00:00
Job succeeded
我是不是错过了什么?这项工作不是应该一遇到失败的测试就失败吗?无论我往哪里看(例如,在本指南中:https://engineering.etermax.com/continuous-integration-in-unity-with-gitlab-ci-cd-part-1-e902c94c0847或我可以在GitHub上找到的其他"示例Unity项目"),除了我的.yml
之外,我什么也看不到。我在监督什么吗?
推荐答案
确定解决!
因此GitLab CI作业的输出等于您正在运行的程序的退出代码的输出。问题在于,仅运行此行无法返回正确的退出代码。
解决方案是创建一个新的PowerShell脚本,该脚本运行unity行,然后将其退出代码存储到一个变量中。然后,我们带着Unity的退出代码退出:
& "C:UnityEditors2020.1.17f1EditorUnity.exe" -batchMode -projectPath . -runTests -logFile .unitTests.log -testResults .unitTests.xml | Tee-Object -FilePath .
unner.log
$UNITYCODE = $LastExitCode
Exit $UNITYCODE
我们将在<root of git repo>CIScripts
中将此脚本存储为runTests.ps1
现在可以将giLab-ci.yml更改为只运行Powershell脚本,然后该脚本将返回unity退出代码:
stages:
- test
unit-test:
script:
- CIScripts/runTests.ps1
stage: test
现在,我的管道作业在我的测试包含Assert.Fail()
时失败,并在测试通过时通过!万岁!我们现在还可以为生成/部署阶段编写单独的Powershell脚本,并相应地调用它们。
编辑:另一个更简单的解决方案是在单位线的末尾添加| Out-Default
。
例如,此GitLab-ci.yml将测试并正确通过/失败我的管道作业:
stages:
- test
unit-test:
script:
- C:UnityEditors2020.1.17f1EditorUnity.exe -batchMode -projectPath . -runTests -logFile .unitTests.log -testResults .unitTests.xml | Out-Default
stage: test
这篇关于Unity单元测试失败,但GitLab CI管道作业通过(外壳执行器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!