diff --git a/AGENTS.md b/AGENTS.md index 94564c9..07bb8ed 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -21,7 +21,10 @@ It adds only the Revit execution model on top of TUnit; TUnit provides the asser * The Revit thread is one process-wide background STA thread running a WPF `Dispatcher`. The dispatcher pumps the Win32 messages COM marshaling needs and routes `await` continuations back to the thread through `DispatcherSynchronizationContext`. * `RevitThreadExecutor` unwraps the dispatcher operation (`operation.Task.Unwrap()`). The returned task completes only after the body and its `await` continuations finish. * `RevitThreadExecutor` declares an `IParallelLimit` of `1` through `ITestRegisteredEventReceiver`, and TUnit applies it to every test the executor runs. The limit belongs to the executor; never reintroduce it as an attribute on the base class. -* `RevitUiThreadExecutor` declares `RevitUiParallelLimit`. TUnit groups tests by the type of their limit, and UI tests run one at a time in parallel with the tests of `RevitThreadExecutor`. A UI test outside Revit awaits its result without the Revit thread. +* UI tests in the test host use one shared `NotInParallel` key, never a parallel limit. A test that waits for a limit occupies a parallel slot. TUnit runs the tests of a limit in no class order. A UI test outside Revit awaits its result without the Revit thread. +* A UI test has one `NotInParallel` constraint: TUnit reads only the first `NotInParallel` constraint of a test. The UI key is added to the keys the test declares. +* Inside Revit, the `Before(Class)` hooks of a UI test class run only after the test host executes the first test of the class. The timeout of a `Before(Class)` hook excludes that wait. +* A base class declares its hook executor with the non-generic `HookExecutor` attribute on .NET Framework: TUnit ignores a generic hook executor attribute of a class there. * The application connection opens in the `RevitSessionSetup` hook, a `Before(TestSession)` hook, and never lazily in the executor. TUnit creates test instances on pool threads, and a class that references a Revit API type loads that type off the Revit thread. An injection running at the same time fails with `Attempted to write protected memory`. TUnit applies no executor of the class to a test session hook, and the hook declares `[HookExecutor]` itself. `RevitSession.EnsureStarted` refuses a call off the Revit thread, and Revit activates only on an STA thread. * The test host is the only scheduler of UI tests. Revit runs a UI test only on the request of the test host, and inside Revit no UI test keeps a parallel limit or a parallel constraint; two schedulers that order the same tests deadlock. * A UI test waits for Revit before TUnit starts its timer. The duration of a UI test covers its hooks and body inside Revit, never the start of Revit. diff --git a/CHANGELOG.md b/CHANGELOG.md index 71b8538..c4f6ad9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,22 @@ using Nice3point.Revit.Injector.Attributes; `Application.ActiveAddInId` in `RevitApiTest` and `UiApplication.ActiveAddInId` in `RevitApiUiTest` return the declared add-in. Each property is optional: `Id` defaults to a random GUID, `Name` to the ID in uppercase, and `VendorId` to `Autodesk`. +## UI test order + +The UI tests of a class run one after another https://github.com/Nice3point/RevitUnit/issues/132: + +- `[Before(Class)]` of a `RevitApiUiTest` class runs right before the first test of the class. +- `[After(Class)]` runs right after the last test of the class. + A document that a class opens in `[Before(Class)]` and closes in `[After(Class)]` is open only during the tests of that class. +- The timeout of a `[Before(Class)]` hook excludes the wait for the tests of other classes. +- The duration of the first test of a class includes its `[Before(Class)]` hooks. +- A UI test that waits for its turn occupies no parallel slot. + +## Bug fixes + +- Fixed `[Before(Class)]` and `[After(Class)]` hooks of `RevitApiTest` and `RevitApiUiTest` classes running outside the Revit thread on Revit 2021 to 2024. +- Fixed the test host crash when `--fail-fast` stops a run with UI tests: the package depends on TUnit 1.76.45. + # 2027.0.7 - `RevitApiTest` waits for a timed-out or cancelled test body to finish before running cleanup hooks, starting another test, or closing the Revit connection. diff --git a/Directory.Packages.props b/Directory.Packages.props index 6e34937..093acfb 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -50,7 +50,7 @@ - + diff --git a/source/Nice3point.TUnit.Revit/Executors/RevitUiThreadExecutor.cs b/source/Nice3point.TUnit.Revit/Executors/RevitUiThreadExecutor.cs index bbeef79..130e9a2 100644 --- a/source/Nice3point.TUnit.Revit/Executors/RevitUiThreadExecutor.cs +++ b/source/Nice3point.TUnit.Revit/Executors/RevitUiThreadExecutor.cs @@ -1,4 +1,3 @@ -using Nice3point.TUnit.Revit.Limiters; using Nice3point.TUnit.Revit.Sessions; using Nice3point.TUnit.Revit.Ui; using TUnit.Core.Interfaces; @@ -15,6 +14,11 @@ namespace Nice3point.TUnit.Revit.Executors; /// public sealed class RevitUiThreadExecutor : ITestExecutor, IHookExecutor, ITestRegisteredEventReceiver { + /// + /// The NotInParallel key of every UI test in the test host. + /// + private const string UiConstraintKey = "Nice3point.TUnit.Revit.Ui"; + /// public int Order => 0; @@ -100,12 +104,47 @@ public ValueTask OnTestRegistered(TestRegisteredContext context) } RevitUiSession.Get(context.TestContext).Register(context.TestDetails.TestId); - context.SetParallelLimiter(RevitUiParallelLimit.Default); + AddUiConstraint(context); context.TestDetails.Timeout = Timeout.InfiniteTimeSpan; context.TestDetails.RetryLimit = 0; return default; } + /// + /// Adds the to the of the specified test. + /// + /// The registration context of the test. + /// + /// TUnit reads only the first of a test. + /// The method adds the key to the keys of a constraint the test declares. + /// The method does not change a constraint without keys: TUnit runs such a test alone. + /// + private static void AddUiConstraint(TestRegisteredContext context) + { + var parallelism = context.TestContext.Parallelism; + var constraints = parallelism.Constraints.ToArray(); + var notInParallel = constraints.OfType().FirstOrDefault(); + if (notInParallel is null) + { + parallelism.AddConstraint(new NotInParallelConstraint([UiConstraintKey])); + return; + } + + var keys = notInParallel.NotInParallelConstraintKeys; + if (keys.Count == 0 || keys.Contains(UiConstraintKey)) + { + return; + } + + context.ClearParallelConstraints(); + foreach (var constraint in constraints) + { + parallelism.AddConstraint(ReferenceEquals(constraint, notInParallel) + ? notInParallel with { NotInParallelConstraintKeys = [.. keys, UiConstraintKey] } + : constraint); + } + } + private static ValueTask InvokeAsync(Func action) { return RevitUiRuntime.Application?.InvokeAsync(action) ?? default; diff --git a/source/Nice3point.TUnit.Revit/RevitApiTest.cs b/source/Nice3point.TUnit.Revit/RevitApiTest.cs index 21161fa..aca3569 100644 --- a/source/Nice3point.TUnit.Revit/RevitApiTest.cs +++ b/source/Nice3point.TUnit.Revit/RevitApiTest.cs @@ -15,7 +15,12 @@ namespace Nice3point.TUnit.Revit; /// Test discovery executes no test and does not start Revit. /// [TestExecutor] +#if NET [HookExecutor] +#else +// On .NET Framework, TUnit ignores a generic hook executor attribute of a class. +[HookExecutor(typeof(RevitThreadExecutor))] +#endif public abstract class RevitApiTest { /// diff --git a/source/Nice3point.TUnit.Revit/RevitApiUiTest.cs b/source/Nice3point.TUnit.Revit/RevitApiUiTest.cs index f9c152c..23ba4d7 100644 --- a/source/Nice3point.TUnit.Revit/RevitApiUiTest.cs +++ b/source/Nice3point.TUnit.Revit/RevitApiUiTest.cs @@ -22,7 +22,12 @@ namespace Nice3point.TUnit.Revit; /// [Category(RevitUiRuntime.Category)] [TestExecutor] +#if NET [HookExecutor] +#else +// On .NET Framework, TUnit ignores a generic hook executor attribute of a class. +[HookExecutor(typeof(RevitUiThreadExecutor))] +#endif public abstract class RevitApiUiTest : ITestStartEventReceiver, ITestEndEventReceiver { /// diff --git a/source/Nice3point.TUnit.Revit/Sessions/RevitUiSessionHooks.cs b/source/Nice3point.TUnit.Revit/Sessions/RevitUiSessionHooks.cs index 33d6910..d8a6b8e 100644 --- a/source/Nice3point.TUnit.Revit/Sessions/RevitUiSessionHooks.cs +++ b/source/Nice3point.TUnit.Revit/Sessions/RevitUiSessionHooks.cs @@ -3,7 +3,7 @@ namespace Nice3point.TUnit.Revit.Sessions; /// -/// Provides the test session hooks of the UI tests. +/// Provides the test session and class hooks of the UI tests. /// /// /// The hooks run without the Revit thread, in the test host process and in the Revit user interface application alike. @@ -19,6 +19,29 @@ public static void RevitUiSessionSetup() RevitUiRuntime.Application?.ReportUnregisteredTests(); } + /// + /// Waits inside Revit until the test host executes the first test of a UI test class. + /// + /// The context of the class. + /// A used to cancel the wait. + /// A task that represents the asynchronous wait operation. + /// + /// TUnit runs the hook before the [Before(Class)] hooks of the class. + /// The timeout of a [Before(Class)] hook excludes the wait. + /// The hook returns at once in the test host process and for a class not derived from . + /// + [Timeout(int.MaxValue)] + [BeforeEvery(Class, Order = int.MinValue)] + public static Task RevitUiClassSetup(ClassHookContext context, CancellationToken cancellationToken) + { + if (RevitUiRuntime.Application is not { } runtime || !typeof(RevitApiUiTest).IsAssignableFrom(context.ClassType)) + { + return Task.CompletedTask; + } + + return runtime.WaitForAnyExecutionAsync(context.Tests.Select(static test => test.Metadata.TestDetails.TestId), cancellationToken); + } + /// /// Closes the Revit user interface session of the finished test session. /// diff --git a/source/Nice3point.TUnit.Revit/Ui/RevitUiApplicationRuntime.cs b/source/Nice3point.TUnit.Revit/Ui/RevitUiApplicationRuntime.cs index 8ee178f..9d52227 100644 --- a/source/Nice3point.TUnit.Revit/Ui/RevitUiApplicationRuntime.cs +++ b/source/Nice3point.TUnit.Revit/Ui/RevitUiApplicationRuntime.cs @@ -85,6 +85,32 @@ public async Task WaitForExecutionAsync(string testId) } } + /// + /// Waits for the test host to execute any of the specified tests. + /// + /// The identifiers of the tests. + /// A used to cancel the wait. + /// A task that represents the asynchronous wait operation. + /// The test host skipped the pending tests. + /// + /// The call sends for every test that is not pending yet. + /// A call without tests completes at once. + /// + public async Task WaitForAnyExecutionAsync(IEnumerable testIds, CancellationToken cancellationToken) + { + var executions = testIds.Select(testId => GetExecution(testId).Task).ToArray(); + if (executions.Length == 0) + { + return; + } + + var execution = await Task.WhenAny(executions).WaitAsync(cancellationToken).ConfigureAwait(false); + if (!await execution.ConfigureAwait(false)) + { + throw new SkipTestException("The test host skipped the pending tests."); + } + } + /// /// Runs the specified test body or hook on the Revit thread inside a Revit API context. /// diff --git a/tests/Nice3point.TUnit.Revit.Tests/Abstractions/ClassLifetimeUiTest.cs b/tests/Nice3point.TUnit.Revit.Tests/Abstractions/ClassLifetimeUiTest.cs new file mode 100644 index 0000000..a5d3166 --- /dev/null +++ b/tests/Nice3point.TUnit.Revit.Tests/Abstractions/ClassLifetimeUiTest.cs @@ -0,0 +1,52 @@ +namespace Nice3point.TUnit.Revit.Tests.Abstractions; + +public abstract class ClassLifetimeUiTest : RevitApiUiTest +{ + private static readonly Lock StateLock = new(); + private static readonly Dictionary ClassesOpenAtSetup = []; + private static Type? _openClass; + + protected static Type? OpenClass + { + get + { + lock (StateLock) + { + return _openClass; + } + } + } + + protected Type? ClassOpenAtSetup + { + get + { + lock (StateLock) + { + return ClassesOpenAtSetup[GetType()]; + } + } + } + + [Before(Class)] + public static void OpenTestClass(ClassHookContext context) + { + lock (StateLock) + { + ClassesOpenAtSetup[context.ClassType] = _openClass; + _openClass = context.ClassType; + } + } + + [After(Class)] + public static void CloseTestClass(ClassHookContext context) + { + lock (StateLock) + { + if (_openClass == context.ClassType) + { + _openClass = null; + } + } + } +} diff --git a/tests/Nice3point.TUnit.Revit.Tests/ClassHookTests.cs b/tests/Nice3point.TUnit.Revit.Tests/ClassHookTests.cs new file mode 100644 index 0000000..03b7f0b --- /dev/null +++ b/tests/Nice3point.TUnit.Revit.Tests/ClassHookTests.cs @@ -0,0 +1,22 @@ +namespace Nice3point.TUnit.Revit.Tests; + +public sealed class ClassHookTests : RevitApiTest +{ + private static int _beforeClassThreadId; + + [Before(Class)] + public static void RecordBeforeClassThread() + { + _beforeClassThreadId = Environment.CurrentManagedThreadId; + } + + [Test] + public async Task BeforeClassHook_DerivedClass_RunsOnTheRevitThread() + { + // Arrange & Act + var threadId = Environment.CurrentManagedThreadId; + + // Assert + await Assert.That(_beforeClassThreadId).IsEqualTo(threadId); + } +} diff --git a/tests/Nice3point.TUnit.Revit.Tests/ClassHookUiTests.cs b/tests/Nice3point.TUnit.Revit.Tests/ClassHookUiTests.cs new file mode 100644 index 0000000..991d6e7 --- /dev/null +++ b/tests/Nice3point.TUnit.Revit.Tests/ClassHookUiTests.cs @@ -0,0 +1,22 @@ +namespace Nice3point.TUnit.Revit.Tests; + +public sealed class ClassHookUiTests : RevitApiUiTest +{ + private static int _beforeClassThreadId; + + [Before(Class)] + public static void RecordBeforeClassThread() + { + _beforeClassThreadId = Environment.CurrentManagedThreadId; + } + + [Test] + public async Task BeforeClassHook_DerivedClass_RunsOnTheRevitThread() + { + // Arrange & Act + var threadId = Environment.CurrentManagedThreadId; + + // Assert + await Assert.That(_beforeClassThreadId).IsEqualTo(threadId); + } +} diff --git a/tests/Nice3point.TUnit.Revit.Tests/FirstClassLifetimeUiTests.cs b/tests/Nice3point.TUnit.Revit.Tests/FirstClassLifetimeUiTests.cs new file mode 100644 index 0000000..ef82819 --- /dev/null +++ b/tests/Nice3point.TUnit.Revit.Tests/FirstClassLifetimeUiTests.cs @@ -0,0 +1,22 @@ +using Nice3point.TUnit.Revit.Tests.Abstractions; + +namespace Nice3point.TUnit.Revit.Tests; + +public sealed class FirstClassLifetimeUiTests : ClassLifetimeUiTest +{ + [Test] + [Repeat(2)] + public async Task ClassHooks_AnotherUiClass_DoNotOverlap() + { + // Arrange & Act + var openClass = OpenClass; + var classOpenAtSetup = ClassOpenAtSetup; + + // Assert + using (Assert.Multiple()) + { + await Assert.That(classOpenAtSetup).IsNull(); + await Assert.That(openClass).IsEqualTo(typeof(FirstClassLifetimeUiTests)); + } + } +} diff --git a/tests/Nice3point.TUnit.Revit.Tests/SecondClassLifetimeUiTests.cs b/tests/Nice3point.TUnit.Revit.Tests/SecondClassLifetimeUiTests.cs new file mode 100644 index 0000000..1f846ae --- /dev/null +++ b/tests/Nice3point.TUnit.Revit.Tests/SecondClassLifetimeUiTests.cs @@ -0,0 +1,22 @@ +using Nice3point.TUnit.Revit.Tests.Abstractions; + +namespace Nice3point.TUnit.Revit.Tests; + +public sealed class SecondClassLifetimeUiTests : ClassLifetimeUiTest +{ + [Test] + [Repeat(2)] + public async Task ClassHooks_AnotherUiClass_DoNotOverlap() + { + // Arrange & Act + var openClass = OpenClass; + var classOpenAtSetup = ClassOpenAtSetup; + + // Assert + using (Assert.Multiple()) + { + await Assert.That(classOpenAtSetup).IsNull(); + await Assert.That(openClass).IsEqualTo(typeof(SecondClassLifetimeUiTests)); + } + } +}