Repository navigation
Raise data-source delegates at test lifecycle stages #7521
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 all commits
Commits
Show all changes
2 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
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,115 @@ | ||
| using TUnit.TestProject.Attributes; | ||
|
|
||
| namespace TUnit.TestProject; | ||
|
|
||
| public class DelegateLifecycleEventTests | ||
| { | ||
| [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] | ||
| public sealed class SubscribeAttribute : DataSourceGeneratorAttribute<int> | ||
| { | ||
| public int Rows { get; set; } = 1; | ||
|
|
||
| protected override IEnumerable<Func<int>> GenerateDataSources(DataGeneratorMetadata metadata) | ||
| { | ||
| for (var i = 0; i < Rows; i++) | ||
| { | ||
| yield return () => | ||
| { | ||
| metadata.TestBuilderContext.Current.StateBag["factoryState"] = "preserved"; | ||
| var events = metadata.TestBuilderContext.Current.Events; | ||
| events.OnTestRegistered += Record("registered"); | ||
| events.OnTestStart += Record("start"); | ||
| events.OnTestEnd += Record("end"); | ||
| events.OnTestSkipped += Record("skipped"); | ||
| events.OnTestRetry += (_, args) => | ||
| { | ||
| args.TestContext.StateBag.Items["retry"] = args.RetryAttempt; | ||
| return default; | ||
| }; | ||
| return 1; | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| private static Func<object, TestContext, ValueTask> Record(string name) => (_, context) => | ||
| { | ||
| context.StateBag.Items.TryGetValue(name, out var count); | ||
| context.StateBag.Items[name] = (count as int? ?? 0) + 1; | ||
| return default; | ||
| }; | ||
| } | ||
|
|
||
| [Test, Subscribe, EngineTest(ExpectedResult.Pass)] | ||
| public async Task Passing(int value) | ||
| { | ||
| await Assert.That(TestContext.Current!.StateBag.Items["registered"]).IsEqualTo(1); | ||
| await Assert.That(TestContext.Current.StateBag.Items["start"]).IsEqualTo(1); | ||
| } | ||
|
|
||
| [Test, Subscribe, EngineTest(ExpectedResult.Pass)] | ||
| public async Task Generic<T>(T value) | ||
| { | ||
| await Assert.That(TestContext.Current!.StateBag.Items["registered"]).IsEqualTo(1); | ||
| await Assert.That(TestContext.Current.StateBag.Items["start"]).IsEqualTo(1); | ||
| } | ||
|
|
||
| [Test, Subscribe, Retry(1), EngineTest(ExpectedResult.Pass)] | ||
| public async Task Retried(int value) | ||
| { | ||
| var context = TestContext.Current!; | ||
| if (!context.StateBag.Items.ContainsKey("retry")) | ||
| { | ||
| throw new InvalidOperationException("Exercise retry notification"); | ||
| } | ||
|
|
||
| await Assert.That(context.StateBag.Items["retry"]).IsEqualTo(1); | ||
| await Assert.That(context.StateBag.Items["start"]).IsEqualTo(2); | ||
| await Assert.That(context.StateBag.Items["end"]).IsEqualTo(1); | ||
| } | ||
|
|
||
| [Test, Subscribe, Skip("Exercise skipped notification")] | ||
| public void Skipped(int value) => throw new InvalidOperationException("Must not execute"); | ||
|
|
||
| [After(Class)] | ||
| public static async Task Verify(ClassHookContext context) | ||
| { | ||
| foreach (var test in context.Tests) | ||
| { | ||
| await Assert.That(test.StateBag.Items["registered"]).IsEqualTo(1); | ||
| if (test.Metadata.TestDetails.MethodName == nameof(Skipped)) | ||
| { | ||
| await Assert.That(test.StateBag.Items["skipped"]).IsEqualTo(1); | ||
| await Assert.That(test.StateBag.Items["end"]).IsEqualTo(1); | ||
| await Assert.That(test.StateBag.Items.ContainsKey("start")).IsFalse(); | ||
| } | ||
| else | ||
| { | ||
| var expected = test.Metadata.TestDetails.MethodName == nameof(Retried) ? 2 : 1; | ||
| await Assert.That(test.StateBag.Items["end"]).IsEqualTo(expected); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [DelegateLifecycleEventTests.Subscribe(Rows = 2)] | ||
| public class ClassDelegateLifecycleEventTests(int value) | ||
| { | ||
| [Test, Repeat(1), EngineTest(ExpectedResult.Pass)] | ||
| public async Task FirstRow() | ||
| { | ||
| await Assert.That(value).IsEqualTo(1); | ||
| await Assert.That(TestContext.Current!.StateBag.Items["factoryState"]).IsEqualTo("preserved"); | ||
| await Assert.That(TestContext.Current!.StateBag.Items["registered"]).IsEqualTo(1); | ||
| await Assert.That(TestContext.Current.StateBag.Items["start"]).IsEqualTo(1); | ||
| } | ||
|
|
||
| [After(Class)] | ||
| public static async Task Verify(ClassHookContext context) | ||
| { | ||
| foreach (var test in context.Tests) | ||
| { | ||
| await Assert.That(test.StateBag.Items["end"]).IsEqualTo(1); | ||
| } | ||
| } | ||
| } | ||
|
|
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.
Uh oh!
There was an error while loading. Please reload this page.