Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add strategies for NUnit 3 test names to support fixed test name
Provide strategies to allow the test name to not change between discovery and execution. That is needed by some test runners to work correctly (e.g. VS adapter, NCrunch)
- Loading branch information
Showing
12 changed files
with
461 additions
and
41 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
22 changes: 22 additions & 0 deletions
22
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,22 @@ | ||
using NUnit.Framework; | ||
using NUnit.Framework.Internal; | ||
using System; | ||
|
||
namespace Ploeh.AutoFixture.NUnit3.UnitTest | ||
{ | ||
public class FixedNameTestMethodBuilderTest | ||
{ | ||
[Test] | ||
public void FixedNameTestMethodBuilderIsResilientToFactoryException() | ||
{ | ||
// Fixture setup | ||
var dummyMethod = new MethodWrapper(typeof(TestNameStrategiesFixture), nameof(TestNameStrategiesFixture.FixedNameDecoratedMethod)); | ||
var sut = new FixedNameTestMethodBuilder(); | ||
// Exercise system | ||
var testMethod = sut.Build(dummyMethod, 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
68 changes: 68 additions & 0 deletions
68
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,68 @@ | ||
namespace Ploeh.AutoFixture.NUnit3.UnitTest | ||
{ | ||
public class TestNameStrategiesFixture | ||
{ | ||
private static IFixture CreateFixtureWithInjectedValues() | ||
{ | ||
var result = new Fixture(); | ||
// Make so that fixed values will be returned | ||
result.Inject(42); | ||
result.Inject("foo"); | ||
return result; | ||
} | ||
|
||
public class AutoDataFixedNameAttribute : AutoDataAttribute | ||
{ | ||
public AutoDataFixedNameAttribute() | ||
{ | ||
TestMethodBuilder = new FixedNameTestMethodBuilder(); | ||
} | ||
} | ||
|
||
public class AutoDataVolatileNameAttribute : AutoDataAttribute | ||
{ | ||
public AutoDataVolatileNameAttribute() : 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(); | ||
} | ||
} | ||
|
||
[AutoDataFixedName] | ||
public void FixedNameDecoratedMethod(int expectedNumber, MyClass sut) | ||
{ | ||
} | ||
|
||
[AutoDataVolatileName] | ||
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) | ||
{ | ||
} | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
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,139 @@ | ||
using NUnit.Framework; | ||
using NUnit.Framework.Internal; | ||
using System; | ||
using System.Linq; | ||
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<AutoDataVolatileNameAttribute>(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<AutoDataVolatileNameAttribute>(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; | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
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,22 @@ | ||
using NUnit.Framework; | ||
using NUnit.Framework.Internal; | ||
using System; | ||
|
||
namespace Ploeh.AutoFixture.NUnit3.UnitTest | ||
{ | ||
public class VolatileNameTestMethodBuilderTest | ||
{ | ||
[Test] | ||
public void VolatileNameTestMethodBuilderIsResilientToFactoryException() | ||
{ | ||
// Fixture setup | ||
var anyMethod = new MethodWrapper(typeof(TestNameStrategiesFixture), nameof(TestNameStrategiesFixture.VolatileNameDecoratedMethod)); | ||
var sut = new VolatileNameTestMethodBuilder(); | ||
// Exercise system | ||
var testMethod = sut.Build(anyMethod, null, () => throw new Exception(), 0); | ||
// Verify outcome | ||
Assert.That(testMethod.Name, Is.EqualTo(nameof(TestNameStrategiesFixture.VolatileNameDecoratedMethod))); | ||
// 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.