-
-
Notifications
You must be signed in to change notification settings - Fork 108
feat: add NotDiscoverableAttribute to hide tests from discovery #4154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1ee4d7a
feat: add IsNotDiscoverable property to TestContext
thomhurst 7485eb7
feat: add NotDiscoverableAttribute
thomhurst 27fe3f4
feat: skip discovery notification for NotDiscoverable tests
thomhurst d321747
test: add integration tests for NotDiscoverableAttribute
thomhurst eec7a05
test: add conditional NotDiscoverable test
thomhurst 86f7116
chore: update public API snapshots for NotDiscoverableAttribute
thomhurst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: add NotDiscoverableAttribute
- Loading branch information
commit 7485eb705bdd24eef86c894445df92aa8b2ac244
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
TUnit.Core/Attributes/TestMetadata/NotDiscoverableAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| using TUnit.Core.Interfaces; | ||
|
|
||
| namespace TUnit.Core; | ||
|
|
||
| /// <summary> | ||
| /// Specifies that a test method, test class, or assembly should be hidden from test discovery/explorer. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// When applied to a test method, class, or assembly, the NotDiscoverableAttribute prevents the test(s) | ||
| /// from appearing in test explorers and IDE test runners, while still allowing them to execute normally | ||
| /// when run via filters or direct invocation. | ||
| /// </para> | ||
| /// <para> | ||
| /// This is useful for infrastructure tests, internal helpers, or tests that should only be run | ||
| /// as dependencies of other tests. | ||
| /// </para> | ||
| /// </remarks> | ||
| /// <example> | ||
| /// <code> | ||
| /// // Simple usage - hide test from explorer | ||
| /// [Test] | ||
| /// [NotDiscoverable] | ||
| /// public void InfrastructureSetupTest() | ||
| /// { | ||
| /// // This test will not appear in test explorer but can still be executed | ||
| /// } | ||
| /// | ||
| /// // With reason for documentation | ||
| /// [Test] | ||
| /// [NotDiscoverable("Internal fixture helper - not meant to be run directly")] | ||
| /// public void SharedFixtureSetup() | ||
| /// { | ||
| /// // Hidden from discovery | ||
| /// } | ||
| /// | ||
| /// // Conditional hiding via inheritance | ||
| /// public class NotDiscoverableOnCIAttribute : NotDiscoverableAttribute | ||
| /// { | ||
| /// public NotDiscoverableOnCIAttribute() : base("Hidden on CI") { } | ||
| /// | ||
| /// public override Task<bool> ShouldHide(TestRegisteredContext context) | ||
| /// { | ||
| /// return Task.FromResult(Environment.GetEnvironmentVariable("CI") == "true"); | ||
| /// } | ||
| /// } | ||
| /// </code> | ||
| /// </example> | ||
| [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = false, Inherited = true)] | ||
| public class NotDiscoverableAttribute : TUnitAttribute, ITestRegisteredEventReceiver | ||
| { | ||
| /// <summary> | ||
| /// Gets the reason why this test is hidden from discovery. | ||
| /// </summary> | ||
| public string? Reason { get; } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="NotDiscoverableAttribute"/> class. | ||
| /// </summary> | ||
| public NotDiscoverableAttribute() | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="NotDiscoverableAttribute"/> class with a reason. | ||
| /// </summary> | ||
| /// <param name="reason">The reason why this test is hidden from discovery.</param> | ||
| public NotDiscoverableAttribute(string reason) | ||
| { | ||
| Reason = reason; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public int Order => int.MinValue; | ||
|
|
||
| /// <inheritdoc /> | ||
| public async ValueTask OnTestRegistered(TestRegisteredContext context) | ||
| { | ||
| if (await ShouldHide(context)) | ||
| { | ||
| context.TestContext.IsNotDiscoverable = true; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Determines whether the test should be hidden from discovery. | ||
| /// </summary> | ||
| /// <param name="context">The test context containing information about the test being registered.</param> | ||
| /// <returns> | ||
| /// A task that represents the asynchronous operation. | ||
| /// The task result is true if the test should be hidden; otherwise, false. | ||
| /// </returns> | ||
| /// <remarks> | ||
| /// Can be overridden in derived classes to implement conditional hiding logic | ||
| /// based on specific conditions or criteria. | ||
| /// | ||
| /// The default implementation always returns true, meaning the test will always be hidden. | ||
| /// </remarks> | ||
| public virtual Task<bool> ShouldHide(TestRegisteredContext context) => Task.FromResult(true); | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HTML entity '<' should be '<' in the code documentation example.