简体   繁体   English

使用Spock进行测试-使用复杂的数据提供程序

[英]Testing with Spock - use a complex data provider

Spock can run parametrized tests using the Data Pipes construct: Spock可以使用Data Pipes构造运行参数化测试:

...
where:
a << [1, 7, 0]

It's very convenient for lists of values or one-liners that generate some custom list (eg a << (0..9).collect({it*it}) But what's a convenient, readable way of providing a complex generated list as a parameter? A list that can't be easily generated in a single line of code. 对于生成某些自定义列表的值列表或单行列表(例如a << (0..9).collect({it*it})这很方便,但是提供一种复杂的生成列表的便捷,易读的方式是参数吗?不能在一行代码中轻易生成的列表。

Edit: I realize that I use a separate static method to initiate the list, but that feels hackish, and I'm wondering whether there is a way of generating the parameters somewhere within the test itself. 编辑:我意识到我使用了一个单独的静态方法来初始化列表,但是感觉有点黑,我想知道是否有一种方法可以在测试本身内的某个位置生成参数。 The where clause itself doesn't allow anything of that sort. where子句本身不允许任何此类操作。

In case you mean complex list as list of objects, you can always break the objects to the relevant fields you are using in the test. 如果您将复杂列表作为对象列表,则始终可以将对象分解为测试中使用的相关字段。

For example: If you need a list of students, each student has the following data: 例如:如果您需要一个学生列表,则每个学生都有以下数据:

class Student {
    String name
    String lastname
    Int age
    String id
}

In the test you can build each object by divide it to its fields. 在测试中,您可以通过将每个对象划分为各个字段来构建对象。 So use the where section like that: 因此,使用如下所示的where部分:

  where:
    name    | lastname | age | id
    "Ron"   | "Black"  | 34  | "2345"
    "Tom"   | "White"  | 26  | "8765"

Using that syntax enable you to have different object in every iteration, as defined in the where section. 使用语法可以使您在每次迭代中都有不同的对象,如where部分所定义。

I hope that is what you are looking for, because your question can be understood in different ways. 我希望这就是您要寻找的,因为您的问题可以通过不同的方式理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM