在 C# 中使用带有 NUNIT 的 params 关键字提供的参数太多
Too many arguments provided using params keyword with NUNIT in C#
您好,我正在使用 NUNIT 框架为 WPF 应用程序编写一些单元测试,我正面临这个 问题:
我这样写 positive 测试用例:
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" });
}
}
我就是这样使用它的:
[TestCaseSource(nameof(ThrowExceptionOnAnyStringNull_ShouldNotThrowException_TestCases))]
public void ThrowExceptionOnAnyStringNull_ShouldNotThrowException (params string[] args)
{
Assert.DoesNotThrow(() => ArgumentChecker.ThrowExceptionOnAnyStringNull(args));
}
要测试的方法的签名是:
public static void ThrowExceptionOnAnyStringNull (params string[] argsAndNames)
当我 运行 测试时,我从 Visual Studio 收到的错误是:
提供的参数过多
我试过添加第一个参数,就像我在其他测试中所做的那样!但该参数仍未使用。
这是与该额外参数一起使用的代码:
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));
}
我还有其他 53 个测试使用这种 TestCaseSource 结构,目前运行良好。
如何去掉那个额外的参数以获得干净的代码?
谢谢。
额外信息:
这就是我的 negative 测试用例的样子:
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));
}
尝试:
yield return new TestCaseData(new object[] { new string[] { <string array elements> }})
问题是 object[]
可从 string[]
分配,因此您的 string[]
被解析为每个元素是一个单独的参数,而不是整个元素是一个参数。
您好,我正在使用 NUNIT 框架为 WPF 应用程序编写一些单元测试,我正面临这个 问题:
我这样写 positive 测试用例:
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" });
}
}
我就是这样使用它的:
[TestCaseSource(nameof(ThrowExceptionOnAnyStringNull_ShouldNotThrowException_TestCases))]
public void ThrowExceptionOnAnyStringNull_ShouldNotThrowException (params string[] args)
{
Assert.DoesNotThrow(() => ArgumentChecker.ThrowExceptionOnAnyStringNull(args));
}
要测试的方法的签名是:
public static void ThrowExceptionOnAnyStringNull (params string[] argsAndNames)
当我 运行 测试时,我从 Visual Studio 收到的错误是: 提供的参数过多
我试过添加第一个参数,就像我在其他测试中所做的那样!但该参数仍未使用。
这是与该额外参数一起使用的代码:
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));
}
我还有其他 53 个测试使用这种 TestCaseSource 结构,目前运行良好。
如何去掉那个额外的参数以获得干净的代码?
谢谢。
额外信息:
这就是我的 negative 测试用例的样子:
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));
}
尝试:
yield return new TestCaseData(new object[] { new string[] { <string array elements> }})
问题是 object[]
可从 string[]
分配,因此您的 string[]
被解析为每个元素是一个单独的参数,而不是整个元素是一个参数。