如何使用多个数据源进行单元测试?
How to make a Unit test with several data sources?
我有一个方法,我想用两个数据源(在我的例子中是两个列表)测试它。
有人可以帮助并解释如何使它正确吗?
我应该使用属性 TestCaseSource 吗?如何使用?
public void TestMethodIntToBin(int intToConvert, string result)
{
Binary converter = new Binary();
string expectedResult = converter.ConvertTo(intToConvert);
Assert.AreEqual(expectedResult, result);
}
public List<int> ToConvert = new List<int>()
{
12,
13,
4,
64,
35,
76,
31,
84
};
public List<string> ResultList = new List<string>()
{
"00110110",
"00110110",
"00121011",
"00110110",
"00110110",
"00100110",
"00110110",
"00110110"
};
首先,您的数据源需要是静态的。这是 NUnit 3 的要求。
完成后,您可以在每个参数上使用 ValueSource attribute。例如,
[Test, Sequential]
public void TestMethodIntToBin([ValueSource(nameof(ToConvert))] int intToConvert,
[ValueSource(nameof(ResultList))] string result)
{
// Asserts
}
Sequential attribute specifies that you want NUnit to generate test cases by selecting the values in order. The other options are Combinatorial that causes every combination of values which is the default or Pairwise 为所有可能的对创建案例。
但是对于你的情况,我建议将你的两个数据源合并为一个并使用 TestCaseSource attribute。
[TestCaseSource(nameof(Conversions))]
public void TestMethodIntToBin(int intToConvert, string result)
{
// Asserts
}
static object[] Conversions = {
new object[] { 12, "00110110" },
new object[] { 13, "00110110" }
}
请注意,我正在为 C# 6 使用 nameof() 运算符。如果您不使用 Visual Studio 2015,只需切换到字符串即可。
我似乎无法将代码添加到此网站的评论中,因此我将其作为单独的答案发布,尽管它实际上是对 Rob 的答案的评论。
在您的特定情况下,您根本不需要 TestCaseSource...考虑一下:
[TestCase( 12, "00110110" )]
[TestCase( 13, "00110110" )]
public void TestMethodIntToBin(int intToConvert, string result)
{
// Asserts
}
我有一个方法,我想用两个数据源(在我的例子中是两个列表)测试它。 有人可以帮助并解释如何使它正确吗? 我应该使用属性 TestCaseSource 吗?如何使用?
public void TestMethodIntToBin(int intToConvert, string result)
{
Binary converter = new Binary();
string expectedResult = converter.ConvertTo(intToConvert);
Assert.AreEqual(expectedResult, result);
}
public List<int> ToConvert = new List<int>()
{
12,
13,
4,
64,
35,
76,
31,
84
};
public List<string> ResultList = new List<string>()
{
"00110110",
"00110110",
"00121011",
"00110110",
"00110110",
"00100110",
"00110110",
"00110110"
};
首先,您的数据源需要是静态的。这是 NUnit 3 的要求。
完成后,您可以在每个参数上使用 ValueSource attribute。例如,
[Test, Sequential]
public void TestMethodIntToBin([ValueSource(nameof(ToConvert))] int intToConvert,
[ValueSource(nameof(ResultList))] string result)
{
// Asserts
}
Sequential attribute specifies that you want NUnit to generate test cases by selecting the values in order. The other options are Combinatorial that causes every combination of values which is the default or Pairwise 为所有可能的对创建案例。
但是对于你的情况,我建议将你的两个数据源合并为一个并使用 TestCaseSource attribute。
[TestCaseSource(nameof(Conversions))]
public void TestMethodIntToBin(int intToConvert, string result)
{
// Asserts
}
static object[] Conversions = {
new object[] { 12, "00110110" },
new object[] { 13, "00110110" }
}
请注意,我正在为 C# 6 使用 nameof() 运算符。如果您不使用 Visual Studio 2015,只需切换到字符串即可。
我似乎无法将代码添加到此网站的评论中,因此我将其作为单独的答案发布,尽管它实际上是对 Rob 的答案的评论。
在您的特定情况下,您根本不需要 TestCaseSource...考虑一下:
[TestCase( 12, "00110110" )]
[TestCase( 13, "00110110" )]
public void TestMethodIntToBin(int intToConvert, string result)
{
// Asserts
}