AvaloniaTestFrameworkExecutor.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Xunit.Internal;
  7. using Xunit.Sdk;
  8. using Xunit.v3;
  9. namespace Avalonia.IntegrationTests.Win32.Infrastructure;
  10. internal sealed class AvaloniaTestFrameworkExecutor(IXunitTestAssembly testAssembly)
  11. : ITestFrameworkExecutor
  12. {
  13. public async ValueTask RunTestCases(
  14. IReadOnlyCollection<ITestCase> testCases,
  15. IMessageSink executionMessageSink,
  16. ITestFrameworkExecutionOptions executionOptions,
  17. CancellationToken? cancellationToken = null)
  18. {
  19. var seed = executionOptions.Seed() ?? testAssembly.ModuleVersionID.GetHashCode();
  20. Randomizer.Seed = seed == int.MinValue ? int.MaxValue : Math.Abs(seed);
  21. var executor = new XunitTestFrameworkExecutor(testAssembly);
  22. var dispatcher = await AppManager.EnsureAppInitializedAsync().ConfigureAwait(false);
  23. try
  24. {
  25. await dispatcher
  26. .InvokeAsync(async () =>
  27. {
  28. using (new PreserveWorkingFolder(testAssembly))
  29. using (new InvariantCultureScope())
  30. {
  31. await executor
  32. .RunTestCases(
  33. testCases.Cast<IXunitTestCase>().ToArray(),
  34. executionMessageSink,
  35. executionOptions,
  36. cancellationToken.GetValueOrDefault())
  37. .ConfigureAwait(false);
  38. }
  39. })
  40. .ConfigureAwait(false);
  41. }
  42. finally
  43. {
  44. AppManager.Stop();
  45. }
  46. }
  47. }