简体   繁体   中英

Dynamic creation of test cases

I have a JSON file containing multiple test cases like so:

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

And I want to generate rougly something like:


   [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)

Now, I know how to parse and process the file, and how to write the test, but how do I generate TestCases based on the processed data?

You should use TestCaseSourceAttribute pointing to a method that generates test cases. There are several ways to use it described in the documentation. The following is typical...

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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM