介绍

由于制作一个支持各种Unity版本的库并在手头测试它很麻烦,

  • 在 GitHub 操作中
  • 指定多个版本的 Unity
  • 在所有版本上使用 Unity Test Framework(以前称为 Unity Test Runner)运行测试

我写了一个这样的工作流程,所以我将介绍它。

顺便请注意,开头推文中的图片内容有问题,测试只能使用单个 Unity 版本运行。

提前准备

需要提前围绕Unity License做前期准备。

我使用以下文章作为参考。

即使您使用多个 Unity 版本运行测试,如果您可以有条件地使用个人许可证,也可以。

运行工作流

在 Secrets 中注册 Unity 的许可信息,↓

{リポジトリのルート}/.github/workflows/

将其放在下面将在推送到 GitHub 时运行测试。

运行-test.yml
name: Run Test

on: [push]

jobs:
  test:
    name: Test in Unity${{ matrix.unityVersion }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        unityVersion:
          - 2021.3.0f1  # 好きなバージョンを指定
          - 2022.1.0f1  # 好きなバージョンを指定
    steps:
      - uses: actions/checkout@v3
        with:
          lfs: true
          path: Unity${{ matrix.unityVersion }}
      - uses: actions/cache@v3
        with:
          path: Unity${{ matrix.unityVersion }}/Library
          key: Library-Unity${{ matrix.unityVersion }}
          restore-keys: |
            Library-
      - uses: game-ci/unity-test-runner@5263cf0ab1d1c310c57c1861c71324b7e273909c
        id: tests
        env:
          UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
        with:
          unityVersion: ${{ matrix.unityVersion }}
          projectPath: Unity${{ matrix.unityVersion }}
          artifactsPath: Unity${{ matrix.unityVersion }}-artifacts
          githubToken: ${{ secrets.GITHUB_TOKEN }}
          checkName: Unity${{ matrix.unityVersion }} Test Results
      - uses: actions/upload-artifact@v3
        if: always()
        with:
          name: Test results for Unity${{ matrix.unityVersion }}
          path: ${{ steps.tests.outputs.artifactsPath }}

当我运行此工作流程时

  • Unity2021.3.0f1
  • Unity2022.1.0f1

测试在两个版本中的每一个上运行

请在使用时适当添加/删除/更改# 好きなバージョンを指定 行。

 
如果您一开始不了解 Unity 测试框架,以下内容非常有帮助。

GameCI 示例代码有点老了

之前介绍的工作流程是游戏 CI 文档我参考下面的描述。

name: Test project

on: [push, pull_request]

jobs:
  testAllModes:
    name: Test in ${{ matrix.testMode }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        projectPath:
          - test-project
        testMode:
          - playmode
          - editmode
    steps:
      - uses: actions/checkout@v2
        with:
          lfs: true
      - uses: actions/cache@v2
        with:
          path: ${{ matrix.projectPath }}/Library
          key: Library-${{ matrix.projectPath }}
          restore-keys: |
            Library-
      - uses: game-ci/unity-test-runner@v2
        id: tests
        env:
          UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
        with:
          projectPath: ${{ matrix.projectPath }}
          testMode: ${{ matrix.testMode }}
          artifactsPath: ${{ matrix.testMode }}-artifacts
          githubToken: ${{ secrets.GITHUB_TOKEN }}
          checkName: ${{ matrix.testMode }} Test Results
          coverageOptions: 'generateAdditionalMetrics;generateHtmlReport;generateBadgeReport;assemblyFilters:+my.assembly.*'
      - uses: actions/upload-artifact@v2
        if: always()
        with:
          name: Test results for ${{ matrix.testMode }}
          path: ${{ steps.tests.outputs.artifactsPath }}
      - uses: actions/upload-artifact@v2
        if: always()
        with:
          name: Coverage results for ${{ matrix.testMode }}
          path: ${{ steps.tests.outputs.coveragePath }}

更改和使用时有一些注意事项,因此我将对其进行介绍。

我收到有关 Node.js 12 的警告

如果保持原样,则在执行工作流时会出现以下警告。

不推荐使用 Node.js 12 个操作。有关更多信息,请参阅:https://github.blog/changelog/2022-09-22-github-actions-all-actions-will-begin-running-on-node16-instead-of-node12/请更新以下操作以使用 Node.js 16:actions/checkout、actions/cache、game-ci/unity-test-runner、actions/upload-artifact、actions/upload-artifact、actions/cache、actions/checkout

正如警告的那样,增加下面正在使用的操作的版本。只需将v2 替换为v3

  • actions/checkout@v2    → actions/checkout@v3
  • actions/cache@v2      → actions/cache@v3
  • actions/upload-artifact@v2actions/upload-artifact@v3

仅供参考:GitHub Actions 将放弃对 Node.js 12 的支持

我收到关于set-output 的警告

我仍然收到以下警告:

set-output 命令已弃用并将很快禁用。请升级为使用环境文件。有关更多信息,请参阅:https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/

参考:save-stateset-output 在 GitHub Actions 中似乎已被弃用。

这是GameCI的game-ci/unity-test-runner@v2造成的,所以我会升级这个版本。

game-ci/unity-test-runner的最新版本目前是v2.0.3,但修复目前只在main分支(第193章,第194章),因此您可以直接指定最新提交的哈希值。

  • game-ci/unity-test-runner@v2
    game-ci/unity-test-runner@5263cf0ab1d1c310c57c1861c71324b7e273909c

如果您在阅读本文时发布了v2.0.4,您应该指定它而不是提交哈希。

很重要的一点

使用多个 Unity 版本运行测试会增加执行时间,因此在使用免费层运行时要小心。

参考:

奖励:测试编译是否在所有平台上通过

我正在制作一个库/资产/框架,以保证使用多个 Unity 版本进行操作,而我想要的最起码的事情是“测试构建是否通过支持的 Unity 版本”。

所以我写了一个在 C# 9.0 Unity 中运行的测试。可编写脚本的构建管道包是必需的。

编译测试.cs
using System.IO;
using NUnit.Framework;
using UnityEditor;
using UnityEditor.Build.Player;

public class CompilationTest
{
    private static readonly string OutputPath = $"Temp/{nameof(CompilationTest)}";

    [Test] public void StandaloneOSX() => Assert.IsTrue(Compile(BuildTarget.StandaloneOSX));
    [Test] public void StandaloneWindows() => Assert.IsTrue(Compile(BuildTarget.StandaloneWindows));
    [Test] public void iOS() => Assert.IsTrue(Compile(BuildTarget.iOS));
    [Test] public void Android() => Assert.IsTrue(Compile(BuildTarget.Android));
    [Test] public void StandaloneWindows64() => Assert.IsTrue(Compile(BuildTarget.StandaloneWindows64));
    [Test] public void WebGL() => Assert.IsTrue(Compile(BuildTarget.WebGL));
    [Test] public void WSAPlayer() => Assert.IsTrue(Compile(BuildTarget.WSAPlayer));
    [Test] public void StandaloneLinux64() => Assert.IsTrue(Compile(BuildTarget.StandaloneLinux64));
    [Test] public void PS4() => Assert.IsTrue(Compile(BuildTarget.PS4));
    [Test] public void XboxOne() => Assert.IsTrue(Compile(BuildTarget.XboxOne));
    [Test] public void tvOS() => Assert.IsTrue(Compile(BuildTarget.tvOS));
    [Test] public void Switch() => Assert.IsTrue(Compile(BuildTarget.Switch));
    [Test] public void Lumin() => Assert.IsTrue(Compile(BuildTarget.Lumin));
    [Test] public void Stadia() => Assert.IsTrue(Compile(BuildTarget.Stadia));
    [Test] public void CloudRendering() => Assert.IsTrue(Compile(BuildTarget.CloudRendering));
    [Test] public void GameCoreXboxSeries() => Assert.IsTrue(Compile(BuildTarget.GameCoreXboxSeries));
    [Test] public void GameCoreXboxOne() => Assert.IsTrue(Compile(BuildTarget.GameCoreXboxOne));
    [Test] public void PS5() => Assert.IsTrue(Compile(BuildTarget.PS5));
    [Test] public void EmbeddedLinux() => Assert.IsTrue(Compile(BuildTarget.EmbeddedLinux));

    private static bool Compile(BuildTarget target)
    {
        var input = new ScriptCompilationSettings
        {
            target = target,
            group = BuildPipeline.GetBuildTargetGroup(target),
        };

        var result = PlayerBuildInterface.CompilePlayerScripts(input, OutputPath);
        var assemblies = result.assemblies;
        var passed = (assemblies is { Count: > 0 } && (result.typeDB != null));

        if (Directory.Exists(OutputPath)) Directory.Delete(OutputPath, true);

        return passed;
    }
}

参考:

其他可能有用的链接


原创声明:本文系作者授权爱码网发表,未经许可,不得转载;

原文地址:https://www.likecs.com/show-308631968.html

相关文章: