-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do something before and after test case
- Loading branch information
1 parent
c0952e2
commit 33dabe0
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 Xunit.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Base attribute which indicates a test method interception (allows code to be run before and | ||
/// after the test is run). | ||
/// </summary> | ||
public abstract class BeforeAfterTest | ||
{ | ||
/// <summary> | ||
/// This method is called after the test method is executed. | ||
/// </summary> | ||
/// <param name="testClassInstance">The instance of test class</param> | ||
/// <param name="methodUnderTest">The method under test</param> | ||
public virtual void After(object? testClassInstance, MethodInfo methodUnderTest) { } | ||
|
||
/// <summary> | ||
/// This method is called after the test method is executed. | ||
/// </summary> | ||
/// <param name="testClassInstance">The instance of test class</param> | ||
/// <param name="methodUnderTest">The method under test</param> | ||
public virtual ValueTask AfterAsync(object? testClassInstance, MethodInfo methodUnderTest) | ||
{ | ||
After(testClassInstance, methodUnderTest); | ||
|
||
return default; | ||
} | ||
|
||
/// <summary> | ||
/// This method is called before the test method is executed. | ||
/// </summary> | ||
/// <param name="testClassInstance">The instance of test class</param> | ||
/// <param name="methodUnderTest">The method under test</param> | ||
public virtual void Before(object? testClassInstance, MethodInfo methodUnderTest) { } | ||
|
||
/// <summary> | ||
/// This method is called before the test method is executed. | ||
/// </summary> | ||
/// <param name="testClassInstance">The instance of test class</param> | ||
/// <param name="methodUnderTest">The method under test</param> | ||
public virtual ValueTask BeforeAsync(object? testClassInstance, MethodInfo methodUnderTest) | ||
{ | ||
Before(testClassInstance, methodUnderTest); | ||
|
||
return default; | ||
} | ||
} |
This file contains 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
20 changes: 20 additions & 0 deletions
20
test/Xunit.DependencyInjection.Test/BeforeAfterTestTest.cs
This file contains 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,20 @@ | ||
using System.Reflection; | ||
|
||
namespace Xunit.DependencyInjection.Test; | ||
|
||
public class BeforeAfterTestTest | ||
{ | ||
public IDependency? Dependency { get; set; } | ||
|
||
[Fact] | ||
public void Test() => Assert.NotNull(Dependency); | ||
} | ||
|
||
public class TestBeforeAfterTest(IDependency dependency) : BeforeAfterTest | ||
{ | ||
public override void Before(object? testClassInstance, MethodInfo methodUnderTest) | ||
{ | ||
if (testClassInstance is BeforeAfterTestTest beforeAfterTestTest) | ||
beforeAfterTestTest.Dependency = dependency; | ||
} | ||
} |
This file contains 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