【发布时间】:2020-12-27 14:13:55
【问题描述】:
如何将方法作为参数调用到TestCase属性NUnit中?
就像我们可以这样写:[TestCase(1, 2, 3)]
如何调用如下函数?: [TestCase(SomeFunction(), 1, 2, 3)]
【问题讨论】:
标签: c# .net .net-core nunit moq
如何将方法作为参数调用到TestCase属性NUnit中?
就像我们可以这样写:[TestCase(1, 2, 3)]
如何调用如下函数?: [TestCase(SomeFunction(), 1, 2, 3)]
【问题讨论】:
标签: c# .net .net-core nunit moq
您可以尝试 TestCaseSource 属性并为您的用例使用静态方法。
public static IEnumerable<EditModel> Generator()
{
// You can also call methods here
yield return new EditModel();
}
[Test]
[TestCaseSource(nameof(Generator))]
public void DoSomething(EditModel model)
{
Console.WriteLine($"{model.commentText}");
}
【讨论】:
要扩展上面的答案,您还可以尝试 TestCaseSource 属性并为您的测试用例实现一个工厂类。
public class MyFactoryClass
{
public static IEnumerable TestCases
{
get
{
yield return new TestCaseData(MyMethod()).Returns(expectedTestResult);
....
}
}
public EditModel MyMethod()
{
// Put method code here
return editModel;
}
}
public class MyTests
{
[Test,TestCaseSource(typeof(MyFactoryClass),"TestCases")]
public void DoSomething(EditModel model)
{
// Test code here.
}
...
}
【讨论】: