引用链接:http://cukes.info/step-definitions.html

Data Tables

Data Tables are handy for specifying a larger piece of data:

Given the following users exist:
| name | email | phone |
| Aslak | aslak@email.com | 123 |
| Matt | matt@email.com | 234 |
| Joe | joe@email.org | 456 |

Just like Doc Strings, they will be passed to the Step Definition as the last argument:

  •  
  •  
@Given("^the following users exist$")
public void the_following_users_exist(DataTable users) {
// Save them in the database
}

See the DataTable API docs for details about how to access data in the table.

Substitution in Scenario Outlines

If you use a DocString or DataTable argument in steps in Scenario Outlines, any < > delimited tokens will be substituted with values from the example tables. For example:

Scenario Outline: Email confirmation
Given I have a user account with my name "Jojo Binks"
When an Admin grants me <Role> rights
Then I should receive an email with the body:
"""
Dear Jojo Binks,
You have been granted <Role> rights. You are <details>. Please be responsible.
-The Admins
"""
Examples:
| Role | details |
| Manager | now able to manage your employee accounts |
| Admin | able to manage any user account on the system |

Data Table diffing

One very powerful feature in Cucumber is comparison of tables. You can compare a table argument to another table that you provide within your step definition. This is something you would typically do in a Then step, and the other table would typically be constructed programmatically from your application’s data.

Beware that the diffing algorithm expects your data to be column-oriented, and that the first row of both tables represents column names. If your tables don’t have some similarity in the first row you will not get very useful results. The column names must be unique for each column – and they must match.

Here is an example of a Data Table that wi want to diff against actual results:

Then I should see the following cukes:
| Latin | English |
| Cucumis sativus | Cucumber |
| Cucumis anguria | Burr Gherkin |

A Step Definition can diff the DataTable with data pulled out of your application, for example from a Web page or a Database:

  •  
  •  
@Then("^I should see the following cukes:$")
public void the_following_users_exist(DataTable expectedCukesTable) {
// We'd typically pull this out of a database or a web page...
List<Cuke> actualCukes = new ArrayList();
actualCukes.add(new Cuke("Cucumis sativus", "Concombre"));
actualCukes.add(new Cuke("Cucumis anguria", "Burr Gherkin")); expectedCukesTable.diff(actualCukes)
}

The list passed to diff can be a DataTable, List<YourType>, List<Map> or a List<List<ScalarType>>.

If the tables are different, an exception is thrown, and the diff of the two tables are reported in the Report.

String Transformations

Cucumber provides an API that lets you take control over how strings are converted to other types. This is useful especially for dynamically typed languages, but also for statically typed languages when you need more control over the transformation.

Let’s consider a common example - turning a string into a date:

Given today's date is "10-03-1971"

First of all, this might mean the 10th of March in some countries, and the 3rd of October in others. It’s best to be explicit about how we want this converted. We’ll try to convert it to 10th of March 1971.

  •  
  •  

Cucumber-JVM knows how to convert strings into various scalar types. A scalar type is a type that can be derived from a single string value. Cucumber-JVM's built-in scalar types are numbers, enums, java.util.Date, java.util.Calendar and arbitrary types that have a single-argument constructor that is either a String or an Object.

Transformation to java.util.Date and java.util.Calendar will work out-of-the-box as long as the string value matches one of the SHORT, MEDIUM, FULL or LONG formats defined by java.util.DateFormat.

It turns out that 10-03-1971 from our example doesn't match any of those formats, so we have to give Cucumber a hint:

@Given("today's date is \"(.*)\"")
public void todays_date_is(@Format("dd-MM-yyyy") Date today) {
}

Many Java programmers like to use Joda Time. Cucumber-JVM doesn't have any special support for Joda Time, but since Joda's LocalDate has a LocalDate(Object) constructor it is considered a scalar by default.

However, in this case it wouldn't also know how to pass the _format_ string, so you would get an exception when Cucumber instantiates it with new LocalDate("10-03-1971").

A custom formatter gives you full control:

@Given("today's date is \"(.*)\"")
public void todays_date_is(
@Format("dd-MM-yyyy")
@Transform(JodaTransformer.class)
LocalDate today) {
}

The custom transformer looks like this:

public class JodaTransformer extends Transformer<LocalDate> {
@Override
public LocalDate transform(String value) {
String format = getParameterInfo().getFormat();
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forStyle(format);
dateTimeFormatter = dateTimeFormatter.withLocale(getLocale());
return dateTimeFormatter.parseLocalDate(value);
}
}

Of course, you can write transformers for anything, not just dates.

Data Table Transformations

This applies to Cucumber-JVM only

Data Tables can be transformed to a List of various types. We’ll see how the table in the following Scenario can be transformed to different kinds of lists.

Scenario: Some vegetables
Given I have these vegetables:
| name | color |
| Cucumber | Green |
| Tomato | Red |

List of YourType

The table can be transformed into a list of vegetables:

public class Vegetable {
public String name;
public Color color; // Color is an enum
}

The Step Definition:

@Given("I have these vegetables:")
public void I_have_these_vegetables(List<Vegetable> vegetables) {
// Do something with the vegetables
}

The header row is used to name fields in the generic List type.

IMPORTANT: If the generic List type (Vegetable in this case) is a scalar (i.e. it has a String or Object constructor), the header will not be used to name fields in the class. Instead you would get a List that has one Vegetable for each cell (6 in this case). See List of Scalar below.

List of Map

You can also transform a DataTable to a list of maps:

@Given("I have these vegetables:")
public void I_have_these_vegetables(List<Map<String, String>> vegetables) {
// Do something with the vegetables
}

The Key and Value generic types of the Map can be any kind of scalar type.

List of List of scalar

You can also convert it to a list of list scalar:

@Given("I have these vegetables:")
public void I_have_these_vegetables(List<List<String>> vegetables) {
// Do something with the vegetables
}

This will convert it to a flattened list like this:

[["name", "color"], ["Cucumber", "Green"], ["Tomato", "Red"]]

You can also convert it to a list of scalar:

List of scalar

@Given("I have these vegetables:")
public void I_have_these_vegetables(List<String> vegetables) {
// Do something with the vegetables
}

This will convert it to a flattened list like this: ["name", "color", "Cucumber", "Green", "Tomato", "Red"]

Cucumber 步骤中传Data Table作为参数的更多相关文章

  1. R中的data.table 快速上手入门

    data.table包提供了一个非常简洁的通用格式:DT[i,j,by]. 可以理解为:对于数据集DT,选取子集行i,通过by分组计算j. 对比与dplyr等包,data.table的运行速度更快. ...

  2. R之data.table速查手册

    R语言data.table速查手册 介绍 R中的data.table包提供了一个data.frame的高级版本,让你的程序做数据整型的运算速度大大的增加.data.table已经在金融,基因工程学等领 ...

  3. data.table 中的动态作用域

    data.table 中最常用的语法就是 data[i, j, by],其中 i.j 和 by 都是在动态作用域中被计算的.换句话说,我们不仅可以直接使用列,也可以提前定义诸如 .N ..I 和 .S ...

  4. 【Java学习笔记之二十七】Java8中传多个参数时的方法

    java中传参数时,在类型后面跟"..."的使用:        public static void main(String[] args){       testStringA ...

  5. 参数中传Null值

    参数中传Null值虽然不是一种优雅的方式,但有时候可以省时间.不过不推荐.

  6. CSS样式表、JS脚本加载顺序与SpringMVC在URL路径中传参数与SpringMVC 拦截器

    CSS样式表和JS脚本加载顺序 Css样式表文件要在<head>中先加载,这样网页显示时可以第一次就渲染出正确的布局和样式,网页就不会闪烁,或跳变 JS脚本尽可能放在<body> ...

  7. python接口自动化11-post传data参数案例【转载】

    前言: 前面登录博客园的是传json参数,有些登录不是传json的,如jenkins的登录,本篇以jenkins登录为案例,传data参数. 一.登录jenkins抓包 1.登录jenkins,输入账 ...

  8. python接口自动化11-post传data参数案例

    前言: 前面登录博客园的是传json参数,有些登录不是传json的,如jenkins的登录,本篇以jenkins登录为案例,传data参数. 一.登录jenkins抓包 1.登录jenkins,输入账 ...

  9. R语言数据分析利器data.table包 —— 数据框结构处理精讲

        R语言data.table包是自带包data.frame的升级版,用于数据框格式数据的处理,最大的特点快.包括两个方面,一方面是写的快,代码简洁,只要一行命令就可以完成诸多任务,另一方面是处理 ...

随机推荐

  1. JSP介绍(4)--- JSP 过滤器

    过滤器是可用于 Servlet 编程的 Java 类,可以实现以下目的: 在客户端的请求访问后端资源之前,拦截这些请求. 在服务器的响应发送回客户端之前,处理这些响应. 过滤器通过 Web 部署描述符 ...

  2. python并发编程之多进程2数据共享及进程池和回调函数

    一.数据共享 尽量避免共享数据的方式 可以借助队列或管道实现通信,二者都是基于消息传递的. 虽然进程间数据独立,但可以用过Manager实现数据共享,事实上Manager的功能远不止于此. 命令就是一 ...

  3. numpy.matlib.randn(标准正态分布)

    #网址 http://docs.scipy.org/doc/numpy/reference/generated/numpy.matlib.randn.html#numpy.matlib.randn n ...

  4. 问题:C# params类型参数;结果:C#的参数类型:params、out和ref

    C#的参数类型:params.out和ref PS:由于水平有限,难免会有错误和遗漏,欢迎各位看官批评和指正,谢谢~ 首先回顾一下C#声明一个方法的语法和各项元素,[]代表可选 [访问修饰符] 返回值 ...

  5. MyBatis构建sql时动态传入表名以及字段名

    今天项目需要用到动态表名,找到这一篇文章,亲测可用 用了mybatis很长一段时间了,但是感觉用的都是比较基本的功能,很多mybatis相对ibatis的新功能都没怎么用过.比如其内置的注解功能之类的 ...

  6. shutdown-用于关闭/重启计算机

    Linux系统下的shutdown命令用于安全的关闭/重启计算机,它不仅可以方便的实现定时关机,还可以由用户决定关机时的相关参数.在执行shutdown命令时,系统会给每个终端(用户)发送一条屏显,提 ...

  7. 网络编程中阻塞和非阻塞socket的区别

    阻塞socket和非阻塞socket 建立连接阻塞方式下,connect首先发送SYN请求道服务器,当客户端收到服务器返回的SYN的确认时,则connect返回.否则的话一直阻塞.非阻塞方式,conn ...

  8. 孙鑫VC学习系列教程

    教程简介 1.循序渐进 从Win32SDK编程开始讲解,帮助大家理解掌握Windows编程的核心 -- 消息循环机制. 2.通俗易懂 编程语言枯燥难懂,然而通过孙鑫老师形象化的讲解,Windows和M ...

  9. 文件格式——fasta格式

    fasta格式 在生物信息学中,FASTA格式(又称为Pearson格式),是一种基于文本用于表示核苷酸序列或氨基酸序列的格式.在这种格式中碱基对或氨基酸用单个字母来编码,且允许在序列前添加序列名及注 ...

  10. charles解决相应乱码问题

    Charles.ini 文件手动添加vmarg.5=-Dfile.encoding=UTF-8