我正在尝试对我们的一个 Web 应用程序进行自动化测试,我需要知道如何让我的 Coded UI 项目从 CSV 文件中读取数据.假设我想测试登录屏幕.我的 CSV 文件将包含一些用户名和密码.我希望我的 Coded UI 测试能够读取这些登录详细信息并遍历它们以对每组数据运行测试.
I am trying to automate some testing for one of our web applications and I need to know how I can make my Coded UI project read data from a CSV file. Lets say I want to test a log in screen. My CSV file will contain a few user names and passwords. I want my Coded UI test to read these log in details and loop through them to run the test on each set of data.
网络上有很多关于数据驱动 Coded UI 测试的教程.使用 CSV 文件进行数据驱动的基本步骤如下.
The web has many tutorials on data driving Coded UI tests. The basic steps for data driving with a CSV file are as follows.
下面解释了详细的步骤,以及一些变化.
The detailed steps, with some variations, are explained below.
Visual Studio 2010 有一个数据源向导",可以执行其中的一些步骤.Visual Studio 2012 和 2013 版没有向导,因此所有步骤都必须手动完成.
Visual Studio 2010 has a "data source wizard" that does some of these steps. Visual Studio versions 2012 and 2013 do not have the wizard and so all the steps have to be done manually.
创建 CSV 文件
一种方法是在电子表格中创建文件,然后将其保存为逗号分隔值.另一种方法是使用文本编辑器并只编写文件.我使用电子表格程序处理大数据源文件,使用文本编辑器创建小文件.一些编辑器在文件开头添加字节顺序标记 (BOM),该标记将添加到 CSV 的第一个字段名称中,这似乎使该字段不可读.有关 BOM 的更多信息,请参阅此页面.
One way is to create the file in a spreadsheet then save it as Comma Separated Values. Another way is to use a text editor and just write the file. I use a spreadsheet program for big data source files and a text editor for creating small files. Some editors add a byte order mark (BOM) at the start of a file, that will be added to the first field name of the CSV which appears to make the field unreadable. See this page for more about the BOM.
将 CSV 文件添加到项目中
使用解决方案资源管理器中的上下文菜单,选择添加 -> 现有项目.然后浏览到所需的文件.请注意,文件过滤器可能需要更改为 *.*
或 *.csv
.
Use the context menu in solution explorer, select Add -> Existing Item. Then browse to the required file. Note the file filter will probably need to be altered to be *.*
or *.csv
.
确保已部署 CSV 文件
从解决方案资源管理器中打开 CSV 文件的属性面板.将Copy to output directory"设置为Copy if newer"或Copy always".一些文档推荐Copy if newer",但我更喜欢Copy always",因为有时文件没有按预期复制.两种复制方法的区别是磁盘空间小,时间少,但是磁盘通常很大,而复制的时间通常很小.在我看来,任何节省都远远超过确保文件将被正确复制.
Open the properties panel for the CSV file from solution explorer. Set "Copy to output directory" to "Copy if newer" or to "Copy always". Some documents recommend "Copy if newer" but I prefer "Copy always" as occasionally a file was not copied as I expected. The difference between the two copy methods is a little disk space and a little time, but disks are normally big and the time to copy is normally small. Any savings are, in my opinion, far outweighed by being sure that the file will be copied correctly.
将 CSV 文件添加为单个测试的数据源
将 [TestMethod]
属性替换为正确的数据源行.这个 微软博客 显示几种可能的数据源文件类型的替换代码.对于 CSV 使用:
Replace the [TestMethod]
attribute with the correct data source line. This Microsoft blog shows the replacement code for several possible data source file types. For CSV use:
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV",
"|DataDirectory|\data.csv", "data#csv",
DataAccessMethod.Sequential), DeploymentItem("data.csv"),
TestMethod]
请注意,文件名出现了 3 次,其中一份包含 #
而不是 .
.我还没有找到关于 Datasource(...)
属性的不同字段的任何有用文档,因此无法进一步建议如何为非 CSV 数据源选择值.
Note that the file name occurs three times and one copy has a #
rather than a .
. I have not found any useful documentation about the different fields of the Datasource(...)
attribute so cannot advise further on how to choose values for non-CSV data sources.
上面的 |DataDirectory|
部分被替换为测试运行时部署文件的目录.如果需要,字符串引号中的整个文件名可以替换为文件的完整路径名.
The |DataDirectory|
part above is replaced by the directory where files are deployed when the tests run. The whole file name within the string quotes could be replaced by a full path name of a file, if required.
读取 CSV 字段并在测试中使用它们
Coded UI 记录和生成工具创建的类具有字段,这些字段保存输入到文本框中或在断言中使用的值.每个动作方法都有一个 ...Params
类,每个断言方法都有一个 ...ExpectedValues
类,其中 ...
是方法名称.这些字段的默认值是记录测试时使用的值.在调用动作或断言方法之前,记录的值可以被赋值覆盖.从 TestContext.DataRow[...]
访问数据源当前行的字段.
The Coded UI record and generate tool creates classes with fields that hold values entered into text boxes or used in assertions. Each action method has a ...Params
class and each assert method has an ...ExpectedValues
class, where the ...
is the method name. The default values of these fields are the values used when the test was recorded. The recorded values can be overwritten by an assignment before the action or assertion method is called. The fields of the current row of the data source are accessed from TestContext.DataRow[...]
.
假设 Coded UI 测试有一个 EnterValue
方法将文本写入屏幕的两个字段,并且它还有一个 CheckResult
方法断言一个字段.测试方法可以写成如下.
Suppose a Coded UI test has an EnterValue
method that writes text into two fields of the screen and it also has a CheckResult
method that asserts one field. The test method might then be written as follows.
[DataSource...
TestMethod]
public void CodedUITestMethod1()
{
this.UIMap.EnterValueParams.UIItem0TextSendKeys = TestContext.DataRow["ValueOne"].ToString();
this.UIMap.EnterValueParams.UIItem1TextSendKeys = TestContext.DataRow["ValueTwo"].ToString();
this.UIMap.EnterValue();
this.UIMap.CheckResultExpectedValues.UIItem0TextDisplayText = TestContext.DataRow["Result"].ToString();
this.UIMap.CheckResult();
}
...Params
和 ...ExpectedValues
类允许测试在测试运行时创建值.例如,如果 EnterValue
方法还想将明天的日期写入字段,我们可以在调用之前添加以下行:
The ...Params
and ...ExpectedValues
classes allow the test to create values when the test runs. For example, if the EnterValue
method also wanted to write tomorrow's date into a field we could add the following line before it is called:
this.UIMap.EnterValueParams.UIItem2TextSendKeys = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
这篇关于如何使用从 .csv 文件中读取的数据多次运行测试(数据驱动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!