问题
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:
- How do we write value to this file during runtime?
- How to read the above value in a different test?
- 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