如何使用从 .csv 文件读取的数据多次运行测试(数据驱动) [英] How to run a test many times with data read from .csv file (data driving)

查看:26
本文介绍了如何使用从 .csv 文件读取的数据多次运行测试(数据驱动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我们的一个 Web 应用程序自动化一些测试,我需要知道如何让我的编码 UI 项目从 CSV 文件读取数据.假设我想测试登录屏幕.我的 CSV 文件将包含一些用户名和密码.我希望我的编码 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.

推荐答案

网上有很多关于数据驱动编码 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.

  • 创建 CSV 文件.
  • 将 CSV 文件添加到项目中.
  • 确保已部署 CSV 文件.
  • 将 CSV 文件添加为单个测试的数据源.
  • 阅读 CSV 字段并在测试中使用它们.

详细步骤(有一些变化)如下所述.

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 文件的属性面板.将复制到输出目录"设置为如果更新则复制"或始终复制".一些文档推荐如果较新则复制",但我更喜欢始终复制",因为有时文件未按预期复制.两种复制方式的区别是磁盘空间小,时间长,但是磁盘一般都比较大,一般复制的时间比较短.在我看来,确保文件被正确复制远远超过任何节省.

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 字段并在测试中使用它们

编码的 UI 记录和生成工具创建类,其中的字段包含输入到文本框中或在断言中使用的值.每个 action 方法都有一个 ...Params 类,每个 assert 方法都有一个 ...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[...].

假设一个编码的 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 文件读取的数据多次运行测试(数据驱动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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