-
-
Notifications
You must be signed in to change notification settings - Fork 112
feat: add TestContext.Isolation interface for test resource isolation #4801
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
+246
−12
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
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
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,46 @@ | ||
| namespace TUnit.Core.Interfaces; | ||
|
|
||
| /// <summary> | ||
| /// Provides helpers for creating isolated resource names per test instance. | ||
| /// Useful for database tables, queue names, cache keys, and other resources | ||
| /// that need to be unique across parallel test execution. | ||
| /// Accessed via <see cref="TestContext.Isolation"/>. | ||
| /// </summary> | ||
| public interface ITestIsolation | ||
| { | ||
| /// <summary> | ||
| /// Gets a unique identifier for this test instance. | ||
| /// This value is assigned atomically and is guaranteed to be unique across all test instances in the process. | ||
| /// </summary> | ||
| int UniqueId { get; } | ||
|
|
||
| /// <summary> | ||
| /// Creates an isolated name by combining a base name with the test's unique identifier. | ||
| /// Use for database tables, Redis keys, Kafka topics, etc. | ||
| /// </summary> | ||
| /// <param name="baseName">The base name for the resource.</param> | ||
| /// <returns>A unique name in the format "Test_{UniqueId}_{baseName}".</returns> | ||
| /// <example> | ||
| /// <code> | ||
| /// // In a test with UniqueId = 42: | ||
| /// var tableName = TestContext.Current!.Isolation.GetIsolatedName("todos"); // Returns "Test_42_todos" | ||
| /// var topicName = TestContext.Current!.Isolation.GetIsolatedName("orders"); // Returns "Test_42_orders" | ||
| /// </code> | ||
| /// </example> | ||
| string GetIsolatedName(string baseName); | ||
|
|
||
| /// <summary> | ||
| /// Creates an isolated prefix using the test's unique identifier. | ||
| /// Use for key prefixes in Redis, Kafka topic prefixes, etc. | ||
| /// </summary> | ||
| /// <param name="separator">The separator character. Defaults to "_".</param> | ||
| /// <returns>A unique prefix in the format "test{separator}{UniqueId}{separator}".</returns> | ||
| /// <example> | ||
| /// <code> | ||
| /// // In a test with UniqueId = 42: | ||
| /// var prefix = TestContext.Current!.Isolation.GetIsolatedPrefix(); // Returns "test_42_" | ||
| /// var dotPrefix = TestContext.Current!.Isolation.GetIsolatedPrefix("."); // Returns "test.42." | ||
| /// </code> | ||
| /// </example> | ||
| string GetIsolatedPrefix(string separator = "_"); | ||
| } | ||
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,19 @@ | ||
| using TUnit.Core.Interfaces; | ||
|
|
||
| namespace TUnit.Core; | ||
|
|
||
| public partial class TestContext | ||
| { | ||
| internal static int _isolationIdCounter; | ||
|
|
||
| internal int IsolationUniqueId { get; } | ||
|
|
||
| /// <inheritdoc/> | ||
| int ITestIsolation.UniqueId => IsolationUniqueId; | ||
|
|
||
| /// <inheritdoc/> | ||
| string ITestIsolation.GetIsolatedName(string baseName) => $"Test_{IsolationUniqueId}_{baseName}"; | ||
|
|
||
| /// <inheritdoc/> | ||
| string ITestIsolation.GetIsolatedPrefix(string separator) => $"test{separator}{IsolationUniqueId}{separator}"; | ||
| } |
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
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
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
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
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
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,114 @@ | ||
| using TUnit.Assertions.Extensions; | ||
| using TUnit.Core.Interfaces; | ||
|
|
||
| namespace TUnit.UnitTests; | ||
|
|
||
| public class TestIsolationTests | ||
| { | ||
| [Test] | ||
| public async Task UniqueId_IsPositive() | ||
| { | ||
| var isolation = TestContext.Current!.Isolation; | ||
|
|
||
| await Assert.That(isolation.UniqueId).IsGreaterThan(0); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task UniqueId_IsDifferentAcrossTests_1() | ||
| { | ||
| // Store uniqueId in state bag so a parallel test can verify it's different | ||
| var isolation = TestContext.Current!.Isolation; | ||
| TestContext.Current.StateBag["isolation_test_id_1"] = isolation.UniqueId; | ||
|
|
||
| await Assert.That(isolation.UniqueId).IsGreaterThan(0); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task UniqueId_IsDifferentAcrossTests_2() | ||
| { | ||
| var isolation = TestContext.Current!.Isolation; | ||
| TestContext.Current.StateBag["isolation_test_id_2"] = isolation.UniqueId; | ||
|
|
||
| await Assert.That(isolation.UniqueId).IsGreaterThan(0); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task GetIsolatedName_ReturnsExpectedFormat() | ||
| { | ||
| var isolation = TestContext.Current!.Isolation; | ||
| var id = isolation.UniqueId; | ||
|
|
||
| var name = isolation.GetIsolatedName("foo"); | ||
|
|
||
| await Assert.That(name).IsEqualTo($"Test_{id}_foo"); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task GetIsolatedName_WithDifferentBaseNames_ReturnsDifferentNames() | ||
| { | ||
| var isolation = TestContext.Current!.Isolation; | ||
|
|
||
| var name1 = isolation.GetIsolatedName("todos"); | ||
| var name2 = isolation.GetIsolatedName("orders"); | ||
|
|
||
| await Assert.That(name1).IsNotEqualTo(name2); | ||
| await Assert.That(name1).Contains("todos"); | ||
| await Assert.That(name2).Contains("orders"); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task GetIsolatedPrefix_WithDefaultSeparator_ReturnsExpectedFormat() | ||
| { | ||
| var isolation = TestContext.Current!.Isolation; | ||
| var id = isolation.UniqueId; | ||
|
|
||
| var prefix = isolation.GetIsolatedPrefix(); | ||
|
|
||
| await Assert.That(prefix).IsEqualTo($"test_{id}_"); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task GetIsolatedPrefix_WithDotSeparator_ReturnsExpectedFormat() | ||
| { | ||
| var isolation = TestContext.Current!.Isolation; | ||
| var id = isolation.UniqueId; | ||
|
|
||
| var prefix = isolation.GetIsolatedPrefix("."); | ||
|
|
||
| await Assert.That(prefix).IsEqualTo($"test.{id}."); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task GetIsolatedPrefix_WithCustomSeparator_ReturnsExpectedFormat() | ||
| { | ||
| var isolation = TestContext.Current!.Isolation; | ||
| var id = isolation.UniqueId; | ||
|
|
||
| var prefix = isolation.GetIsolatedPrefix("-"); | ||
|
|
||
| await Assert.That(prefix).IsEqualTo($"test-{id}-"); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Isolation_IsAccessibleViaInterface() | ||
| { | ||
| ITestIsolation isolation = TestContext.Current!.Isolation; | ||
|
|
||
| await Assert.That(isolation).IsNotNull(); | ||
| await Assert.That(isolation.UniqueId).IsGreaterThan(0); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Isolation_SameContextReturnsSameValues() | ||
| { | ||
| var context = TestContext.Current!; | ||
|
|
||
| var id1 = context.Isolation.UniqueId; | ||
| var id2 = context.Isolation.UniqueId; | ||
| var name1 = context.Isolation.GetIsolatedName("test"); | ||
| var name2 = context.Isolation.GetIsolatedName("test"); | ||
|
|
||
| await Assert.That(id1).IsEqualTo(id2); | ||
| await Assert.That(name1).IsEqualTo(name2); | ||
| } | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Missing Snapshot Test Updates
This PR introduces public API changes that require updating snapshot tests per CLAUDE.md Rule 2:
Public API Changes Made
ITestIsolation(this file)ITestIsolationand exposespublic ITestIsolation Isolation => this;Required Action
Run the public API snapshot tests and commit the updated
.verified.txtfiles:See mandatory-rules.md for details on when snapshot tests are required.