Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NUnit3: change the way test name is created so that it doesn't change…
… between discovery and execution
- Loading branch information
Showing
7 changed files
with
264 additions
and
9 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
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
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
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,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using NUnit.Framework.Interfaces; | ||
using NUnit.Framework.Internal; | ||
|
||
namespace Ploeh.AutoFixture.NUnit3 | ||
{ | ||
/// <summary> | ||
/// Default builder used by <see cref="AutoDataAttribute"/> and <see cref="InlineAutoDataAttribute"/> to create | ||
/// a <see cref="TestCaseParameters"/> instance. | ||
/// </summary> | ||
/// <seealso cref="ITestCaseParameterBuilder" /> | ||
public class DefaultTestCaseParameterBuilder : ITestCaseParameterBuilder | ||
{ | ||
/// <summary> | ||
/// Gets a singleton instance of <see cref="DefaultTestCaseParameterBuilder"/>. | ||
/// </summary> | ||
/// <value>The instance.</value> | ||
public static DefaultTestCaseParameterBuilder Instance { get; } = new DefaultTestCaseParameterBuilder(); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DefaultTestCaseParameterBuilder"/> class. | ||
/// </summary> | ||
protected DefaultTestCaseParameterBuilder() { } | ||
|
||
/// <summary> | ||
/// Builds a <see cref="TestCaseParameters" /> from a method and the argument values. | ||
/// </summary> | ||
/// <param name="method">The MethodInfo for which tests are to be constructed.</param> | ||
/// <param name="args">The argument values generated for the test case.</param> | ||
/// <param name="autoDataStartIndex">Index at which the autodata values have been generated.</param> | ||
public virtual TestCaseParameters Build(IMethodInfo method, object[] args, int autoDataStartIndex) => | ||
new TestCaseParameters(args); | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
Src/AutoFixture.NUnit3/FixedNameTestCaseParameterBuilder.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,100 @@ | ||
| ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using NUnit.Framework.Interfaces; | ||
using NUnit.Framework.Internal; | ||
using System.Globalization; | ||
|
||
namespace Ploeh.AutoFixture.NUnit3 | ||
{ | ||
/// <summary> | ||
/// Builder that generates fixed values for the <see cref="TestCaseParameters"/>.OriginalArguments | ||
/// so that the the test name will have a fixed value.<BR/> | ||
/// This is needed for some test runners such as the Nunit test adaptor for Visual Studio. | ||
/// </summary> | ||
/// <seealso cref="DefaultTestCaseParameterBuilder" /> | ||
public class FixedNameTestCaseParameterBuilder : DefaultTestCaseParameterBuilder | ||
{ | ||
#region FixedNameArgument | ||
private class FixedNameArgument | ||
{ | ||
public Type Type { get; } | ||
|
||
public FixedNameArgument(Type type) | ||
{ | ||
this.Type = type ?? throw new ArgumentNullException(nameof(type)); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return string.Format(CultureInfo.InvariantCulture, InvariantAutoDataArgumentValue, Type.Name); | ||
} | ||
} | ||
#endregion | ||
|
||
/// <summary> | ||
/// Name used for producing the argument value as string for the test method so | ||
/// that it don't vary between discovery and execution. | ||
/// </summary> | ||
internal const string InvariantAutoDataArgumentValue = "auto<{0}>"; | ||
|
||
/// <summary> | ||
/// Gets a singleton instance of <see cref="FixedNameTestCaseParameterBuilder"/>. | ||
/// </summary> | ||
/// <value>The instance.</value> | ||
static new public FixedNameTestCaseParameterBuilder Instance { get; } = new FixedNameTestCaseParameterBuilder(); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FixedNameTestCaseParameterBuilder"/> class. | ||
/// </summary> | ||
protected FixedNameTestCaseParameterBuilder() { } | ||
|
||
/// <summary> | ||
/// Builds a <see cref="TestCaseParameters" /> from a method and the argument values. | ||
/// </summary> | ||
/// <param name="method">The MethodInfo for which tests are to be constructed.</param> | ||
/// <param name="args">The argument values generated for the test case.</param> | ||
/// <param name="autoDataStartIndex">Index at which the autodata values have been generated.</param> | ||
public override TestCaseParameters Build(IMethodInfo method, object[] args, int autoDataStartIndex) | ||
{ | ||
if (method == null) | ||
{ | ||
throw new ArgumentNullException(nameof(method)); | ||
} | ||
|
||
var result = base.Build(method, args, autoDataStartIndex); | ||
var methodParameters = method.GetParameters(); | ||
|
||
FixUniqueArgumentReference(result); | ||
|
||
for (int i = autoDataStartIndex; i < result.OriginalArguments.Length; i++) | ||
{ | ||
result.OriginalArguments[i] = new FixedNameArgument(methodParameters[i].ParameterType); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/// <summary> | ||
/// In NUnit 3.5+ the Arguments and OriginalArguments properties are containing two different instances, | ||
/// not in earlier versions.<BR/> | ||
/// Unfortunately Arguments has a private setter and there is not way to change the instance other | ||
/// than by using reflection...<BR/> | ||
/// When running in NUnit3.5+ the test below will be false. | ||
/// </summary> | ||
/// <param name="parameters">The parameters.</param> | ||
private static void FixUniqueArgumentReference(TestCaseParameters parameters) | ||
{ | ||
if (ReferenceEquals(parameters.Arguments, parameters.OriginalArguments)) | ||
{ | ||
var clonedArguments = new object[parameters.Arguments.Length]; | ||
Array.Copy((Array)parameters.Arguments, (Array)clonedArguments, parameters.Arguments.Length); | ||
|
||
var property = typeof(TestCaseParameters).GetProperty(nameof(TestCaseParameters.Arguments)); | ||
property.GetSetMethod(true).Invoke(parameters, new object[] { clonedArguments }); | ||
} | ||
} | ||
} | ||
} |
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,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 | ||
{ | ||
/// <summary> | ||
/// Utility used by <see cref="AutoDataAttribute"/> and <see cref="InlineAutoDataAttribute"/> to create | ||
/// a <see cref="TestCaseParameters"/> instance. | ||
/// </summary> | ||
public interface ITestCaseParameterBuilder | ||
{ | ||
/// <summary> | ||
/// Builds a <see cref="TestCaseParameters"/> from a method and the argument values. | ||
/// </summary> | ||
/// <param name="method">The MethodInfo for which tests are to be constructed.</param> | ||
/// <param name="args">The argument values generated for the test case.</param> | ||
/// <param name="autoDataStartIndex">Index at which the autodata values have been generated.</param> | ||
TestCaseParameters Build(IMethodInfo method, object[] args, int autoDataStartIndex); | ||
} | ||
} |
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