Skip to content

Commit

Permalink
Add support for NUnit TestCaseSource attribute (#146)
Browse files Browse the repository at this point in the history
Add support for name generation when tests have `TestCaseSource` attribute
  • Loading branch information
mcliment authored Nov 16, 2021
1 parent 28b94d2 commit 7860166
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/Snapshooter.NUnit.Tests/NUnitSnapshotFullNameReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,49 @@ public async Task ReadSnapshotFullName_ResolveTheorySnapshotNameAsync_NameResolv
$"_{param1}_{param2}"));
}

[TestCaseSource(nameof(TestCases))]
public void ReadSnapshotFullName_ResolveTheoryDataSnapshotName_NameResolvedWithoutInlineDataParameters(
string param1, int param2)
{
// arrange
var snapshotFullNameResolver = new NUnitSnapshotFullNameReader();

// act
SnapshotFullName snapshotFullName = snapshotFullNameResolver.ReadSnapshotFullName();

// assert
Assert.That(snapshotFullName.Filename, Is.EqualTo(
$"{nameof(NUnitSnapshotFullNameReaderTests)}." +
$"{nameof(ReadSnapshotFullName_ResolveTheoryDataSnapshotName_NameResolvedWithoutInlineDataParameters)}" +
$"_{param1}_{param2}"));
}

[TestCaseSource(nameof(TestCases))]
public async Task ReadSnapshotFullName_ResolveTheoryDataSnapshotNameAsync_NameResolvedWithoutInlineDataParameters(
string param1, int param2)
{
// arrange
var snapshotFullNameResolver = new NUnitSnapshotFullNameReader();
await Task.Delay(1);

// act
SnapshotFullName snapshotFullName = snapshotFullNameResolver.ReadSnapshotFullName();

// assert
await Task.Delay(1);
Assert.That(snapshotFullName.Filename, Is.EqualTo(
$"{nameof(NUnitSnapshotFullNameReaderTests)}." +
$"{nameof(ReadSnapshotFullName_ResolveTheoryDataSnapshotNameAsync_NameResolvedWithoutInlineDataParameters)}" +
$"_{param1}_{param2}"));
}

private static object[] TestCases => new object[]
{
new object[] { "testString1", 5 },
new object[] { "testString2", 6 },
new object[] { "testString3", 7 }
};

#pragma warning restore xUnit1026 // Theory methods should use all of their parameters
}
}
8 changes: 7 additions & 1 deletion src/Snapshooter.NUnit/NUnitSnapshotFullNameReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ private static bool IsNUnitTestMethod(MemberInfo method)
{
bool isFactTest = IsTestMethod(method);
bool isTheoryTest = IsTestCaseTestMethod(method);
bool isTheoryDataTest = IsTestCaseSourceTestMethod(method);

return isFactTest || isTheoryTest;
return isFactTest || isTheoryTest || isTheoryDataTest;
}

private static bool IsTestMethod(MemberInfo method)
Expand All @@ -86,6 +87,11 @@ private static bool IsTestCaseTestMethod(MemberInfo method)
return method?.GetCustomAttributes(typeof(TestCaseAttribute))?.Any() ?? false;
}

private static bool IsTestCaseSourceTestMethod(MemberInfo method)
{
return method?.GetCustomAttributes(typeof(TestCaseSourceAttribute))?.Any() ?? false;
}

private static MethodBase EvaluateAsynchronMethodBase(MemberInfo method)
{
Type methodDeclaringType = method?.DeclaringType;
Expand Down

0 comments on commit 7860166

Please sign in to comment.