PlayerTestAssemblyProvider.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using NUnit.Framework.Interfaces;
  5. using UnityEngine.TestTools.NUnitExtensions;
  6. namespace UnityEngine.TestTools.Utils
  7. {
  8. internal class PlayerTestAssemblyProvider
  9. {
  10. private IAssemblyLoadProxy m_AssemblyLoadProxy;
  11. private readonly List<string> m_AssembliesToLoad;
  12. //Cached until domain reload
  13. private static List<IAssemblyWrapper> m_LoadedAssemblies;
  14. internal PlayerTestAssemblyProvider(IAssemblyLoadProxy assemblyLoadProxy, List<string> assembliesToLoad)
  15. {
  16. m_AssemblyLoadProxy = assemblyLoadProxy;
  17. m_AssembliesToLoad = assembliesToLoad;
  18. LoadAssemblies();
  19. }
  20. public ITest GetTestsWithNUnit()
  21. {
  22. return BuildTests(TestPlatform.PlayMode, m_LoadedAssemblies.ToArray());
  23. }
  24. public List<IAssemblyWrapper> GetUserAssemblies()
  25. {
  26. return m_LoadedAssemblies;
  27. }
  28. protected static ITest BuildTests(TestPlatform testPlatform, IAssemblyWrapper[] assemblies)
  29. {
  30. var settings = UnityTestAssemblyBuilder.GetNUnitTestBuilderSettings(testPlatform);
  31. var builder = new UnityTestAssemblyBuilder();
  32. return builder.Build(assemblies.Select(a => a.Assembly).ToArray(), Enumerable.Repeat(testPlatform, assemblies.Length).ToArray(), settings);
  33. }
  34. private void LoadAssemblies()
  35. {
  36. if (m_LoadedAssemblies != null)
  37. {
  38. return;
  39. }
  40. m_LoadedAssemblies = new List<IAssemblyWrapper>();
  41. foreach (var userAssembly in m_AssembliesToLoad)
  42. {
  43. IAssemblyWrapper a;
  44. try
  45. {
  46. a = m_AssemblyLoadProxy.Load(userAssembly);
  47. }
  48. catch (FileNotFoundException)
  49. {
  50. continue;
  51. }
  52. if (a != null)
  53. m_LoadedAssemblies.Add(a);
  54. }
  55. }
  56. }
  57. }