编码的ui-从.csv文件读取/写入值 [英] Coded ui - Read/Write values from .csv files

查看:107
本文介绍了编码的ui-从.csv文件读取/写入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个codedui测试,该测试从.csv文件读取值.我正在使用以下代码:

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();
}

作为我的要求的一部分,我需要捕获测试结果,例如id生成,然后再次写回"testdata.csv".

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

我尝试使用:TestContext.DataRow["IDField"] = id;

但是它不成功.

我的问题是:

  1. 我们如何在运行时将值写入此文件?
  2. 如何在其他测试中读取上述值?
  3. 如果我需要从多个来源读取值,有没有办法做到这一点,因为在数据源中,我只能指定一个文件名.

谢谢

推荐答案

您无法更新为正在读取的CSV数据源文件.一种方法是写入新的CSV文件.这个新文件可能包含来自数据源CSV的所有数据以及任何新字段. [ClassInitialize][AssemblyInitialize]可用于用列标题写入CSV的第一行. [TestCleanup]可能是编写CSV数据行的好地方,因为无论测试用例是否通过,都应调用它.如果仅应为通过的测试编写CSV,则将CSV数据行写为[TestMethod]的最后一个动作.

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].

可以使用第二种读取上面编写的CSV的测试.尝试同时运行两个测试可能会失败.一个测试用例正在读取CSV,而另一个测试用例正在向其写入行,则可能导致各种问题. (例如,如果第二项测试赶上了第一项测试,它将如何处理读取没有数据的文件?不要假设这会如您所愿地工作,请假设是最差的.)

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.)

编码的UI仅允许为测试提供一个数据源属性.

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

这篇关于编码的ui-从.csv文件读取/写入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆