Skip to content

Commit

Permalink
Do something before and after test case
Browse files Browse the repository at this point in the history
  • Loading branch information
pengweiqhca committed Apr 24, 2024
1 parent c0952e2 commit 33dabe0
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Xunit.DependencyInjection/BeforeAfterTest.cs
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;
}
}
16 changes: 16 additions & 0 deletions src/Xunit.DependencyInjection/DependencyInjectionTestInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ public class DependencyInjectionTestInvoker(
private static readonly ActivitySource ActivitySource = new("Xunit.DependencyInjection", typeof(DependencyInjectionTestInvoker).Assembly.GetName().Version?.ToString());
private static readonly MethodInfo AsTaskMethod = new Func<ObjectMethodExecutorAwaitable, Task>(AsTask).Method;

/// <inheritdoc/>
protected override async Task<decimal> InvokeTestMethodAsync(object? testClassInstance)
{
var beforeAfterTests = provider.GetServices<BeforeAfterTest>().ToArray();

foreach (var beforeAfterTest in beforeAfterTests)
await beforeAfterTest.BeforeAsync(testClassInstance, TestMethod).ConfigureAwait(false);

var result = await base.InvokeTestMethodAsync(testClassInstance).ConfigureAwait(false);

for (var index = beforeAfterTests.Length - 1; index >= 0; index--)
await beforeAfterTests[index].BeforeAsync(testClassInstance, TestMethod).ConfigureAwait(false);

return result;
}

/// <inheritdoc />
protected override object CallTestMethod(object testClassInstance)
{
Expand Down
20 changes: 20 additions & 0 deletions test/Xunit.DependencyInjection.Test/BeforeAfterTestTest.cs
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;
}
}
1 change: 1 addition & 0 deletions test/Xunit.DependencyInjection.Test/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public void ConfigureServices(IServiceCollection services) =>
services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Debug).AddXunitOutput())
.AddScoped<IDependency, DependencyClass>()
.AddScoped<IDependencyWithManagedLifetime, DependencyWithManagedLifetime>()
.AddScoped<BeforeAfterTest, TestBeforeAfterTest>()
.AddHostedService<HostServiceTest>()
.AddSkippableFactSupport()
.AddStaFactSupport()
Expand Down

0 comments on commit 33dabe0

Please sign in to comment.