RequirePlatformSupportAttribute.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. using System.Linq;
  3. using NUnit.Framework;
  4. using NUnit.Framework.Interfaces;
  5. using NUnit.Framework.Internal;
  6. namespace UnityEditor.TestTools
  7. {
  8. [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
  9. public class RequirePlatformSupportAttribute : NUnitAttribute, IApplyToTest
  10. {
  11. public RequirePlatformSupportAttribute(params BuildTarget[] platforms)
  12. {
  13. this.platforms = platforms;
  14. }
  15. public BuildTarget[] platforms { get; private set; }
  16. void IApplyToTest.ApplyToTest(Test test)
  17. {
  18. test.Properties.Add(PropertyNames.Category, string.Format("RequirePlatformSupport({0})", string.Join(", ", platforms.Select(p => p.ToString()).OrderBy(p => p).ToArray())));
  19. if (!platforms.All(p => BuildPipeline.IsBuildTargetSupported(BuildTargetGroup.Unknown, p)))
  20. {
  21. var missingPlatforms = platforms.Where(p => !BuildPipeline.IsBuildTargetSupported(BuildTargetGroup.Unknown, p)).Select(p => p.ToString()).ToArray();
  22. string skipReason = "Test cannot be run as it requires support for the following platforms to be installed: " + string.Join(", ", missingPlatforms);
  23. test.RunState = RunState.Skipped;
  24. test.Properties.Add(PropertyNames.SkipReason, skipReason);
  25. }
  26. }
  27. }
  28. }