From bae6fe334b4d76bdb442c0d819c3e8e5fe092f2b Mon Sep 17 00:00:00 2001 From: Nice3point Date: Thu, 29 Oct 2026 14:41:57 +0300 Subject: [PATCH 1/7] [Build] Update TUnit to 1.76.45 --- Directory.Packages.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 @@ - + From bb82f3943371193bcaf3ec828b42e84cdbbaf9b2 Mon Sep 17 00:00:00 2001 From: Nice3point Date: Thu, 29 Oct 2026 14:44:03 +0300 Subject: [PATCH 2/7] [Executors] Run the class hooks of a test class on the Revit thread on .NET Framework --- CHANGELOG.md | 4 ++++ source/Nice3point.TUnit.Revit/RevitApiTest.cs | 5 +++++ .../Nice3point.TUnit.Revit/RevitApiUiTest.cs | 5 +++++ .../ClassHookTests.cs | 22 +++++++++++++++++++ .../ClassHookUiTests.cs | 22 +++++++++++++++++++ 5 files changed, 58 insertions(+) create mode 100644 tests/Nice3point.TUnit.Revit.Tests/ClassHookTests.cs create mode 100644 tests/Nice3point.TUnit.Revit.Tests/ClassHookUiTests.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 71b8538..a06a204 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,10 @@ 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`. +## Bug fixes + +- Fixed `[Before(Class)]` and `[After(Class)]` hooks of `RevitApiTest` and `RevitApiUiTest` classes running outside the Revit thread on Revit 2021 to 2024. + # 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/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/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); + } +} From 3d8ad78440931ee9d1f9b5f867221383b91cf7bb Mon Sep 17 00:00:00 2001 From: Nice3point Date: Thu, 29 Oct 2026 14:59:44 +0300 Subject: [PATCH 3/7] [Executors] Run the UI tests of a class in a row between its class hooks --- AGENTS.md | 4 +- CHANGELOG.md | 11 +++++ README.md | 1 + .../Executors/RevitUiThreadExecutor.cs | 34 ++++++++++++-- .../Sessions/RevitUiSessionHooks.cs | 24 +++++++++- .../Ui/RevitUiApplicationRuntime.cs | 26 ++++++++++ .../Abstractions/ClassLifetimeUiTest.cs | 47 +++++++++++++++++++ .../FirstClassLifetimeUiTests.cs | 13 +++++ .../SecondClassLifetimeUiTests.cs | 13 +++++ 9 files changed, 168 insertions(+), 5 deletions(-) create mode 100644 tests/Nice3point.TUnit.Revit.Tests/Abstractions/ClassLifetimeUiTest.cs create mode 100644 tests/Nice3point.TUnit.Revit.Tests/FirstClassLifetimeUiTests.cs create mode 100644 tests/Nice3point.TUnit.Revit.Tests/SecondClassLifetimeUiTests.cs diff --git a/AGENTS.md b/AGENTS.md index 94564c9..ed2f9c3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -21,7 +21,9 @@ 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. +* In the test host, `RevitUiThreadExecutor` gives every UI test a `NotInParallel` constraint with one shared key and joins that key to the keys of an existing constraint; TUnit reads only the first `NotInParallel` constraint of a test. TUnit runs keyed tests one at a time, class by class, in parallel with the tests of `RevitThreadExecutor`, and a keyed test takes a parallel slot only once it holds its key. Never reintroduce a parallel limit for UI tests: a test waiting for a limit holds a slot, and the order of a limit interleaves classes. A UI test outside Revit awaits its result without the Revit thread. +* Inside Revit, the `RevitUiClassSetup` hook, a `BeforeEvery(Class)` hook with the lowest order and no timeout, waits until the test host executes the first test of a UI test class. The `Before(Class)` hooks of the class run after it with their own timeouts. +* On .NET Framework, TUnit ignores a generic hook executor attribute of a class, and the base classes declare `[HookExecutor(typeof(...))]` 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 a06a204..7472157 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,9 +13,20 @@ 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, and `[After(Class)]` right after the last. + The documents a class opens in its hooks stay open only while the tests of that class run. +- The timeout of a `[Before(Class)]` hook covers only the hook. +- The duration of the first test of a class includes its `[Before(Class)]` hooks. +- UI tests waiting for their turn leave the parallel slots to other tests. + ## 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. TUnit is updated to 1.76.45. # 2027.0.7 diff --git a/README.md b/README.md index fb845c1..7c53d08 100644 --- a/README.md +++ b/README.md @@ -226,6 +226,7 @@ public sealed class UiDocumentTests : RevitApiUiTest ``` UI tests run one at a time, in parallel with `RevitApiTest` tests. +The UI tests of a class run one after another: `[Before(Class)]` runs right before the first of them, and `[After(Class)]` right after the last. To run them in one queue with `RevitApiTest` tests, mark the class with `[ParallelLimiter]`: ```csharp diff --git a/source/Nice3point.TUnit.Revit/Executors/RevitUiThreadExecutor.cs b/source/Nice3point.TUnit.Revit/Executors/RevitUiThreadExecutor.cs index bbeef79..e24f72b 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; @@ -10,7 +9,7 @@ namespace Nice3point.TUnit.Revit.Executors; /// /// /// A test body and its hooks run inside a Revit API context, and their await continuations stay on the same thread. -/// The executor runs one Revit UI test at a time. +/// The executor runs one Revit UI test at a time, and the test host runs the UI tests class by class. /// The executor supports only tests of a class derived from . /// public sealed class RevitUiThreadExecutor : ITestExecutor, IHookExecutor, ITestRegisteredEventReceiver @@ -100,12 +99,41 @@ 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; } + // TUnit reads only the first NotInParallel constraint of a test, and the UI key joins the keys of an existing one. + private static void AddUiConstraint(TestRegisteredContext context) + { + const string uiConstraintKey = "Nice3point.TUnit.Revit.Ui"; + + 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/Sessions/RevitUiSessionHooks.cs b/source/Nice3point.TUnit.Revit/Sessions/RevitUiSessionHooks.cs index 33d6910..64ddd8e 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,28 @@ 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, and 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 . + /// + [BeforeEvery(Class, Order = int.MinValue)] + [Timeout(int.MaxValue)] + 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..d1fa00d --- /dev/null +++ b/tests/Nice3point.TUnit.Revit.Tests/Abstractions/ClassLifetimeUiTest.cs @@ -0,0 +1,47 @@ +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; + + [Before(Class)] + public static void OpenClass(ClassHookContext context) + { + lock (StateLock) + { + ClassesOpenAtSetup[context.ClassType] = _openClass; + _openClass = context.ClassType; + } + } + + [After(Class)] + public static void CloseClass(ClassHookContext context) + { + lock (StateLock) + { + if (_openClass == context.ClassType) + { + _openClass = null; + } + } + } + + protected async Task AssertClassRunsAloneAsync() + { + Type? openClass; + Type? classOpenAtSetup; + lock (StateLock) + { + openClass = _openClass; + classOpenAtSetup = ClassesOpenAtSetup[GetType()]; + } + + using (Assert.Multiple()) + { + await Assert.That(classOpenAtSetup).IsNull(); + await Assert.That(openClass).IsEqualTo(GetType()); + } + } +} diff --git a/tests/Nice3point.TUnit.Revit.Tests/FirstClassLifetimeUiTests.cs b/tests/Nice3point.TUnit.Revit.Tests/FirstClassLifetimeUiTests.cs new file mode 100644 index 0000000..d68b454 --- /dev/null +++ b/tests/Nice3point.TUnit.Revit.Tests/FirstClassLifetimeUiTests.cs @@ -0,0 +1,13 @@ +using Nice3point.TUnit.Revit.Tests.Abstractions; + +namespace Nice3point.TUnit.Revit.Tests; + +public sealed class FirstClassLifetimeUiTests : ClassLifetimeUiTest +{ + [Test] + [Repeat(2)] + public Task ClassHooks_AnotherUiClass_DoNotOverlap() + { + return AssertClassRunsAloneAsync(); + } +} diff --git a/tests/Nice3point.TUnit.Revit.Tests/SecondClassLifetimeUiTests.cs b/tests/Nice3point.TUnit.Revit.Tests/SecondClassLifetimeUiTests.cs new file mode 100644 index 0000000..abefb33 --- /dev/null +++ b/tests/Nice3point.TUnit.Revit.Tests/SecondClassLifetimeUiTests.cs @@ -0,0 +1,13 @@ +using Nice3point.TUnit.Revit.Tests.Abstractions; + +namespace Nice3point.TUnit.Revit.Tests; + +public sealed class SecondClassLifetimeUiTests : ClassLifetimeUiTest +{ + [Test] + [Repeat(2)] + public Task ClassHooks_AnotherUiClass_DoNotOverlap() + { + return AssertClassRunsAloneAsync(); + } +} From 518372854645890cf02adbed4e9d702367cdd0ae Mon Sep 17 00:00:00 2001 From: Nice3point Date: Thu, 29 Oct 2026 17:58:43 +0300 Subject: [PATCH 4/7] [Executors] Document the shared UI key and state the UI test order as rules --- AGENTS.md | 6 +-- CHANGELOG.md | 4 +- .../Executors/RevitUiThreadExecutor.cs | 22 +++++++--- .../Abstractions/ClassLifetimeUiTest.cs | 41 +++++++++++-------- .../FirstClassLifetimeUiTests.cs | 13 +++++- .../SecondClassLifetimeUiTests.cs | 13 +++++- 6 files changed, 66 insertions(+), 33 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index ed2f9c3..06e0f54 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -21,9 +21,9 @@ 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. -* In the test host, `RevitUiThreadExecutor` gives every UI test a `NotInParallel` constraint with one shared key and joins that key to the keys of an existing constraint; TUnit reads only the first `NotInParallel` constraint of a test. TUnit runs keyed tests one at a time, class by class, in parallel with the tests of `RevitThreadExecutor`, and a keyed test takes a parallel slot only once it holds its key. Never reintroduce a parallel limit for UI tests: a test waiting for a limit holds a slot, and the order of a limit interleaves classes. A UI test outside Revit awaits its result without the Revit thread. -* Inside Revit, the `RevitUiClassSetup` hook, a `BeforeEvery(Class)` hook with the lowest order and no timeout, waits until the test host executes the first test of a UI test class. The `Before(Class)` hooks of the class run after it with their own timeouts. -* On .NET Framework, TUnit ignores a generic hook executor attribute of a class, and the base classes declare `[HookExecutor(typeof(...))]` there. +* UI tests in the test host run under one shared `NotInParallel` key, never under a parallel limit. A test waiting for a limit holds a parallel slot, and the tests of a limit interleave their classes. The UI key joins the keys a test declares in one constraint: TUnit reads only the first `NotInParallel` constraint of a test. A UI test outside Revit awaits its result without the Revit thread. +* Inside Revit, a UI test class starts only when the test host executes its first test. The wait runs before every `Before(Class)` hook of the class and counts towards none of their timeouts. +* 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 7472157..197d7e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,12 +21,12 @@ The UI tests of a class run one after another https://github.com/Nice3point/Revi The documents a class opens in its hooks stay open only while the tests of that class run. - The timeout of a `[Before(Class)]` hook covers only the hook. - The duration of the first test of a class includes its `[Before(Class)]` hooks. -- UI tests waiting for their turn leave the parallel slots to other tests. +- A UI test waiting for its turn holds no parallel slot, and `RevitApiTest` tests run meanwhile. ## 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. TUnit is updated to 1.76.45. +- 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 diff --git a/source/Nice3point.TUnit.Revit/Executors/RevitUiThreadExecutor.cs b/source/Nice3point.TUnit.Revit/Executors/RevitUiThreadExecutor.cs index e24f72b..7a27e3f 100644 --- a/source/Nice3point.TUnit.Revit/Executors/RevitUiThreadExecutor.cs +++ b/source/Nice3point.TUnit.Revit/Executors/RevitUiThreadExecutor.cs @@ -14,6 +14,11 @@ namespace Nice3point.TUnit.Revit.Executors; /// public sealed class RevitUiThreadExecutor : ITestExecutor, IHookExecutor, ITestRegisteredEventReceiver { + /// + /// The NotInParallel key every UI test in the test host shares. + /// + private const string UiConstraintKey = "Nice3point.TUnit.Revit.Ui"; + /// public int Order => 0; @@ -105,22 +110,27 @@ public ValueTask OnTestRegistered(TestRegisteredContext context) return default; } - // TUnit reads only the first NotInParallel constraint of a test, and the UI key joins the keys of an existing one. + /// + /// Adds the to the of the specified test. + /// + /// The registration context of the test. + /// + /// TUnit reads only the first of a test, and the key joins the keys of a constraint the test declares. + /// A constraint without keys runs the test alone, and the method leaves it unchanged. + /// private static void AddUiConstraint(TestRegisteredContext context) { - const string uiConstraintKey = "Nice3point.TUnit.Revit.Ui"; - var parallelism = context.TestContext.Parallelism; var constraints = parallelism.Constraints.ToArray(); var notInParallel = constraints.OfType().FirstOrDefault(); if (notInParallel is null) { - parallelism.AddConstraint(new NotInParallelConstraint([uiConstraintKey])); + parallelism.AddConstraint(new NotInParallelConstraint([UiConstraintKey])); return; } var keys = notInParallel.NotInParallelConstraintKeys; - if (keys.Count == 0 || keys.Contains(uiConstraintKey)) + if (keys.Count == 0 || keys.Contains(UiConstraintKey)) { return; } @@ -129,7 +139,7 @@ private static void AddUiConstraint(TestRegisteredContext context) foreach (var constraint in constraints) { parallelism.AddConstraint(ReferenceEquals(constraint, notInParallel) - ? notInParallel with { NotInParallelConstraintKeys = [.. keys, uiConstraintKey] } + ? notInParallel with { NotInParallelConstraintKeys = [.. keys, UiConstraintKey] } : constraint); } } diff --git a/tests/Nice3point.TUnit.Revit.Tests/Abstractions/ClassLifetimeUiTest.cs b/tests/Nice3point.TUnit.Revit.Tests/Abstractions/ClassLifetimeUiTest.cs index d1fa00d..a5d3166 100644 --- a/tests/Nice3point.TUnit.Revit.Tests/Abstractions/ClassLifetimeUiTest.cs +++ b/tests/Nice3point.TUnit.Revit.Tests/Abstractions/ClassLifetimeUiTest.cs @@ -6,42 +6,47 @@ public abstract class ClassLifetimeUiTest : RevitApiUiTest private static readonly Dictionary ClassesOpenAtSetup = []; private static Type? _openClass; - [Before(Class)] - public static void OpenClass(ClassHookContext context) + protected static Type? OpenClass { - lock (StateLock) + get { - ClassesOpenAtSetup[context.ClassType] = _openClass; - _openClass = context.ClassType; + lock (StateLock) + { + return _openClass; + } } } - [After(Class)] - public static void CloseClass(ClassHookContext context) + protected Type? ClassOpenAtSetup { - lock (StateLock) + get { - if (_openClass == context.ClassType) + lock (StateLock) { - _openClass = null; + return ClassesOpenAtSetup[GetType()]; } } } - protected async Task AssertClassRunsAloneAsync() + [Before(Class)] + public static void OpenTestClass(ClassHookContext context) { - Type? openClass; - Type? classOpenAtSetup; lock (StateLock) { - openClass = _openClass; - classOpenAtSetup = ClassesOpenAtSetup[GetType()]; + ClassesOpenAtSetup[context.ClassType] = _openClass; + _openClass = context.ClassType; } + } - using (Assert.Multiple()) + [After(Class)] + public static void CloseTestClass(ClassHookContext context) + { + lock (StateLock) { - await Assert.That(classOpenAtSetup).IsNull(); - await Assert.That(openClass).IsEqualTo(GetType()); + if (_openClass == context.ClassType) + { + _openClass = null; + } } } } diff --git a/tests/Nice3point.TUnit.Revit.Tests/FirstClassLifetimeUiTests.cs b/tests/Nice3point.TUnit.Revit.Tests/FirstClassLifetimeUiTests.cs index d68b454..ef82819 100644 --- a/tests/Nice3point.TUnit.Revit.Tests/FirstClassLifetimeUiTests.cs +++ b/tests/Nice3point.TUnit.Revit.Tests/FirstClassLifetimeUiTests.cs @@ -6,8 +6,17 @@ public sealed class FirstClassLifetimeUiTests : ClassLifetimeUiTest { [Test] [Repeat(2)] - public Task ClassHooks_AnotherUiClass_DoNotOverlap() + public async Task ClassHooks_AnotherUiClass_DoNotOverlap() { - return AssertClassRunsAloneAsync(); + // 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 index abefb33..1f846ae 100644 --- a/tests/Nice3point.TUnit.Revit.Tests/SecondClassLifetimeUiTests.cs +++ b/tests/Nice3point.TUnit.Revit.Tests/SecondClassLifetimeUiTests.cs @@ -6,8 +6,17 @@ public sealed class SecondClassLifetimeUiTests : ClassLifetimeUiTest { [Test] [Repeat(2)] - public Task ClassHooks_AnotherUiClass_DoNotOverlap() + public async Task ClassHooks_AnotherUiClass_DoNotOverlap() { - return AssertClassRunsAloneAsync(); + // Arrange & Act + var openClass = OpenClass; + var classOpenAtSetup = ClassOpenAtSetup; + + // Assert + using (Assert.Multiple()) + { + await Assert.That(classOpenAtSetup).IsNull(); + await Assert.That(openClass).IsEqualTo(typeof(SecondClassLifetimeUiTests)); + } } } From 0d1bf4d9220b5d3dccff43744b9f69f04a8edeec Mon Sep 17 00:00:00 2001 From: Nice3point Date: Thu, 29 Oct 2026 18:03:28 +0300 Subject: [PATCH 5/7] [Docs] Split compound sentences and order the hook attributes by length --- AGENTS.md | 2 +- CHANGELOG.md | 5 +++-- README.md | 4 +++- .../Executors/RevitUiThreadExecutor.cs | 8 +++++--- .../Sessions/RevitUiSessionHooks.cs | 5 +++-- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 06e0f54..4b5d615 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -21,7 +21,7 @@ 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. -* UI tests in the test host run under one shared `NotInParallel` key, never under a parallel limit. A test waiting for a limit holds a parallel slot, and the tests of a limit interleave their classes. The UI key joins the keys a test declares in one constraint: TUnit reads only the first `NotInParallel` constraint of a test. A UI test outside Revit awaits its result without the Revit thread. +* UI tests in the test host run under one shared `NotInParallel` key, never under a parallel limit. A test waiting for a limit holds a parallel slot. The tests of a limit interleave their classes. The UI key joins the keys a test declares in one constraint: TUnit reads only the first `NotInParallel` constraint of a test. A UI test outside Revit awaits its result without the Revit thread. * Inside Revit, a UI test class starts only when the test host executes its first test. The wait runs before every `Before(Class)` hook of the class and counts towards none of their timeouts. * 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. diff --git a/CHANGELOG.md b/CHANGELOG.md index 197d7e7..cd1031e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,11 +17,12 @@ Each property is optional: `Id` defaults to a random GUID, `Name` to the ID in u 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, and `[After(Class)]` right after the last. +- `[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. The documents a class opens in its hooks stay open only while the tests of that class run. - The timeout of a `[Before(Class)]` hook covers only the hook. - The duration of the first test of a class includes its `[Before(Class)]` hooks. -- A UI test waiting for its turn holds no parallel slot, and `RevitApiTest` tests run meanwhile. +- A UI test waiting for its turn holds no parallel slot. ## Bug fixes diff --git a/README.md b/README.md index 7c53d08..58c5e5d 100644 --- a/README.md +++ b/README.md @@ -226,7 +226,9 @@ public sealed class UiDocumentTests : RevitApiUiTest ``` UI tests run one at a time, in parallel with `RevitApiTest` tests. -The UI tests of a class run one after another: `[Before(Class)]` runs right before the first of them, and `[After(Class)]` right after the last. +The UI tests of a class run one after another. +`[Before(Class)]` runs right before the first of them. +`[After(Class)]` runs right after the last. To run them in one queue with `RevitApiTest` tests, mark the class with `[ParallelLimiter]`: ```csharp diff --git a/source/Nice3point.TUnit.Revit/Executors/RevitUiThreadExecutor.cs b/source/Nice3point.TUnit.Revit/Executors/RevitUiThreadExecutor.cs index 7a27e3f..3d7b0c0 100644 --- a/source/Nice3point.TUnit.Revit/Executors/RevitUiThreadExecutor.cs +++ b/source/Nice3point.TUnit.Revit/Executors/RevitUiThreadExecutor.cs @@ -9,7 +9,8 @@ namespace Nice3point.TUnit.Revit.Executors; /// /// /// A test body and its hooks run inside a Revit API context, and their await continuations stay on the same thread. -/// The executor runs one Revit UI test at a time, and the test host runs the UI tests class by class. +/// The executor runs one Revit UI test at a time. +/// The test host runs the UI tests class by class. /// The executor supports only tests of a class derived from . /// public sealed class RevitUiThreadExecutor : ITestExecutor, IHookExecutor, ITestRegisteredEventReceiver @@ -115,8 +116,9 @@ public ValueTask OnTestRegistered(TestRegisteredContext context) /// /// The registration context of the test. /// - /// TUnit reads only the first of a test, and the key joins the keys of a constraint the test declares. - /// A constraint without keys runs the test alone, and the method leaves it unchanged. + /// TUnit reads only the first of a test. + /// The key joins the keys of a constraint the test declares. + /// The method leaves a constraint without keys unchanged: that constraint runs the test alone. /// private static void AddUiConstraint(TestRegisteredContext context) { diff --git a/source/Nice3point.TUnit.Revit/Sessions/RevitUiSessionHooks.cs b/source/Nice3point.TUnit.Revit/Sessions/RevitUiSessionHooks.cs index 64ddd8e..d8a6b8e 100644 --- a/source/Nice3point.TUnit.Revit/Sessions/RevitUiSessionHooks.cs +++ b/source/Nice3point.TUnit.Revit/Sessions/RevitUiSessionHooks.cs @@ -26,11 +26,12 @@ public static void RevitUiSessionSetup() /// 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, and the timeout of a [Before(Class)] hook excludes the wait. + /// 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 . /// - [BeforeEvery(Class, Order = int.MinValue)] [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)) From 66569e65d65961ec641410fdf8af8a3058e076bf Mon Sep 17 00:00:00 2001 From: Nice3point Date: Thu, 29 Oct 2026 18:10:07 +0300 Subject: [PATCH 6/7] [Docs] Replace figurative verbs and remove a redundant remark --- AGENTS.md | 5 +++-- CHANGELOG.md | 6 +++--- .../Executors/RevitUiThreadExecutor.cs | 7 +++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 4b5d615..07bb8ed 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -21,8 +21,9 @@ 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. -* UI tests in the test host run under one shared `NotInParallel` key, never under a parallel limit. A test waiting for a limit holds a parallel slot. The tests of a limit interleave their classes. The UI key joins the keys a test declares in one constraint: TUnit reads only the first `NotInParallel` constraint of a test. A UI test outside Revit awaits its result without the Revit thread. -* Inside Revit, a UI test class starts only when the test host executes its first test. The wait runs before every `Before(Class)` hook of the class and counts towards none of their timeouts. +* 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. diff --git a/CHANGELOG.md b/CHANGELOG.md index cd1031e..c4f6ad9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,10 +19,10 @@ The UI tests of a class run one after another https://github.com/Nice3point/Revi - `[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. - The documents a class opens in its hooks stay open only while the tests of that class run. -- The timeout of a `[Before(Class)]` hook covers only the hook. + 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 waiting for its turn holds no parallel slot. +- A UI test that waits for its turn occupies no parallel slot. ## Bug fixes diff --git a/source/Nice3point.TUnit.Revit/Executors/RevitUiThreadExecutor.cs b/source/Nice3point.TUnit.Revit/Executors/RevitUiThreadExecutor.cs index 3d7b0c0..130e9a2 100644 --- a/source/Nice3point.TUnit.Revit/Executors/RevitUiThreadExecutor.cs +++ b/source/Nice3point.TUnit.Revit/Executors/RevitUiThreadExecutor.cs @@ -10,13 +10,12 @@ namespace Nice3point.TUnit.Revit.Executors; /// /// A test body and its hooks run inside a Revit API context, and their await continuations stay on the same thread. /// The executor runs one Revit UI test at a time. -/// The test host runs the UI tests class by class. /// The executor supports only tests of a class derived from . /// public sealed class RevitUiThreadExecutor : ITestExecutor, IHookExecutor, ITestRegisteredEventReceiver { /// - /// The NotInParallel key every UI test in the test host shares. + /// The NotInParallel key of every UI test in the test host. /// private const string UiConstraintKey = "Nice3point.TUnit.Revit.Ui"; @@ -117,8 +116,8 @@ public ValueTask OnTestRegistered(TestRegisteredContext context) /// The registration context of the test. /// /// TUnit reads only the first of a test. - /// The key joins the keys of a constraint the test declares. - /// The method leaves a constraint without keys unchanged: that constraint runs the test alone. + /// 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) { From f7c037e08ce161272642c0ddc68749d22556c96c Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 29 Oct 2026 21:16:24 +0300 Subject: [PATCH 7/7] Update README.md --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 58c5e5d..fb845c1 100644 --- a/README.md +++ b/README.md @@ -226,9 +226,6 @@ public sealed class UiDocumentTests : RevitApiUiTest ``` UI tests run one at a time, in parallel with `RevitApiTest` tests. -The UI tests of a class run one after another. -`[Before(Class)]` runs right before the first of them. -`[After(Class)]` runs right after the last. To run them in one queue with `RevitApiTest` tests, mark the class with `[ParallelLimiter]`: ```csharp