【问题标题】:Unity Unit Tests fail, but GitLab CI pipeline job passes (shell executor)Unity 单元测试失败,但 GitLab CI 管道作业通过(shell 执行器)
【发布时间】:2021-03-20 22:30:16
【问题描述】:

在我的 Unity 项目中实施持续集成时遇到的这个问题让我大吃一惊。

我下载了 Gitlab CI Runner 并将我的台式电脑注册为我项目的特定运行器。我将执行者设置为shell,并且没有为我的跑步者设置任何标签。

然后我编写了一个简单的测试,其中包含一行 Assert.Fail()。如果我进入 Unity > Window > General > Test Runner 并运行我的测试 - 它失败:

然后我在我的项目中添加了gitlab-ci.yml

stages:
  - test
  - build
  - deploy

unit-test:
  script: 
    - echo testing ; & "C:\Program Files\Unity\Hub\Editor\2020.1.4f1\Editor\Unity.exe" -batchmode -projectPath "C:\workspace\automatedtesting\Simple Lane Runner" -runTests -testPlatform editmode -testResults "C:\workspace\automatedtesting\testresults\results.xml" -logFile "C:\workspace\automatedtesting\testresults\logfile.log"
  stage: test

如果我在本地运行script 上的线路,我的计算机会有点忙,回显“测试”。如果我然后转到我的logfile.logresults.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 Files\Unity\Hub\Editor\2020.1.4f1\Editor\Unity.exe" -batchmode -projectPath "C:\workspace\automatedtesting\Simple Lane Runner" -runTests -testPlatform editmode -testResults "C:\workspace\automatedtesting\testresults\results.xml" -logFile "C:\workspace\automatedtesting\testresults\logfile.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 上找到的其他“示例统一项目”)除了我的 .yml 中的内容之外,我没有看到其他任何事情发生。我在监督什么吗?

【问题讨论】:

  • 我认为使用调用符号不一定能捕获输出。你试过没有'echo'或'&'吗?
  • 我试过没有echo,没有回显.yml文件将被标记为无效。如果我也省略了 &,它也会将语法标记为无效。
  • 您可以将:before_script: $env:Path = $env:Path + ";C:\Program Files\Unity\Hub\Editor\2020.1.4f1\Editor\" 添加到您的gitlab-ci.yml,然后您应该可以直接调用Unity.exe,无需& 或路径。
  • 这是一个很好的说明,谢谢。我最终做的是将我的项目和统一编辑器移动到不包含任何空格的目录,完全消除对字符串的需要。但我同意这有点糟糕:)

标签: unity3d gitlab gitlab-ci


【解决方案1】:

好的解决了!

因此,Gitlab CI 作业的输出等于您正在运行的程序的退出代码的输出。问题是仅运行此行未能返回正确的退出代码。

解决方案是创建一个新的 PowerShell 脚本,该脚本运行统一行,然后将其退出代码存储到变量中。然后我们在记住unity的退出代码的情况下退出:

& "C:\UnityEditors\2020.1.17f1\Editor\Unity.exe" -batchMode -projectPath . -runTests -logFile .\unitTests.log -testResults .\unitTests.xml | Tee-Object -FilePath .\runner.log
$UNITYCODE = $LastExitCode
Exit $UNITYCODE

我们会将这个脚本存储在<root of git repo>\CIScripts 中作为runTests.ps1

现在可以将 gitlab-ci.yml 更改为只运行 powershell 脚本,然后返回统一退出代码:

stages:
  - test

unit-test:
  script: 
    - CIScripts/runTests.ps1
  stage: test

当我的测试包含 Assert.Fail() 并在测试通过时通过时,我的管道作业现在失败!万岁!

我们现在还可以为构建/部署阶段编写单独的 powershell 脚本,并相应地调用它们。

编辑:另一个更简单的解决方案是在统一行的末尾添加| Out-Default

例如,这个 gitlab-ci.yml 将正确地测试和通过/失败我的管道作业:

stages:
  - test

unit-test:
  script: 
    - C:\UnityEditors\2020.1.17f1\Editor\Unity.exe -batchMode -projectPath . -runTests -logFile .\unitTests.log -testResults .\unitTests.xml | Out-Default
  stage: test

【讨论】:

    猜你喜欢
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 2019-02-01
    相关资源
    最近更新 更多