How to execute a line of code which is a data setu

2019-09-10 04:19发布

问题:

I have a very uncommon scenario. I have a function, lets call this as DataGenerator. This method generates all test XMLs which are needed for the tests to execute. These XMLs are referenced as data source in each of the MSTests.

    [TestMethod]
    [TestCategory("UITest"), TestCategory("PersonalDetailsFlow")]
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\TestFlows.xml", "flow", DataAccessMethod.Sequential)]
    public void TestMethod1()
    {
     //Test Code
    } 

And I use the below code to create test XMLs

    [ClassInitialize]
    public static void ClassInit(TestContext context)
    {
        DriverData driverData = new DriverData();
        driverData.DataGenerator();
    }

When I run this code, I get the below error line The unit test adapter failed to connect to the data source or to read the data. For more information on troubleshooting this error, see "Troubleshooting Data-Driven Unit Tests"

I believe this is because MSTEST is looking for TestFlow.xml in |DataDirectory|\|

Can anyone please help me how to execute the code

DriverData driverData = new DriverData();
driverData.DataGenerator();

before any of the code gets executed so that I can avoid the above message. Any pointers would be really great

回答1:

did you try to use a static constructor? See: MSDN - static constructor

Best, Daniel