簡體   English   中英

如何將方法作為參數調用到 TestCase 屬性 NUnit 中?

[英]How to call method as a parameter into TestCase attribute NUnit?

如何將方法作為參數調用到 TestCase 屬性 NUnit 中?

就像我們可以這樣寫:[TestCase(1, 2, 3)]

如何調用如下函數?:[TestCase(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 屬性並為您的測試用例實現工廠類。

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.
    }

    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM