Files
.vs
Assets
Library
APIUpdater
Artifacts
Bee
BuildPlayerData
PackageCache
com.unity.collab-proxy@2.5.2
com.unity.editorcoroutines@1.0.0
com.unity.ext.nunit@1.0.6
com.unity.feature.development@1.0.1
com.unity.ide.rider@3.0.31
com.unity.ide.visualstudio@2.0.22
com.unity.ide.vscode@1.2.5
com.unity.modules.ai@1.0.0
com.unity.modules.androidjni@1.0.0
com.unity.modules.animation@1.0.0
com.unity.modules.assetbundle@1.0.0
com.unity.modules.audio@1.0.0
com.unity.modules.cloth@1.0.0
com.unity.modules.director@1.0.0
com.unity.modules.imageconversion@1.0.0
com.unity.modules.imgui@1.0.0
com.unity.modules.jsonserialize@1.0.0
com.unity.modules.particlesystem@1.0.0
com.unity.modules.physics2d@1.0.0
com.unity.modules.physics@1.0.0
com.unity.modules.screencapture@1.0.0
com.unity.modules.subsystems@1.0.0
com.unity.modules.terrain@1.0.0
com.unity.modules.terrainphysics@1.0.0
com.unity.modules.tilemap@1.0.0
com.unity.modules.ui@1.0.0
com.unity.modules.uielements@1.0.0
com.unity.modules.umbra@1.0.0
com.unity.modules.unityanalytics@1.0.0
com.unity.modules.unitywebrequest@1.0.0
com.unity.modules.unitywebrequestassetbundle@1.0.0
com.unity.modules.unitywebrequestaudio@1.0.0
com.unity.modules.unitywebrequesttexture@1.0.0
com.unity.modules.unitywebrequestwww@1.0.0
com.unity.modules.vehicles@1.0.0
com.unity.modules.video@1.0.0
com.unity.modules.vr@1.0.0
com.unity.modules.wind@1.0.0
com.unity.modules.xr@1.0.0
com.unity.performance.profile-analyzer@1.2.2
com.unity.settings-manager@2.0.1
com.unity.test-framework@1.1.33
Documentation~
images
TableOfContents.md
edit-mode-vs-play-mode-tests.md
extending.md
extension-get-test-results.md
extension-retrieve-test-list.md
extension-run-tests.md
getting-started.md
index.md
manual.md
reference-actions-outside-tests.md
reference-attribute-conditionalignore.md
reference-attribute-testmustexpectalllogs.md
reference-attribute-testplayerbuildmodifier.md
reference-attribute-testruncallback.md
reference-attribute-unityplatform.md
reference-attribute-unitytest.md
reference-command-line.md
reference-comparer-color.md
reference-comparer-equals.md
reference-comparer-float.md
reference-comparer-quaternion.md
reference-comparer-vector2.md
reference-comparer-vector3.md
reference-comparer-vector4.md
reference-custom-assertion.md
reference-custom-attributes.md
reference-custom-constraints.md
reference-custom-equality-comparers.md
reference-custom-yield-instructions.md
reference-execution-settings.md
reference-filter.md
reference-icallbacks.md
reference-ierror-callbacks.md
reference-itest-adaptor.md
reference-itest-result-adaptor.md
reference-itest-run-settings.md
reference-outerunitytestaction.md
reference-recompile-scripts.md
reference-setup-and-cleanup.md
reference-test-runner-api.md
reference-test-utils.md
reference-tests-monobehaviour.md
reference-tests-parameterized.md
reference-unitysetup-and-unityteardown.md
reference-wait-for-domain-reload.md
resources.md
workflow-create-playmode-test.md
workflow-create-test-assembly.md
workflow-create-test.md
workflow-run-playmode-test-standalone.md
workflow-run-test.md
UnityEditor.TestRunner
UnityEngine.TestRunner
.signature
CHANGELOG.md
CHANGELOG.md.meta
CONTRIBUTING.md
CONTRIBUTING.md.meta
LICENSE.md
LICENSE.md.meta
UnityEditor.TestRunner.meta
UnityEngine.TestRunner.meta
package.json
package.json.meta
com.unity.testtools.codecoverage@1.2.6
com.unity.textmeshpro@3.0.6
com.unity.timeline@1.7.6
com.unity.ugui@1.0.0
com.unity.visualscripting@1.9.4
PackageManager
PlayModeViewStates
PlayerDataCache
ScriptAssemblies
Search
ShaderCache
SplashScreenCache
StateCache
UIElements
AnnotationManager
ArtifactDB
ArtifactDB-lock
BuildPlayer.prefs
BuildSettings.asset
EditorOnlyScriptingSettings.json
EditorOnlyVirtualTextureState.json
EditorSnapSettings.asset
EditorUserBuildSettings.asset
InspectorExpandedItems.asset
LastBuild.buildreport
LastSceneManagerSetup.txt
LibraryFormatVersion.txt
MonoManager.asset
SceneVisibilityState.asset
ScriptMapper
ShaderCache.db
SourceAssetDB
SourceAssetDB-lock
SpriteAtlasDatabase.asset
Style.catalog
expandedItems
ilpp.pid
Logs
Packages
ProjectSettings
UserSettings
obj
.vsconfig
Assembly-CSharp.Player.csproj
Assembly-CSharp.csproj
TM1.sln
Unity.CollabProxy.Editor.csproj
Unity.EditorCoroutines.Editor.csproj
Unity.Performance.Profile-Analyzer.Editor.csproj
Unity.PlasticSCM.Editor.csproj
Unity.Rider.Editor.csproj
Unity.Settings.Editor.csproj
Unity.TestTools.CodeCoverage.Editor.OpenCover.Model.csproj
Unity.TestTools.CodeCoverage.Editor.OpenCover.Mono.Reflection.csproj
Unity.TestTools.CodeCoverage.Editor.csproj
Unity.TextMeshPro.Editor.csproj
Unity.TextMeshPro.Player.csproj
Unity.TextMeshPro.csproj
Unity.Timeline.Editor.csproj
Unity.Timeline.Player.csproj
Unity.Timeline.csproj
Unity.VSCode.Editor.csproj
Unity.VisualScripting.Core.Editor.csproj
Unity.VisualScripting.Core.Player.csproj
Unity.VisualScripting.Core.csproj
Unity.VisualScripting.Flow.Editor.csproj
Unity.VisualScripting.Flow.Player.csproj
Unity.VisualScripting.Flow.csproj
Unity.VisualScripting.SettingsProvider.Editor.csproj
Unity.VisualScripting.Shared.Editor.csproj
Unity.VisualScripting.State.Editor.csproj
Unity.VisualScripting.State.Player.csproj
Unity.VisualScripting.State.csproj
Unity.VisualStudio.Editor.csproj
UnityEditor.TestRunner.csproj
UnityEditor.UI.csproj
UnityEngine.TestRunner.Player.csproj
UnityEngine.TestRunner.csproj
UnityEngine.UI.Player.csproj
UnityEngine.UI.csproj
TP_Multimedia/Library/PackageCache/com.unity.test-framework@1.1.33/Documentation~/reference-attribute-testplayerbuildmodifier.md

106 lines
5.1 KiB
Markdown
Raw Normal View History

2025-01-17 13:10:20 +01:00
# TestPlayerBuildModifier attribute
You can use the `TestPlayerBuildModifier` attribute to accomplish a couple of different scenarios:
## Modify the Player build options for Play Mode tests
It is possible to change the [BuildPlayerOptions](https://docs.unity3d.com/ScriptReference/BuildPlayerOptions.html) for the test **Player**, to achieve custom behavior when running **Play Mode** tests. Modifying the build options allows for changing the target location of the build as well as changing [BuildOptions](https://docs.unity3d.com/ScriptReference/BuildOptions.html).
To modify the `BuildPlayerOptions`, do the following:
* Implement the `ITestPlayerBuildModifier`
* Reference the implementation type in a `TestPlayerBuildModifier` attribute on an assembly level.
### Example
```c#
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;
}
}
```
> **Note:** When building the Player, it includes all `TestPlayerBuildModifier` attributes across all loaded assemblies, independent of the currently used test filter. As the implementation references the `UnityEditor` namespace, the code is typically implemented in an Editor only assembly, as the `UnityEditor` namespace is not available otherwise.
## Split build and run
It is possible to use the Unity Editor for building the Player with tests, without [running the tests](./workflow-run-playmode-test-standalone.md). This allows for running the Player on e.g. another machine. In this case, it is necessary to modify the Player to build and implement a custom handling of the test result.
By using `TestPlayerBuildModifier`, you can alter the `BuildOptions` to not start the Player after the build as well as build the Player at a specific location. Combined with [PostBuildCleanup](./reference-setup-and-cleanup.md#prebuildsetup-and-postbuildcleanup), you can automatically exit the Editor on completion of the build.
### Example
```c#
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");
}
}
}
```
If the Editor is still running after the Play Mode tests have run, the Player tries to report the results back, using [PlayerConnection](https://docs.unity3d.com/ScriptReference/Networking.PlayerConnection.PlayerConnection.html), which has a reference to the IP address of the Editor machine, when built.
To implement a custom way of reporting the results of the test run, let one of the assemblies in the Player include a [TestRunCallback](./reference-attribute-testruncallback.md). At `RunFinished`, it is possible to get the full test report as XML from the [NUnit](http://www.nunit.org/) test result by calling `result.ToXml(true)`. You can save the result and then save it on the device or send it to another machine as needed.