Version: 6000.3
语言: 中文
在播放器中运行播放模式测试
Unity 测试框架学习材料

修改用于测试的 Player 构建

你可以在特定于平台的独立播放器中运行运行模式测试。这[TestPlayerBuildModifier]属性提供了许多可配置的选项来控制为运行测试而构建的播放器的属性。

修改播放模式测试的播放器构建选项

你可以更改测试播放器的 BuildPlayerOptions,以便在运行运行模式测试时实现自定义行为。修改构建选项允许更改构建的目标位置以及更改 BuildOptions

要修改BuildPlayerOptions,请执行以下作:

  • 实现ITestPlayerBuildModifier
  • TestPlayerBuildModifier属性。

以下示例演示了这一点:

using UnityEditor;
using UnityEditor.TestTools;

[assembly:TestPlayerBuildModifier(typeof(BuildModifier))]
public class BuildModifier : ITestPlayerBuildModifier
{
    public BuildPlayerOptions ModifyOptions(BuildPlayerOptions playerOptions)
    {
        if (playerOptions.target == BuildTarget.iOS)
        {
            playerOptions.options |= BuildOptions.SymlinkLibraries; // Enable symlink libraries when running on iOS
        }
        
        playerOptions.options |= BuildOptions.AllowDebugging; // Enable allow Debugging flag on the test Player.
        return playerOptions;
    }
}

注意:构建播放器时,它包括所有TestPlayerBuildModifier所有加载的程序集的属性,与当前使用的测试筛选器无关。由于实现引用了UnityEditor命名空间,则代码通常在仅编辑器程序集中实现,如UnityEditor否则命名空间不可用。

在不运行测试的情况下构建播放器

可以使用 Unity 编辑器生成带有测试但不运行测试的播放器。例如,这允许在另一台计算机上运行播放器。在这种情况下,需要修改 Player 以构建和实现测试结果的自定义处理。

您可以使用[TestPlayerBuildModifier]BuildOptions将播放器构建到特定位置,但不运行它。结合PostBuildCleanup,你可以在构建完成后自动退出编辑器。以下示例演示了这一点:

using System;
using System.IO;
using System.Linq;
using Tests;
using UnityEditor;
using UnityEditor.TestTools;
using UnityEngine;
using UnityEngine.TestTools;

[assembly:TestPlayerBuildModifier(typeof(HeadlessPlayModeSetup))]
[assembly:PostBuildCleanup(typeof(HeadlessPlayModeSetup))]

namespace Tests
{
    public class HeadlessPlayModeSetup : ITestPlayerBuildModifier, IPostBuildCleanup
    {
        private static bool s_RunningPlayerTests;
        public BuildPlayerOptions ModifyOptions(BuildPlayerOptions playerOptions)
        {
            // Do not launch the player after the build completes.
            playerOptions.options &= ~BuildOptions.AutoRunPlayer;

            // Set the headlessBuildLocation to the output directory you desire. It does not need to be inside the project.
            var headlessBuildLocation = Path.GetFullPath(Path.Combine(Application.dataPath, ".//..//PlayModeTestPlayer"));
            var fileName = Path.GetFileName(playerOptions.locationPathName);
            if (!string.IsNullOrEmpty(fileName))
            {
                headlessBuildLocation = Path.Combine(headlessBuildLocation, fileName);
            }
            playerOptions.locationPathName = headlessBuildLocation;

            // Instruct the cleanup to exit the Editor if the run came from the command line. 
            // The variable is static because the cleanup is being invoked in a new instance of the class.
            s_RunningPlayerTests = true;
            return playerOptions;
        }

        public void Cleanup()
        {
            if (s_RunningPlayerTests && IsRunningTestsFromCommandLine())
            {
                // Exit the Editor on the next update, allowing for other PostBuildCleanup steps to run.
                EditorApplication.update += () => { EditorApplication.Exit(0); };
            }
        }

        private static bool IsRunningTestsFromCommandLine()
        {
            var commandLineArgs = Environment.GetCommandLineArgs();
            return commandLineArgs.Any(value => value == "-runTests");
        }
    }
}

如果在运行模式测试运行后编辑器仍在运行,则播放器会尝试使用 PlayerConnection 报告结果,该 PlayerConnection 在构建时引用了编辑器计算机的 IP 地址。

若要实现报告测试运行结果的自定义方式,请让 Player 中的一个程序集包含TestRunCallback.在RunFinished,可以通过调用 NUnit 测试结果从 NUnit 测试结果中获取完整的测试报告 XML 格式result.ToXml(true).您可以保存结果,然后将其保存在设备上或根据需要将其发送到另一台机器。

其他资源

在播放器中运行播放模式测试
Unity 测试框架学习材料