Coded ui - Read/Write values from .csv files

六眼飞鱼酱① 提交于 2020-01-07 02:12:07

问题


I have a codedui test which reads value from an .csv file. I'm using the following code:

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\testdata.csv", "testdata#csv", DataAccessMethod.Sequential), DeploymentItem("testdata.csv"), TestMethod]

public void test()
{
 String a = TestContext.DataRow["Field 1"].ToString();
}

As part of my requirement, i need to capture the test results e.g. id generated and write it back again to "testdata.csv".

I tried using: TestContext.DataRow["IDField"] = id;

But its not successful.

My question is:

  1. How do we write value to this file during runtime?
  2. How to read the above value in a different test?
  3. If i need to read values from multiple sources, is there a way to do it because in the datasource, i can give only one file name.

Thanks


回答1:


You cannot update to a CSV data source file that is being read. One approach is to write to a new CSV file. This new file could contain all the data from the data source CSV plus any new fields. The [ClassInitialize] or [AssemblyInitialize] could be used to write the first line of the CSV with the column titles. The [TestCleanup] might be a good place to write the CSV data line as it should be called whether or not the test case passed. If the CSV should be written only for tests that pass then write the CSV data line as the last action of the [TestMethod].

A second test that reads the CSV written as above can be used. Attempting to run both tests at the same time is likely to fail. Having a CSV being read by one test case while another test case is writing lines to it can lead all sorts of problems. (Eg what if the second test catches up with the first, how does it handle reading a file with no data? Do not assume this will work as you would like, assume the worst.)

Coded UI only allows one data source attribute to be given for a test.



来源:https://stackoverflow.com/questions/38111897/coded-ui-read-write-values-from-csv-files

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