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? this[string key] => (IReadOnlyList?)test.ListRecursive(key); diff --git a/NUnit.Middlewares/SimpleTestContextExtensions.cs b/NUnit.Middlewares/SimpleTestContextExtensions.cs index 358563a..38f15ff 100644 --- a/NUnit.Middlewares/SimpleTestContextExtensions.cs +++ b/NUnit.Middlewares/SimpleTestContextExtensions.cs @@ -10,56 +10,91 @@ public static class SimpleTestContextExtensions { public static T Get(this IPropertyBag properties) { - return properties.Get(typeof(T).Name); + return properties.Get(TypeKey()); + } + + public static T? TryGet(this IPropertyBag properties) + { + return properties.TryGet(TypeKey()); } public static T Get(this IPropertyBag properties, string key) { - return (T?)properties.Get(key) + return properties.TryGet(key) ?? throw new KeyNotFoundException($"Cannot find item by key {key} in test properties"); } + public static T? TryGet(this IPropertyBag properties, string key) + { + return (T?)properties.Get(key); + } + public static T GetFromThisOrParentContext(this ITest test) { - return test.GetFromThisOrParentContext(typeof(T).Name); + return test.GetFromThisOrParentContext(TypeKey()); + } + + public static T? TryGetFromThisOrParentContext(this ITest test) + { + return test.TryGetFromThisOrParentContext(TypeKey()); } public static T GetFromThisOrParentContext(this ITest test, string key) { - return (T)test.GetRecursiveOrThrow(key); + return (T)test.GetRecursive(key); + } + + public static T? TryGetFromThisOrParentContext(this ITest test, string key) + { + return (T?)test.TryGetRecursive(key); } public static T Get(this SimpleTestContext context) { - return context.Get(typeof(T).Name); + return context.Get(TypeKey()); + } + + public static T? TryGet(this SimpleTestContext context) + { + return context.TryGet(TypeKey()); } public static T Get(this SimpleTestContext context, string key) { - return (T?)context.Get(key) + return context.TryGet(key) ?? throw new KeyNotFoundException($"Cannot find item by key {key} in test context"); } + public static T? TryGet(this SimpleTestContext context, string key) + { + return (T?)context.TryGet(key); + } + public static void Set(this IPropertyBag properties, T value) where T : notnull { - properties.Set(typeof(T).Name, value); + properties.Set(TypeKey(), value); } - public static object GetRecursiveOrThrow(this ITest test, string key) + public static object GetRecursive(this ITest test, string key) { - return test.GetRecursive(key) - ?? throw new KeyNotFoundException($"Cannot find item by key {key} in test {test.Name} or its parents"); + return test.TryGetRecursive(key) + ?? new KeyNotFoundException($"Cannot find item by key {key} in test {test.Name} or its parents"); } - public static object? GetRecursive(this ITest test, string key) => - GetRecursive(test, key, (p, k) => p.Get(k)); + public static object? TryGetRecursive(this ITest test, string key) => + GetRecursive(test, key, static (p, k) => p.Get(k)); public static bool ContainsKeyRecursive(this ITest test, string key) => - GetRecursive(test, key, (p, k) => p.ContainsKey(k) ? true : (bool?)null) ?? false; + GetRecursive(test, key, static (p, k) => p.ContainsKey(k) ? true : (bool?)null) ?? false; public static IList? ListRecursive(this ITest test, string key) => - GetRecursive(test, key, (p, k) => p[k]); + GetRecursive(test, key, static (p, k) => p[k]); + + private static string TypeKey() + { + return $"nunit-middlewares.{typeof(T).Name}"; + } private static T? GetRecursive(ITest leaf, string key, Func getValue) {