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
10 changed files
with
430 additions
and
38 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
85 changes: 85 additions & 0 deletions
85
Src/AutoFixture.NUnit3.UnitTest/TestNameStrategiesFixture.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,85 @@ | ||
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 = 1; | ||
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 AutoDataDefaultNameAttribute : AutoDataAttribute | ||
{ | ||
public AutoDataDefaultNameAttribute() : base(CreateFixtureWithInjectedValues()) | ||
{ | ||
} | ||
} | ||
|
||
public class InlineAutoDataFixedNameAttribute : InlineAutoDataAttribute | ||
{ | ||
public InlineAutoDataFixedNameAttribute(params object[] arguments) : base(arguments) | ||
{ | ||
TestMethodBuilder = new FixedNameTestMethodBuilder(); | ||
} | ||
} | ||
|
||
public class InlineAutoDataDefaultNameAttribute : InlineAutoDataAttribute | ||
{ | ||
public InlineAutoDataDefaultNameAttribute(params object[] arguments) : base(CreateFixtureWithInjectedValues(), arguments) | ||
{ | ||
} | ||
} | ||
#endregion | ||
|
||
[AutoDataFixedName] | ||
public void FixedNameDecoratedMethod( | ||
int expectedNumber, MyClass sut) | ||
{ | ||
} | ||
|
||
[AutoDataDefaultName] | ||
public void DefaultAttributeStrategyDecoratedMethod( | ||
int expectedNumber, string value) | ||
{ | ||
} | ||
|
||
[InlineAutoDataFixedName("alpha", "beta")] | ||
public void InlineFixedNameDecoratedMethod(string p1, string p2, string p3) | ||
{ | ||
} | ||
|
||
[InlineAutoDataDefaultName("alpha", "beta")] | ||
public void DefaultInlineAttributeStrategyDecoratedMethod(string p1, string p2, string p3) | ||
{ | ||
} | ||
|
||
public static TestMethod GetTestMethod<TAttribute>(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; | ||
} | ||
} | ||
} |
133 changes: 133 additions & 0 deletions
133
Src/AutoFixture.NUnit3.UnitTest/TestNameStrategiesTest.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,133 @@ | ||
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<MutableNameTestMethodBuilder>()); | ||
// Teardown | ||
} | ||
|
||
[Test] | ||
public void InlineAutoDataAttributeUsesRealValueByDefault() | ||
{ | ||
// Fixture setup | ||
// Exercise system | ||
var sut = new InlineAutoDataAttribute(); | ||
// Verify outcome | ||
Assert.That(sut.TestMethodBuilder, | ||
Is.TypeOf<MutableNameTestMethodBuilder>()); | ||
// Teardown | ||
} | ||
|
||
[Test] | ||
public void AutoDataUsesFixedValuesForTestName() | ||
{ | ||
// Fixture setup | ||
// Exercise system | ||
var testMethod = GetTestMethod<AutoDataFixedNameAttribute>(nameof(TestNameStrategiesFixture.FixedNameDecoratedMethod)); | ||
// Verify outcome | ||
Assert.That(testMethod.Name, | ||
Is.EqualTo(nameof(TestNameStrategiesFixture.FixedNameDecoratedMethod) + "(auto<Int32>,auto<MyClass>)")); | ||
// Teardown | ||
} | ||
|
||
[Test] | ||
public void AutoDataFixedNameUsesFixedValuesForTestFullName() | ||
{ | ||
// Fixture setup | ||
// Exercise system | ||
var testMethod = GetTestMethod<AutoDataFixedNameAttribute>(nameof(TestNameStrategiesFixture.FixedNameDecoratedMethod)); | ||
// Verify outcome | ||
Assert.That(testMethod.FullName, | ||
Is.EqualTo(typeof(TestNameStrategiesFixture).FullName + "." + nameof(TestNameStrategiesFixture.FixedNameDecoratedMethod) + "(auto<Int32>,auto<MyClass>)")); | ||
// Teardown | ||
} | ||
|
||
[Test] | ||
public void AutoDataUsesRealValuesForTestNameByDefault() | ||
{ | ||
// Fixture setup | ||
// Exercise system | ||
var testMethod = GetTestMethod<AutoDataDefaultNameAttribute>(nameof(TestNameStrategiesFixture.DefaultAttributeStrategyDecoratedMethod)); | ||
// Verify outcome | ||
Assert.That(testMethod.Name, | ||
Is.EqualTo(nameof(TestNameStrategiesFixture.DefaultAttributeStrategyDecoratedMethod) + "(" + TestNameStrategiesFixture.InjectedIntValue + @",""" + TestNameStrategiesFixture.InjectedStringValue + @""")")); | ||
// Teardown | ||
} | ||
|
||
[Test] | ||
public void AutoDataUsesRealValuesForTestFullNameByDefault() | ||
{ | ||
// Fixture setup | ||
// Exercise system | ||
var testMethod = GetTestMethod<AutoDataDefaultNameAttribute>(nameof(TestNameStrategiesFixture.DefaultAttributeStrategyDecoratedMethod)); | ||
// Verify outcome | ||
Assert.That(testMethod.FullName, | ||
Is.EqualTo(typeof(TestNameStrategiesFixture).FullName + "." + nameof(TestNameStrategiesFixture.DefaultAttributeStrategyDecoratedMethod) + "(" + TestNameStrategiesFixture.InjectedIntValue + @",""" + TestNameStrategiesFixture.InjectedStringValue + @""")")); | ||
// Teardown | ||
} | ||
|
||
[Test] | ||
public void InlineAutoDataUsesFixedValuesForTestName() | ||
{ | ||
// Fixture setup | ||
// Exercise system | ||
var testMethod = GetTestMethod<InlineAutoDataFixedNameAttribute>(nameof(TestNameStrategiesFixture.InlineFixedNameDecoratedMethod)); | ||
// Verify outcome | ||
Assert.That(testMethod.Name, | ||
Is.EqualTo(nameof(TestNameStrategiesFixture.InlineFixedNameDecoratedMethod) + @"(""alpha"",""beta"",auto<String>)")); | ||
// Teardown | ||
} | ||
|
||
[Test] | ||
public void InlineAutoDataFixedNameUsesFixedValuesForTestFullName() | ||
{ | ||
// Fixture setup | ||
// Exercise system | ||
var testMethod = GetTestMethod<InlineAutoDataFixedNameAttribute>(nameof(TestNameStrategiesFixture.InlineFixedNameDecoratedMethod)); | ||
// Verify outcome | ||
Assert.That(testMethod.FullName, | ||
Is.EqualTo(typeof(TestNameStrategiesFixture).FullName + "." + nameof(TestNameStrategiesFixture.InlineFixedNameDecoratedMethod) + @"(""alpha"",""beta"",auto<String>)")); | ||
// Teardown | ||
} | ||
|
||
[Test] | ||
public void InlineAutoDataUsesRealValuesForTestNameByDefault() | ||
{ | ||
// Fixture setup | ||
// Exercise system | ||
var testMethod = GetTestMethod<InlineAutoDataDefaultNameAttribute>(nameof(TestNameStrategiesFixture.DefaultInlineAttributeStrategyDecoratedMethod)); | ||
// Verify outcome | ||
Assert.That(testMethod.Name, | ||
Is.EqualTo(nameof(TestNameStrategiesFixture.DefaultInlineAttributeStrategyDecoratedMethod) + @"(""alpha"",""beta"",""" + TestNameStrategiesFixture.InjectedStringValue + @""")")); | ||
// Teardown | ||
} | ||
|
||
[Test] | ||
public void InlineAutoDataFixedNameUsesRealValuesForTestFullNameByDefault() | ||
{ | ||
// Fixture setup | ||
// Exercise system | ||
var testMethod = GetTestMethod<InlineAutoDataDefaultNameAttribute>(nameof(TestNameStrategiesFixture.DefaultInlineAttributeStrategyDecoratedMethod)); | ||
// Verify outcome | ||
Assert.That(testMethod.FullName, | ||
Is.EqualTo(typeof(TestNameStrategiesFixture).FullName + "." + nameof(TestNameStrategiesFixture.DefaultInlineAttributeStrategyDecoratedMethod) + @"(""alpha"",""beta"",""" + TestNameStrategiesFixture.InjectedStringValue + @""")")); | ||
// Teardown | ||
} | ||
} | ||
} |
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
Oops, something went wrong.