我有一个简单的方法,可以从列表中计算给定的计算。我想为这个方法写一些测试。
我正在使用NUnit。我使用TestCaseSource是因为我试图给出一个列表作为参数。我有来自这个question的解决方案。我的测试如下所示:
[TestFixture]
public class CalcViewModelTests : CalcViewModel
{
private static readonly object[] _data =
{
new object[] { new List<string> { "3", "+", "3" } },
new object[] { new List<string> { "5", "+", "10" } }
};
[Test, TestCaseSource(nameof(_data))]
public void Test(List<string> calculation)
{
var result = SolveCalculation(calculation);
Assert.That(result, Is.EqualTo("6"));
}
}我想测试多个计算,就像testCases一样。
TestCases拥有Result parameter。如何将结果添加到TestCaseSource,以便测试多个计算?
发布于 2019-07-10 16:54:24
看起来这应该行得通:
private static readonly object[] _data =
{
new object[] { new List<string> { "3", "+", "3" }, "6" },
new object[] { new List<string> { "5", "+", "10" }, "15" }
};
[Test, TestCaseSource(nameof(_data))]
public void Test(List<string> calculation, string expectedResult)
{
var result = SolveCalculation(calculation);
Assert.That(result, Is.EqualTo(expectedResult));
}发布于 2019-07-10 17:12:14
为此,您可以使用TestCaseData属性。它允许您将测试数据封装在单独的类中,并重用于其他测试
public class MyDataClass
{
public static IEnumerable TestCases
{
get
{
yield return new TestCaseData("3", "+", "3").Returns("6");
yield return new TestCaseData("5", "+", "10").Returns("15");
}
}
}
[Test]
[TestCaseSource(typeof(MyDataClass), nameof(MyDataClass.TestCases))]
public string Test(List<string> calculation)
{
var result = SolveCalculation(calculation);
return result;
}https://stackoverflow.com/questions/56966914
复制相似问题