Fix TimeoutAttribute not being applied to non-test hooks #4115
+344
−134
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
[Timeout]attribute on[Before(Assembly)],[Before(Class)],[Before(TestSession)], and[Before(TestDiscovery)]hooks was ignored—always using the default 5-minute timeout regardless of the specified value.Root Cause
ProcessHookRegistrationAsync(which invokesTimeoutAttribute.OnHookRegistered) was only called for test hooks. The delegate creation methods for class/assembly/session/discovery hooks were synchronous and bypassed hook registration entirely.Changes
CreateClassHookDelegateAsync,CreateAssemblyHookDelegateAsync, etc.) that callProcessHookRegistrationAsyncCollectBefore/AfterClassHooksAsyncandCollectBefore/AfterAssemblyHooksAsyncto properly process hook registration eventsVerification
Both execution modes (Source Generated and Reflection) now respect hook timeouts:
Original prompt
This section details on the original issue you should resolve
<issue_title>[Bug]: TimeoutAttribute on [Before(Assembly)] hook is ignored - default 5 minute timeout always used</issue_title>
<issue_description>### Description
The
[Timeout]attribute placed on a[Before(Assembly)]hook method is not being applied. The hook always uses the default 5-minute timeout (300,000ms) defined inHookMethod.cs, regardless of the timeout value specified in the attribute.Expected Behavior
The hook should use the 15-minute timeout specified in the [Timeout(900_000)] attribute.
Actual Behavior
The hook fails with:
TUnit.Engine.Exceptions.TestFailedException: BeforeAssemblyException: BeforeAssembly hook failed:
Hook 'IntegrationTestsBase.SetUp()' exceeded timeout of 300000ms
The timeout is always 300,000ms (5 minutes) - the default value from HookMethod.Timeout.
Steps to Reproduce
[Before(Assembly)]hook that takes longer than 5 minutes on slow CI/CD environments[Timeout(900_000)]attribute to increase timeout to 15 minutesTUnit Version
1.5.80
.NET Version
.NET 10.0
Operating System
Linux
IDE / Test Runner
dotnet CLI (dotnet test / dotnet run)
Error Output / Stack Tra...
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.