带输入参数的单元测试
Unit Test with input parameter
如何将方法作为参数调用到 TestCase 属性 NUnit 中?
我们可以这样写:[TestCase(1, 2, 3)]
如何调用如下函数?:
[测试用例(SomeFunction(), 1, 2, 3)]
您可以尝试 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 属性并为您的测试用例实施工厂 class。
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.
}
...
}
如何将方法作为参数调用到 TestCase 属性 NUnit 中?
我们可以这样写:[TestCase(1, 2, 3)]
如何调用如下函数?: [测试用例(SomeFunction(), 1, 2, 3)]
您可以尝试 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 属性并为您的测试用例实施工厂 class。
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.
}
...
}