0
votes

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
why do you need to capture this value? Can you just assert a failure if the value is not what is expected and review the test log? If you are trying to chain tests together, this would not be my recommendation for how to do that. In my blog, I cover an approach (called Scenarios) for performing actions required for another test to run correctly (eg, setting up preconditions like pending orders, etc). michaelpavlakcoding.blogspot.com/2016/06/… - MPavlak
Thanks for your reply. I need to capture this value because i need to pass it to a different test as part of end-to-end scenario. Also the business wants to see the results in the excel sheet rather than as coded ui default output. - Balaji Venkatachalam
It would not be wise to drive the tests this way. Your tests should be able to run independent of each other. if you are able, Microsoft Test Manager can output better reporting around what is passing and failing. - MPavlak

1 Answers

0
votes

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.