From 2d3c2df8149d3bf4e584e50765e6ab4c8a37ff78 Mon Sep 17 00:00:00 2001
From: Pavel Vostretsov
Date: Wed, 17 Apr 2024 17:26:35 +0500
Subject: [PATCH] add simple setup factory
---
.../ParallelTestFixtureSetUpTest.cs | 2 +-
NUnit.Middlewares/ISetupFactory.cs | 9 +++
NUnit.Middlewares/SetupBuilderExtensions.cs | 12 ++--
NUnit.Middlewares/SetupFactory.cs | 12 ++++
NUnit.Middlewares/SimpleTestContext.cs | 2 +-
.../SimpleTestContextExtensions.cs | 63 ++++++++++++++-----
6 files changed, 78 insertions(+), 22 deletions(-)
create mode 100644 NUnit.Middlewares/ISetupFactory.cs
create mode 100644 NUnit.Middlewares/SetupFactory.cs
diff --git a/NUnit.Extensions.Tests/Middlewares/ParallelTestFixtureSetUpTest.cs b/NUnit.Extensions.Tests/Middlewares/ParallelTestFixtureSetUpTest.cs
index de4334f..018b06f 100644
--- a/NUnit.Extensions.Tests/Middlewares/ParallelTestFixtureSetUpTest.cs
+++ b/NUnit.Extensions.Tests/Middlewares/ParallelTestFixtureSetUpTest.cs
@@ -64,7 +64,7 @@ protected override void Configure(ISetupBuilder fixture, ISetupBuilder test)
[TestCase(10)]
public void Test(int n)
{
- var item3 = SimpleTestContext.Current.Get("item3");
+ var item3 = SimpleTestContext.Current.TryGet("item3");
item3.Should().NotBeNull();
((Counter)item3!).InvocationsCount.Should().Be(0);
diff --git a/NUnit.Middlewares/ISetupFactory.cs b/NUnit.Middlewares/ISetupFactory.cs
new file mode 100644
index 0000000..99ab664
--- /dev/null
+++ b/NUnit.Middlewares/ISetupFactory.cs
@@ -0,0 +1,9 @@
+using System;
+
+namespace SkbKontur.NUnit.Middlewares
+{
+ public interface ISetupFactory
+ {
+ ISetup Create(Type setupType, object[] args);
+ }
+}
\ No newline at end of file
diff --git a/NUnit.Middlewares/SetupBuilderExtensions.cs b/NUnit.Middlewares/SetupBuilderExtensions.cs
index 24ae81a..3f9fe5b 100644
--- a/NUnit.Middlewares/SetupBuilderExtensions.cs
+++ b/NUnit.Middlewares/SetupBuilderExtensions.cs
@@ -61,20 +61,20 @@ public static ISetupBuilder Use(this ISetupBuilder builder, SetUpAsync
});
}
- public static ISetupBuilder UseSetup(this ISetupBuilder builder, ISetup setup)
+ public static ISetupBuilder UseSetup(this ISetupBuilder builder, params object[] args)
+ where TSetup : ISetup
{
return builder.Use(async test =>
{
+ var factory = test.TryGetFromThisOrParentContext() ?? setupFactory;
+ var setup = factory.Create(typeof(TSetup), args);
+
await setup.SetUpAsync(test).ConfigureAwait(false);
return () => setup.TearDownAsync(test);
});
}
- public static ISetupBuilder UseSetup(this ISetupBuilder builder)
- where TSetup : ISetup, new()
- {
- return builder.UseSetup(new TSetup());
- }
+ private static readonly ISetupFactory setupFactory = new SetupFactory();
}
}
\ No newline at end of file
diff --git a/NUnit.Middlewares/SetupFactory.cs b/NUnit.Middlewares/SetupFactory.cs
new file mode 100644
index 0000000..79846a0
--- /dev/null
+++ b/NUnit.Middlewares/SetupFactory.cs
@@ -0,0 +1,12 @@
+using System;
+
+namespace SkbKontur.NUnit.Middlewares
+{
+ public class SetupFactory : ISetupFactory
+ {
+ public ISetup Create(Type setupType, object[] args)
+ {
+ return (ISetup)Activator.CreateInstance(setupType, args);
+ }
+ }
+}
\ No newline at end of file
diff --git a/NUnit.Middlewares/SimpleTestContext.cs b/NUnit.Middlewares/SimpleTestContext.cs
index 5c2ffff..0a47059 100644
--- a/NUnit.Middlewares/SimpleTestContext.cs
+++ b/NUnit.Middlewares/SimpleTestContext.cs
@@ -14,7 +14,7 @@ private SimpleTestContext(ITest test)
public static SimpleTestContext Current => new(TestExecutionContext.CurrentContext.CurrentTest);
- public object? Get(string key) => test.GetRecursive(key);
+ public object? TryGet(string key) => test.TryGetRecursive(key);
public bool ContainsKey(string key) => test.ContainsKeyRecursive(key);
public IReadOnlyList