SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
5.1 KiB

4 years ago
  1. # TestPlayerBuildModifier attribute
  2. You can use the `TestPlayerBuildModifier` attribute to accomplish a couple of different scenarios:
  3. ## Modify the Player build options for Play Mode tests
  4. 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).
  5. To modify the `BuildPlayerOptions`, do the following:
  6. * Implement the `ITestPlayerBuildModifier`
  7. * Reference the implementation type in a `TestPlayerBuildModifier` attribute on an assembly level.
  8. ### Example
  9. ```c#
  10. using UnityEditor;
  11. using UnityEditor.TestTools;
  12. [assembly:TestPlayerBuildModifier(typeof(BuildModifier))]
  13. public class BuildModifier : ITestPlayerBuildModifier
  14. {
  15. public BuildPlayerOptions ModifyOptions(BuildPlayerOptions playerOptions)
  16. {
  17. if (playerOptions.target == BuildTarget.iOS)
  18. {
  19. playerOptions.options |= BuildOptions.SymlinkLibraries; // Enable symlink libraries when running on iOS
  20. }
  21. playerOptions.options |= BuildOptions.AllowDebugging; // Enable allow Debugging flag on the test Player.
  22. return playerOptions;
  23. }
  24. }
  25. ```
  26. > **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.
  27. ## Split build and run
  28. 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.
  29. 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.
  30. ### Example
  31. ```c#
  32. using System;
  33. using System.IO;
  34. using System.Linq;
  35. using Tests;
  36. using UnityEditor;
  37. using UnityEditor.TestTools;
  38. using UnityEngine;
  39. using UnityEngine.TestTools;
  40. [assembly:TestPlayerBuildModifier(typeof(HeadlessPlayModeSetup))]
  41. [assembly:PostBuildCleanup(typeof(HeadlessPlayModeSetup))]
  42. namespace Tests
  43. {
  44. public class HeadlessPlayModeSetup : ITestPlayerBuildModifier, IPostBuildCleanup
  45. {
  46. private static bool s_RunningPlayerTests;
  47. public BuildPlayerOptions ModifyOptions(BuildPlayerOptions playerOptions)
  48. {
  49. // Do not launch the player after the build completes.
  50. playerOptions.options &= ~BuildOptions.AutoRunPlayer;
  51. // Set the headlessBuildLocation to the output directory you desire. It does not need to be inside the project.
  52. var headlessBuildLocation = Path.GetFullPath(Path.Combine(Application.dataPath, ".//..//PlayModeTestPlayer"));
  53. var fileName = Path.GetFileName(playerOptions.locationPathName);
  54. if (!string.IsNullOrEmpty(fileName))
  55. {
  56. headlessBuildLocation = Path.Combine(headlessBuildLocation, fileName);
  57. }
  58. playerOptions.locationPathName = headlessBuildLocation;
  59. // Instruct the cleanup to exit the Editor if the run came from the command line.
  60. // The variable is static because the cleanup is being invoked in a new instance of the class.
  61. s_RunningPlayerTests = true;
  62. return playerOptions;
  63. }
  64. public void Cleanup()
  65. {
  66. if (s_RunningPlayerTests && IsRunningTestsFromCommandLine())
  67. {
  68. // Exit the Editor on the next update, allowing for other PostBuildCleanup steps to run.
  69. EditorApplication.update += () => { EditorApplication.Exit(0); };
  70. }
  71. }
  72. private static bool IsRunningTestsFromCommandLine()
  73. {
  74. var commandLineArgs = Environment.GetCommandLineArgs();
  75. return commandLineArgs.Any(value => value == "-runTests");
  76. }
  77. }
  78. }
  79. ```
  80. 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.
  81. 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.