Skip to content

Commit

Permalink
Update TestCaseSourceService to support TestCaseData.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricaun committed Sep 11, 2024
1 parent 5a3227d commit fef76c5
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 12 deletions.
10 changes: 10 additions & 0 deletions Build/.nuke/build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
"type": "boolean",
"description": "Shows the execution plan (HTML)"
},
"PreReleaseFilter": {
"type": "array",
"items": {
"type": "string"
}
},
"Profile": {
"type": "array",
"description": "Defines the profiles to load",
Expand Down Expand Up @@ -99,8 +105,10 @@
"Build",
"Clean",
"Compile",
"GitPreRelease",
"GitRelease",
"Pack",
"PrePack",
"Release",
"Sign",
"Test"
Expand All @@ -120,8 +128,10 @@
"Build",
"Clean",
"Compile",
"GitPreRelease",
"GitRelease",
"Pack",
"PrePack",
"Release",
"Sign",
"Test"
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Update to support target `net48` and `net8.0`.
- Update `TestAttributeService` to convert `TestCaseSourceAttribute` to `TestCaseAttribute`.
- Update `TestCaseSourceService` to support multiple parameters.
- Update `TestCaseSourceService` to support `TestCaseData`.
### Added
- Add `TestCaseSourceService` with `TestCaseSourceAttribute` implementation.
### Tests
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>1.4.0-alpha.1</Version>
<Version>1.4.0-beta</Version>
</PropertyGroup>
</Project>
29 changes: 23 additions & 6 deletions SampleTest.Tests/TestsCaseSource.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using System.Collections;
using System.Collections.Generic;

namespace SampleTest.Tests
Expand Down Expand Up @@ -40,21 +41,37 @@ public void CasesSourceMethodWithParametersTest(int i)
Assert.True(i > 0);
}

public class AnotherClass
{
public static object[] CasesSource =
{
new object[] { 1, 2, 3 },
new object[] { 2, 3, 4 },
new object[] { 3, 4, 5 }
};
}
[TestCaseSource(typeof(AnotherClass), nameof(AnotherClass.CasesSource))]
public void CasesSourceAnotherClassTest(int i, int j, int k)
{
Assert.True(i > 0);
Assert.True(j > 0);
Assert.True(k > 0);
}
public class AnotherClass

public static IEnumerable TestCaseDatas
{
public static object[] CasesSource =
get
{
new object[] { 1, 2, 3 },
new object[] { 2, 3, 4 },
new object[] { 3, 4, 5 }
};
yield return new TestCaseData(0).Returns(false);
yield return new TestCaseData(1).Returns(true);
yield return new TestCaseData(2).Returns(true);
yield return new TestCaseData(3).Returns(true);
}
}
[TestCaseSource(nameof(TestCaseDatas))]
public bool CasesSourceTestCaseDataTest(int i)
{
return i > 0;
}
}
}
4 changes: 2 additions & 2 deletions ricaun.NUnit.Tests/NUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ public void TestAssembly_Parameters()
[TestCase("*.MultipleTest.*", 2)]
[TestCase("*AbstractTest?*", 4)]
[TestCase("*Abstract*", 4)]
[TestCase("*.TestsCaseSource.*", 13)]
[TestCase("*", 73)]
[TestCase("*.TestsCaseSource.*", 17)]
[TestCase("*", 77)]
public void TestAssembly_Filter(string testName, int numberOfTests)
{
TestEngineFilter.Add(testName);
Expand Down
14 changes: 11 additions & 3 deletions ricaun.NUnit/Services/TestCaseSourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,24 @@ internal static IEnumerable<TestCaseAttribute> GetTestCasesFromSource(MethodInfo
{
foreach (object item in source)
{
var testCase = new TestCaseAttribute(item);
if (item is Array array)
{
var args = new object[array.Length];
for (var i = 0; i < array.Length; i++)
args[i] = array.GetValue(i);

attributes.Add(new TestCaseAttribute(args));
continue;
testCase = new TestCaseAttribute(args);
}
attributes.Add(new TestCaseAttribute(item));
else if (item is TestCaseData testCaseData)
{
testCase = new TestCaseAttribute(testCaseData.Arguments);

if (testCaseData.HasExpectedResult)
testCase.ExpectedResult = testCaseData.ExpectedResult;
}

attributes.Add(testCase);
}
}
}
Expand Down

0 comments on commit fef76c5

Please sign in to comment.