简体   繁体   中英

How to execute a line of code which is a data setup code in MSTest before all test

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

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

Best, Daniel

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