簡體   English   中英

動態創建測試用例

[英]Dynamic creation of test cases

我有一個包含多個測試用例的 JSON 文件,如下所示:

{
    "cases":[
        {
            "case": "TestCas1",
            "input": "x=y",
            "result": {
                "type": "Eq",
                "lhs": "x",
                "rhs": "y"
            }
        },
        { 
        //etc
        }
    ]
}

我想粗略地生成類似的東西:


   [Test]
   [TestCase("x=y", "x", "y", "Eq")]
   /// Other test cases from file go here.
   public void Test(string input, string lhs, string rhs, string op)

現在,我知道如何解析和處理文件,以及如何編寫測試,但是如何根據處理后的數據生成 TestCases 呢?

您應該使用TestCaseSourceAttribute指向生成測試用例的方法。 文檔中描述了幾種使用它的方法。 以下是典型...

public class MyTestFixture
{
    [TestCaseSource(nameof(MyTestCases))]
    public void MyTestMethod(string input, string lhs, string rhs, string op)
    {
        // Your test code here
    }

    static IEnumerable<TestCaseData> MyTestCases()
    {
        foreach (var item in your json file) // pseudocode
        {
            // Get the four argument values

            yield return new TestCaseData(input, lhs, rhs, op);
        }
    }
}

暫無
暫無

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

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