From 60aa0191fc04f8f4cedfb722b057c7eeae058a60 Mon Sep 17 00:00:00 2001 From: Thomas Caudal Date: Tue, 4 Oct 2016 10:19:43 +0200 Subject: [PATCH] NUnit3: change the way test name is created so that it doesn't change between discovery and execution --- .../AutoFixture.NUnit3.UnitTest.csproj | 4 + .../FixedNameTestMethodBuilderTest.cs | 25 ++++ Src/AutoFixture.NUnit3.UnitTest/Scenario.cs | 2 +- .../TestNameStrategiesFixture.cs | 79 ++++++++++ .../TestNameStrategiesTest.cs | 141 ++++++++++++++++++ .../VolatileNameTestMethodBuilderTest.cs | 25 ++++ Src/AutoFixture.NUnit3/AutoDataAttribute.cs | 32 ++-- .../AutoFixture.NUnit3.csproj | 3 + .../FixedNameTestMethodBuilder.cs | 105 +++++++++++++ Src/AutoFixture.NUnit3/ITestMethodBuilder.cs | 24 +++ .../InlineAutoDataAttribute.cs | 29 ++-- .../VolatileNameTestMethodBuilder.cs | 58 +++++++ 12 files changed, 489 insertions(+), 38 deletions(-) create mode 100644 Src/AutoFixture.NUnit3.UnitTest/FixedNameTestMethodBuilderTest.cs create mode 100644 Src/AutoFixture.NUnit3.UnitTest/TestNameStrategiesFixture.cs create mode 100644 Src/AutoFixture.NUnit3.UnitTest/TestNameStrategiesTest.cs create mode 100644 Src/AutoFixture.NUnit3.UnitTest/VolatileNameTestMethodBuilderTest.cs create mode 100644 Src/AutoFixture.NUnit3/FixedNameTestMethodBuilder.cs create mode 100644 Src/AutoFixture.NUnit3/ITestMethodBuilder.cs create mode 100644 Src/AutoFixture.NUnit3/VolatileNameTestMethodBuilder.cs diff --git a/Src/AutoFixture.NUnit3.UnitTest/AutoFixture.NUnit3.UnitTest.csproj b/Src/AutoFixture.NUnit3.UnitTest/AutoFixture.NUnit3.UnitTest.csproj index 603f933d3..55698216a 100644 --- a/Src/AutoFixture.NUnit3.UnitTest/AutoFixture.NUnit3.UnitTest.csproj +++ b/Src/AutoFixture.NUnit3.UnitTest/AutoFixture.NUnit3.UnitTest.csproj @@ -62,8 +62,12 @@ + + + + diff --git a/Src/AutoFixture.NUnit3.UnitTest/FixedNameTestMethodBuilderTest.cs b/Src/AutoFixture.NUnit3.UnitTest/FixedNameTestMethodBuilderTest.cs new file mode 100644 index 000000000..00c6d4287 --- /dev/null +++ b/Src/AutoFixture.NUnit3.UnitTest/FixedNameTestMethodBuilderTest.cs @@ -0,0 +1,25 @@ +using NUnit.Framework; +using NUnit.Framework.Internal; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Ploeh.AutoFixture.NUnit3.UnitTest +{ + public class FixedNameTestMethodBuilderTest + { + [Test] + public void FixedNameTestMethodBuilderIsResilientToFactoryException() + { + // Fixture setup + var method = new MethodWrapper(typeof(TestNameStrategiesFixture), nameof(TestNameStrategiesFixture.FixedNameDecoratedMethod)); + var sut = new FixedNameTestMethodBuilder(); + // Exercise system + var testMethod = sut.Build(method, null, () => throw new Exception(), 0); + // Verify outcome + Assert.That(testMethod.Name, Is.EqualTo(nameof(TestNameStrategiesFixture.FixedNameDecoratedMethod))); + // Teardown + } + } +} diff --git a/Src/AutoFixture.NUnit3.UnitTest/Scenario.cs b/Src/AutoFixture.NUnit3.UnitTest/Scenario.cs index 4c93f1454..c45127dde 100644 --- a/Src/AutoFixture.NUnit3.UnitTest/Scenario.cs +++ b/Src/AutoFixture.NUnit3.UnitTest/Scenario.cs @@ -378,7 +378,7 @@ public void InlineAutoDataCanBeUsedWithFrozen(int p1, int p2, [Frozen]string p3, [Theory, AutoData] public void NoAutoPropertiesAttributeLeavesPropertiesUnset( - [NoAutoProperties]PropertyHolder ph1, + [NoAutoProperties]PropertyHolder ph1, [NoAutoProperties]PropertyHolder ph2, [NoAutoProperties]PropertyHolder ph3 ) diff --git a/Src/AutoFixture.NUnit3.UnitTest/TestNameStrategiesFixture.cs b/Src/AutoFixture.NUnit3.UnitTest/TestNameStrategiesFixture.cs new file mode 100644 index 000000000..894ab6aa8 --- /dev/null +++ b/Src/AutoFixture.NUnit3.UnitTest/TestNameStrategiesFixture.cs @@ -0,0 +1,79 @@ +using NUnit.Framework.Internal; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Ploeh.AutoFixture.NUnit3.UnitTest +{ + public class TestNameStrategiesFixture + { + #region Custom attribute definitions + public const int InjectedIntValue = 42; + public const string InjectedStringValue = "foo"; + + private static IFixture CreateFixtureWithInjectedValues() + { + var result = new Fixture(); + // Make so that fixed values will be returned + result.Inject(InjectedIntValue); + result.Inject(InjectedStringValue); + return result; + } + + public class AutoDataFixedNameAttribute : AutoDataAttribute + { + public AutoDataFixedNameAttribute() + { + TestMethodBuilder = new FixedNameTestMethodBuilder(); + } + } + + public class AutoDataVolatileNameAttributeAttribute : AutoDataAttribute + { + public AutoDataVolatileNameAttributeAttribute() : base(CreateFixtureWithInjectedValues()) + { + TestMethodBuilder = new VolatileNameTestMethodBuilder(); + } + } + + public class InlineAutoDataFixedNameAttribute : InlineAutoDataAttribute + { + public InlineAutoDataFixedNameAttribute(params object[] arguments) : base(arguments) + { + TestMethodBuilder = new FixedNameTestMethodBuilder(); + } + } + + public class InlineAutoDataVolatileNameAttribute : InlineAutoDataAttribute + { + public InlineAutoDataVolatileNameAttribute(params object[] arguments) : base(CreateFixtureWithInjectedValues(), arguments) + { + TestMethodBuilder = new VolatileNameTestMethodBuilder(); + } + } + #endregion + + [AutoDataFixedName] + public void FixedNameDecoratedMethod( + int expectedNumber, MyClass sut) + { + } + + [AutoDataVolatileNameAttribute] + public void VolatileNameDecoratedMethod( + int expectedNumber, string value) + { + } + + [InlineAutoDataFixedName("alpha", "beta")] + public void InlineFixedNameDecoratedMethod(string p1, string p2, string p3) + { + } + + [InlineAutoDataVolatileNameAttribute("alpha", "beta")] + public void InlineVolatileNameDecoratedMethod(string p1, string p2, string p3) + { + } + } +} diff --git a/Src/AutoFixture.NUnit3.UnitTest/TestNameStrategiesTest.cs b/Src/AutoFixture.NUnit3.UnitTest/TestNameStrategiesTest.cs new file mode 100644 index 000000000..ad9e850e0 --- /dev/null +++ b/Src/AutoFixture.NUnit3.UnitTest/TestNameStrategiesTest.cs @@ -0,0 +1,141 @@ +using NUnit.Framework; +using NUnit.Framework.Internal; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using static Ploeh.AutoFixture.NUnit3.UnitTest.TestNameStrategiesFixture; + +namespace Ploeh.AutoFixture.NUnit3.UnitTest +{ + public class TestNameStrategiesTest + { + [Test] + public void AutoDataAttributeUsesRealValueByDefault() + { + // Fixture setup + // Exercise system + var sut = new AutoDataAttribute(); + // Verify outcome + Assert.That(sut.TestMethodBuilder, + Is.TypeOf()); + // Teardown + } + + [Test] + public void InlineAutoDataAttributeUsesRealValueByDefault() + { + // Fixture setup + // Exercise system + var sut = new InlineAutoDataAttribute(); + // Verify outcome + Assert.That(sut.TestMethodBuilder, + Is.TypeOf()); + // Teardown + } + + [Test] + public void AutoDataUsesFixedValuesForTestName() + { + // Fixture setup + // Exercise system + var testMethod = GetTestMethod(nameof(TestNameStrategiesFixture.FixedNameDecoratedMethod)); + // Verify outcome + Assert.That(testMethod.Name, + Is.EqualTo(@"FixedNameDecoratedMethod(auto,auto)")); + // Teardown + } + + [Test] + public void AutoDataFixedNameUsesFixedValuesForTestFullName() + { + // Fixture setup + // Exercise system + var testMethod = GetTestMethod(nameof(TestNameStrategiesFixture.FixedNameDecoratedMethod)); + // Verify outcome + Assert.That(testMethod.FullName, + Is.EqualTo(@"Ploeh.AutoFixture.NUnit3.UnitTest.TestNameStrategiesFixture.FixedNameDecoratedMethod(auto,auto)")); + // Teardown + } + + [Test] + public void AutoDataVolatileNameUsesRealValuesForTestName() + { + // Fixture setup + // Exercise system + var testMethod = GetTestMethod(nameof(TestNameStrategiesFixture.VolatileNameDecoratedMethod)); + // Verify outcome + Assert.That(testMethod.Name, + Is.EqualTo(@"VolatileNameDecoratedMethod(42,""foo"")")); + // Teardown + } + + [Test] + public void AutoDataVolatileNameUsesRealValuesForTestFullName() + { + // Fixture setup + // Exercise system + var testMethod = GetTestMethod(nameof(TestNameStrategiesFixture.VolatileNameDecoratedMethod)); + // Verify outcome + Assert.That(testMethod.FullName, + Is.EqualTo(@"Ploeh.AutoFixture.NUnit3.UnitTest.TestNameStrategiesFixture.VolatileNameDecoratedMethod(42,""foo"")")); + // Teardown + } + + [Test] + public void InlineAutoDataUsesFixedValuesForTestName() + { + // Fixture setup + // Exercise system + var testMethod = GetTestMethod(nameof(TestNameStrategiesFixture.InlineFixedNameDecoratedMethod)); + // Verify outcome + Assert.That(testMethod.Name, + Is.EqualTo(@"InlineFixedNameDecoratedMethod(""alpha"",""beta"",auto)")); + // Teardown + } + + [Test] + public void InlineAutoDataFixedNameUsesFixedValuesForTestFullName() + { + // Fixture setup + // Exercise system + var testMethod = GetTestMethod(nameof(TestNameStrategiesFixture.InlineFixedNameDecoratedMethod)); + // Verify outcome + Assert.That(testMethod.FullName, + Is.EqualTo(@"Ploeh.AutoFixture.NUnit3.UnitTest.TestNameStrategiesFixture.InlineFixedNameDecoratedMethod(""alpha"",""beta"",auto)")); + // Teardown + } + + [Test] + public void InlineAutoDataVolatileNameUsesRealValuesForTestName() + { + // Fixture setup + // Exercise system + var testMethod = GetTestMethod(nameof(TestNameStrategiesFixture.InlineVolatileNameDecoratedMethod)); + // Verify outcome + Assert.That(testMethod.Name, + Is.EqualTo(@"InlineVolatileNameDecoratedMethod(""alpha"",""beta"",""foo"")")); + // Teardown + } + + [Test] + public void InlineAutoDataVolatileNameUsesRealValuesForFullName() + { + // Fixture setup + // Exercise system + var testMethod = GetTestMethod(nameof(TestNameStrategiesFixture.InlineVolatileNameDecoratedMethod)); + // Verify outcome + Assert.That(testMethod.FullName, + Is.EqualTo(@"Ploeh.AutoFixture.NUnit3.UnitTest.TestNameStrategiesFixture.InlineVolatileNameDecoratedMethod(""alpha"",""beta"",""foo"")")); + // Teardown + } + + private static TestMethod GetTestMethod(string testName) where TAttribute : Attribute, NUnit.Framework.Interfaces.ITestBuilder + { + var method = new MethodWrapper(typeof(TestNameStrategiesFixture), testName); + var inlineAttribute = (TAttribute)Attribute.GetCustomAttribute(method.MethodInfo, typeof(TAttribute)); + var testMethod = inlineAttribute.BuildFrom(method, null).Single(); + return testMethod; + } + } +} diff --git a/Src/AutoFixture.NUnit3.UnitTest/VolatileNameTestMethodBuilderTest.cs b/Src/AutoFixture.NUnit3.UnitTest/VolatileNameTestMethodBuilderTest.cs new file mode 100644 index 000000000..4464e8afd --- /dev/null +++ b/Src/AutoFixture.NUnit3.UnitTest/VolatileNameTestMethodBuilderTest.cs @@ -0,0 +1,25 @@ +using NUnit.Framework; +using NUnit.Framework.Internal; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Ploeh.AutoFixture.NUnit3.UnitTest +{ + public class VolatileNameTestMethodBuilderTest + { + [Test] + public void VolatileNameTestMethodBuilderIsResilientToFactoryException() + { + // Fixture setup + var method = new MethodWrapper(typeof(TestNameStrategiesFixture), nameof(TestNameStrategiesFixture.FixedNameDecoratedMethod)); + var sut = new VolatileNameTestMethodBuilder(); + // Exercise system + var testMethod = sut.Build(method, null, () => throw new Exception(), 0); + // Verify outcome + Assert.That(testMethod.Name, Is.EqualTo(nameof(TestNameStrategiesFixture.FixedNameDecoratedMethod))); + // Teardown + } + } +} diff --git a/Src/AutoFixture.NUnit3/AutoDataAttribute.cs b/Src/AutoFixture.NUnit3/AutoDataAttribute.cs index 3b771721e..b824027e4 100644 --- a/Src/AutoFixture.NUnit3/AutoDataAttribute.cs +++ b/Src/AutoFixture.NUnit3/AutoDataAttribute.cs @@ -5,6 +5,7 @@ using NUnit.Framework.Internal; using NUnit.Framework.Internal.Builders; using Ploeh.AutoFixture.Kernel; +using System.Diagnostics.CodeAnalysis; namespace Ploeh.AutoFixture.NUnit3 { @@ -13,11 +14,21 @@ namespace Ploeh.AutoFixture.NUnit3 /// This implementation is based on TestCaseAttribute of NUnit3 /// [AttributeUsage(AttributeTargets.Method)] - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", Justification = "This attribute is the root of a potential attribute hierarchy.")] + [SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", Justification = "This attribute is the root of a potential attribute hierarchy.")] public class AutoDataAttribute : Attribute, ITestBuilder { private readonly IFixture _fixture; + private ITestMethodBuilder _testMethodBuilder = new VolatileNameTestMethodBuilder(); + /// + /// Gets or sets the current strategy. + /// + public ITestMethodBuilder TestMethodBuilder + { + get => _testMethodBuilder; + set => _testMethodBuilder = value ?? throw new ArgumentNullException(nameof(value)); + } + /// /// Construct a /// @@ -49,28 +60,11 @@ protected AutoDataAttribute(IFixture fixture) /// One or more TestMethods public IEnumerable BuildFrom(IMethodInfo method, Test suite) { - var test = new NUnitTestCaseBuilder().BuildTestMethod(method, suite, this.GetParametersForMethod(method)); + var test = TestMethodBuilder.Build(method, suite, () => GetParameterValues(method.GetParameters()).ToArray(), 0); yield return test; } - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "This method is always expected to return an instance of the TestCaseParameters class.")] - private TestCaseParameters GetParametersForMethod(IMethodInfo method) - { - try - { - var parameters = method.GetParameters(); - - var parameterValues = this.GetParameterValues(parameters); - - return new TestCaseParameters(parameterValues.ToArray()); - } - catch (Exception ex) - { - return new TestCaseParameters(ex); - } - } - private IEnumerable GetParameterValues(IEnumerable parameters) { return parameters.Select(Resolve); diff --git a/Src/AutoFixture.NUnit3/AutoFixture.NUnit3.csproj b/Src/AutoFixture.NUnit3/AutoFixture.NUnit3.csproj index 22ba43f8a..b4c37b75b 100644 --- a/Src/AutoFixture.NUnit3/AutoFixture.NUnit3.csproj +++ b/Src/AutoFixture.NUnit3/AutoFixture.NUnit3.csproj @@ -71,6 +71,7 @@ + @@ -84,6 +85,8 @@ + + diff --git a/Src/AutoFixture.NUnit3/FixedNameTestMethodBuilder.cs b/Src/AutoFixture.NUnit3/FixedNameTestMethodBuilder.cs new file mode 100644 index 000000000..de2d27c29 --- /dev/null +++ b/Src/AutoFixture.NUnit3/FixedNameTestMethodBuilder.cs @@ -0,0 +1,105 @@ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework.Interfaces; +using NUnit.Framework.Internal; +using System.Globalization; +using NUnit.Framework.Internal.Builders; +using System.Diagnostics.CodeAnalysis; + +namespace Ploeh.AutoFixture.NUnit3 +{ + /// + /// Builder that generates with fixed names. + /// This is needed for some test runners such as the Nunit test adaptor for Visual Studio. + /// + /// + public class FixedNameTestMethodBuilder : ITestMethodBuilder + { + /// + /// Initializes a new instance of the class. + /// + public FixedNameTestMethodBuilder() { } + + /// + public virtual TestMethod Build(IMethodInfo method, Test suite, Func argFactory, int autoDataStartIndex) + { + if (method == null) + { + throw new ArgumentNullException(nameof(method)); + } + + if (argFactory == null) + { + throw new ArgumentNullException(nameof(argFactory)); + } + + return new NUnitTestCaseBuilder().BuildTestMethod(method, suite, GetParametersForMethod(method, argFactory, autoDataStartIndex)); + } + + [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "This method is always expected to return an instance of the TestCaseParameters class.")] + private TestCaseParameters GetParametersForMethod(IMethodInfo method, Func argFactory, int autoDataStartIndex) + { + try + { + var parameterValues = argFactory(); + return GetParametersForMethod(method, parameterValues.ToArray(), autoDataStartIndex); + } + catch (Exception ex) + { + return new TestCaseParameters(ex); + } + } + + private TestCaseParameters GetParametersForMethod(IMethodInfo method, object[] args, int autoDataStartIndex) + { + var result = new TestCaseParameters(args); + var methodParameters = method.GetParameters(); + + EnsureOriginalArgumentsArrayIsNotShared(result); + + for (int i = autoDataStartIndex; i < result.OriginalArguments.Length; i++) + { + result.OriginalArguments[i] = new TypeNameRenderer(methodParameters[i].ParameterType); + } + + return result; + } + + /// + /// In NUnit 3.5+ the Arguments and OriginalArguments properties are containing two different instances, + /// not in earlier versions.
+ /// Unfortunately Arguments has a private setter and there is not way to change the instance other + /// than by using reflection...
+ /// When running in NUnit3.5+ the method is supposed to do nothing. + ///
+ private static void EnsureOriginalArgumentsArrayIsNotShared(TestCaseParameters parameters) + { + if (ReferenceEquals(parameters.Arguments, parameters.OriginalArguments)) + { + var clonedArguments = new object[parameters.OriginalArguments.Length]; + Array.Copy(parameters.OriginalArguments, clonedArguments, parameters.OriginalArguments.Length); + + var property = typeof(TestParameters).GetProperty(nameof(TestCaseParameters.OriginalArguments)); + property.SetValue(parameters, clonedArguments, null); + } + } + + private class TypeNameRenderer + { + public Type Type { get; } + + public TypeNameRenderer(Type type) + { + this.Type = type ?? throw new ArgumentNullException(nameof(type)); + } + + public override string ToString() + { + return "auto<" + Type.Name + ">"; + } + } + } +} diff --git a/Src/AutoFixture.NUnit3/ITestMethodBuilder.cs b/Src/AutoFixture.NUnit3/ITestMethodBuilder.cs new file mode 100644 index 000000000..643efdf1e --- /dev/null +++ b/Src/AutoFixture.NUnit3/ITestMethodBuilder.cs @@ -0,0 +1,24 @@ +using NUnit.Framework.Interfaces; +using NUnit.Framework.Internal; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Ploeh.AutoFixture.NUnit3 +{ + /// + /// Utility used to create a instance. + /// + public interface ITestMethodBuilder + { + /// + /// Builds a from a method and the argument values. + /// + /// The MethodInfo for which tests are to be constructed. + /// The suite to which the tests will be added. + /// The argument values generated for the test case. + /// Index at which the autodata values have been generated. + TestMethod Build(IMethodInfo method, Test suite, Func argFactory, int autoDataStartIndex); + } +} diff --git a/Src/AutoFixture.NUnit3/InlineAutoDataAttribute.cs b/Src/AutoFixture.NUnit3/InlineAutoDataAttribute.cs index acadf34a8..82608be2a 100644 --- a/Src/AutoFixture.NUnit3/InlineAutoDataAttribute.cs +++ b/Src/AutoFixture.NUnit3/InlineAutoDataAttribute.cs @@ -20,6 +20,16 @@ public class InlineAutoDataAttribute : Attribute, ITestBuilder private readonly object[] _existingParameterValues; private readonly IFixture _fixture; + private ITestMethodBuilder _testMethodBuilder = new VolatileNameTestMethodBuilder(); + /// + /// Gets or sets the current strategy. + /// + public ITestMethodBuilder TestMethodBuilder + { + get => _testMethodBuilder; + set => _testMethodBuilder = value ?? throw new ArgumentNullException(nameof(value)); + } + /// /// Construct a /// with parameter values for test method @@ -66,28 +76,11 @@ public IEnumerable Arguments /// One or more TestMethods public IEnumerable BuildFrom(IMethodInfo method, Test suite) { - var test = new NUnitTestCaseBuilder().BuildTestMethod(method, suite, this.GetParametersForMethod(method)); + var test = TestMethodBuilder.Build(method, suite, () => GetParameterValues(method.GetParameters()).ToArray(), _existingParameterValues.Count()); yield return test; } - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "This method is always expected to return an instance of the TestCaseParameters class.")] - private TestCaseParameters GetParametersForMethod(IMethodInfo method) - { - try - { - var parameters = method.GetParameters(); - - var parameterValues = this.GetParameterValues(parameters); - - return new TestCaseParameters(parameterValues.ToArray()); - } - catch (Exception ex) - { - return new TestCaseParameters(ex); - } - } - /// /// Get values for a collection of /// diff --git a/Src/AutoFixture.NUnit3/VolatileNameTestMethodBuilder.cs b/Src/AutoFixture.NUnit3/VolatileNameTestMethodBuilder.cs new file mode 100644 index 000000000..cb9121715 --- /dev/null +++ b/Src/AutoFixture.NUnit3/VolatileNameTestMethodBuilder.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework.Interfaces; +using NUnit.Framework.Internal; +using System.Diagnostics.CodeAnalysis; +using NUnit.Framework.Internal.Builders; + +namespace Ploeh.AutoFixture.NUnit3 +{ + /// + /// Utility used to create a instance. + /// + /// + public class VolatileNameTestMethodBuilder : ITestMethodBuilder + { + /// + /// Initializes a new instance of the class. + /// + public VolatileNameTestMethodBuilder() { } + + /// + public virtual TestMethod Build(IMethodInfo method, Test suite, Func argFactory, int autoDataStartIndex) + { + if (method == null) + { + throw new ArgumentNullException(nameof(method)); + } + + if (argFactory == null) + { + throw new ArgumentNullException(nameof(argFactory)); + } + + return new NUnitTestCaseBuilder().BuildTestMethod(method, suite, GetParametersForMethod(method, argFactory, autoDataStartIndex)); + } + + [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "This method is always expected to return an instance of the TestCaseParameters class.")] + private TestCaseParameters GetParametersForMethod(IMethodInfo method, Func argFactory, int autoDataStartIndex) + { + try + { + var parameterValues = argFactory(); + return GetParametersForMethod(method, parameterValues.ToArray(), autoDataStartIndex); + } + catch (Exception ex) + { + return new TestCaseParameters(ex); + } + } + + private TestCaseParameters GetParametersForMethod(IMethodInfo method, object[] args, int autoDataStartIndex) + { + return new TestCaseParameters(args); + } + } +}