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

断了今生、忘了曾经 提交于 2019-12-12 03:44:23

问题


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



来源:https://stackoverflow.com/questions/37740301/how-to-execute-a-line-of-code-which-is-a-data-setup-code-in-mstest-before-all-te

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!