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
12 changed files
with
489 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
25 changes: 25 additions & 0 deletions
25
Src/AutoFixture.NUnit3.UnitTest/FixedNameTestMethodBuilderTest.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,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 | ||
} | ||
} | ||
} |
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
79 changes: 79 additions & 0 deletions
79
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,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) | ||
{ | ||
} | ||
} | ||
} |
141 changes: 141 additions & 0 deletions
141
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,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<VolatileNameTestMethodBuilder>()); | ||
// Teardown | ||
} | ||
|
||
[Test] | ||
public void InlineAutoDataAttributeUsesRealValueByDefault() | ||
{ | ||
// Fixture setup | ||
// Exercise system | ||
var sut = new InlineAutoDataAttribute(); | ||
// Verify outcome | ||
Assert.That(sut.TestMethodBuilder, | ||
Is.TypeOf<VolatileNameTestMethodBuilder>()); | ||
// Teardown | ||
} | ||
|
||
[Test] | ||
public void AutoDataUsesFixedValuesForTestName() | ||
{ | ||
// Fixture setup | ||
// Exercise system | ||
var testMethod = GetTestMethod<AutoDataFixedNameAttribute>(nameof(TestNameStrategiesFixture.FixedNameDecoratedMethod)); | ||
// Verify outcome | ||
Assert.That(testMethod.Name, | ||
Is.EqualTo(@"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(@"Ploeh.AutoFixture.NUnit3.UnitTest.TestNameStrategiesFixture.FixedNameDecoratedMethod(auto<Int32>,auto<MyClass>)")); | ||
// Teardown | ||
} | ||
|
||
[Test] | ||
public void AutoDataVolatileNameUsesRealValuesForTestName() | ||
{ | ||
// Fixture setup | ||
// Exercise system | ||
var testMethod = GetTestMethod<AutoDataVolatileNameAttributeAttribute>(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<AutoDataVolatileNameAttributeAttribute>(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<InlineAutoDataFixedNameAttribute>(nameof(TestNameStrategiesFixture.InlineFixedNameDecoratedMethod)); | ||
// Verify outcome | ||
Assert.That(testMethod.Name, | ||
Is.EqualTo(@"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(@"Ploeh.AutoFixture.NUnit3.UnitTest.TestNameStrategiesFixture.InlineFixedNameDecoratedMethod(""alpha"",""beta"",auto<String>)")); | ||
// Teardown | ||
} | ||
|
||
[Test] | ||
public void InlineAutoDataVolatileNameUsesRealValuesForTestName() | ||
{ | ||
// Fixture setup | ||
// Exercise system | ||
var testMethod = GetTestMethod<InlineAutoDataVolatileNameAttribute>(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<InlineAutoDataVolatileNameAttribute>(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<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; | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
Src/AutoFixture.NUnit3.UnitTest/VolatileNameTestMethodBuilderTest.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,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 | ||
} | ||
} | ||
} |
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.