Reputation: 45
Hello I'm writing some unit tests for a WPF application using NUNIT framework and I'm facing this problem:
I write my positive test cases like that:
public static IEnumerable<TestCaseData> ThrowExceptionOnAnyStringNull_ShouldNotThrowException_TestCases
{
get
{
yield return new TestCaseData(new string[] { "", "first test argument name" });
yield return new TestCaseData(new string[] { "test string", "first test argument name" });
yield return new TestCaseData(new string[] { "first", "first name", "second", "second name", "third", "third name" });
}
}
And I'm using it like that:
[TestCaseSource(nameof(ThrowExceptionOnAnyStringNull_ShouldNotThrowException_TestCases))]
public void ThrowExceptionOnAnyStringNull_ShouldNotThrowException (params string[] args)
{
Assert.DoesNotThrow(() => ArgumentChecker.ThrowExceptionOnAnyStringNull(args));
}
The signature of the method to test is:
public static void ThrowExceptionOnAnyStringNull (params string[] argsAndNames)
The error I receive from Visual Studio when I run the tests is: Too many arguments provided
I've tried to add a first parameter like I'm doing in other tests and its works! but the parameter remains unused.
This is the code used with that extra parameter:
public static IEnumerable<TestCaseData> ThrowExceptionOnAnyStringNull_ShouldNotThrowException_TestCases
{
get
{
yield return new TestCaseData(null, new string[] { "", "first test argument name" });
yield return new TestCaseData(null, new string[] { "test string", "first test argument name" });
yield return new TestCaseData(null, new string[] { "first", "first name", "second", "second name", "third", "third name" });
}
}
[TestCaseSource(nameof(ThrowExceptionOnAnyStringNull_ShouldNotThrowException_TestCases))]
public void ThrowExceptionOnAnyStringNull_ShouldNotThrowException (Type expectedExceptionType, params string[] args)
{
Assert.DoesNotThrow(() => ArgumentChecker.ThrowExceptionOnAnyStringNull(args));
}
I have other 53 tests with this structure of TestCaseSource currently working perfect.
How can I get rid of that extra parameter to get a clean code?
Thank you.
Extra info:
This is how my negative test cases look like:
public static IEnumerable<TestCaseData> ThrowExceptionOnAnyStringNull_ShouldThrowException_TestCases
{
get
{
// Empty list
yield return new TestCaseData(typeof(ArgumentNullException), null);
// List with odd number of elements
yield return new TestCaseData(typeof(ArgumentException) , new string[] { null });
yield return new TestCaseData(typeof(ArgumentException) , new string[] { "" });
yield return new TestCaseData(typeof(ArgumentException) , new string[] { "test string" });
yield return new TestCaseData(typeof(ArgumentException) , new string[] { null, "first test argument name", null });
yield return new TestCaseData(typeof(ArgumentException) , new string[] { "", "first test argument name", "" });
yield return new TestCaseData(typeof(ArgumentException) , new string[] { "test string", "first test argument name", "other" });
// List with even number of elements
yield return new TestCaseData(typeof(ArgumentNullException), new string[] { null, null });
yield return new TestCaseData(typeof(ArgumentException) , new string[] { null, "" });
yield return new TestCaseData(typeof(ArgumentNullException), new string[] { null, "first test argument name" });
yield return new TestCaseData(typeof(ArgumentNullException), new string[] { "", null });
yield return new TestCaseData(typeof(ArgumentException) , new string[] { "", "" });
yield return new TestCaseData(typeof(ArgumentNullException), new string[] { "test string", null });
yield return new TestCaseData(typeof(ArgumentException) , new string[] { "test string", "" });
yield return new TestCaseData(typeof(ArgumentNullException), new string[] { "first", "first name", null, "second name", "third", "third name" });
}
}
[TestCaseSource(nameof(ThrowExceptionOnAnyStringNull_ShouldThrowException_TestCases))]
public void ThrowExceptionOnAnyStringNull_ShouldThrowException (Type expectedExceptionType, params string[] args)
{
Assert.Throws(expectedExceptionType, () => ArgumentChecker.ThrowExceptionOnAnyStringNull(args));
}
Upvotes: 1
Views: 627
Reputation: 10035
Try:
yield return new TestCaseData(new object[] { new string[] { <string array elements> }})
The issue was object[]
is assignable from string[]
, so your string[]
was parsed as each element being one separate parameter not the whole being one single parameter.
Upvotes: 3