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

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

问题描述

我试图自动为我们的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.


  • 创建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文件

一种方法是在A S preadsheet创建文件,然后将其保存为逗号分隔值。另一种方法是使用文本编辑器,只是写入文件。我使用的大数据源文件A S preadsheet计划和创建小文件的文本编辑器。有些编辑在文件的开始,这将被添加到CSV的第一个字段名称出现,使现场无法读取添加一个字节顺序标记(BOM)。见<一href=\"http://stackoverflow.com/questions/24735579/$c$cdui-test-will-not-find-csv-input/24768318#24768318\">this页了解有关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文件的项目

使用上下文菜单在Solution Explorer中,选择添加 - > 现有项即可。然后浏览到所需的文件。请注意,文件过滤器可能会需要改变为 *。* *。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文件部署

打开属性面板从Solution Explorer中的CSV文件。将复制到输出目录复制如果新复制总是。有些文件建议复制如果新,但我preFER复制总是作为偶尔文件未被复制如我所料。两个拷贝方法之间的差别是一个小的磁盘空间和很少的时间,但是磁盘通常大和复制的时间通常是小的。任何储蓄,在我看来,远远由是确保文件将被正确复制抵销。

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的] 属性。这<一个href=\"http://blogs.msdn.com/b/mathew_aniyan/archive/2009/03/17/data-driving-$c$cd-ui-tests.aspx\">Microsoft博客显示替换code几种可能的数据源文件类型。对于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]

请注意该文件的名称出现三次,一个副本有一个而非。我还没有发现有关的不同领域的任何有用的文档数据源(...)属性所以不能对如何选择非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文件中读取数据运行测试很多次(数据驱动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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