如何为空手道中的数据驱动测试准备嵌套数据结构?
How to prepare a nested data structure for a data-driven test in Karate?
我目前使用 junit5、wiremock 和 restassured 进行集成测试。空手道看起来很有前途,但我在设置数据驱动测试方面遇到了一些困难,因为我需要准备一个嵌套数据结构,在当前设置中,它如下所示:
abstract class StationRequests(val stations: Collection<String>): ArgumentsProvider {
override fun provideArguments(context: ExtensionContext): java.util.stream.Stream<out Arguments>{
val now = LocalDateTime.now()
val samples = mutableListOf<Arguments>()
stations.forEach { station ->
Subscription.values().forEach { subscription ->
listOf(
*Device.values(),
null
).forEach { device ->
Stream.Protocol.values().forEach { protocol ->
listOf(
null,
now.minusMinutes(5),
now.minusHours(2),
now.minusDays(1)
).forEach { startTime ->
samples.add(
Arguments.of(
subscription, device, station, protocol, startTime
)
)
}
}
}
}
}
return java.util.stream.Stream.of(*samples.toTypedArray())
}
}
有什么首选方法可以用空手道设置这种嵌套数据结构吗?我最初考虑定义 5 个不同的数组,其中包含订阅、设备、站点、协议和 startTime 的示例值,并将它们组合并合并到一个数组中,该数组将在 Examples:
部分中使用。
到目前为止我还没有成功,我想知道是否有更好的方法来准备这种嵌套的数据驱动测试?
除非绝对必要,否则我不建议嵌套。您可以 "flatten" 您的排列成一个 table,像这样:https://github.com/intuit/karate/issues/661#issue-402624580
也就是说,寻找 Examples:
的替代选项,它可能适合您的情况:https://github.com/intuit/karate#data-driven-features
这是一个简单的例子:
Feature:
Scenario:
* def data = [{ rows: [{a: 1},{a: 2}] }, { rows: [{a: 3},{a: 4}] }]
* call read('called.feature@one') data
这是:called.feature
:
@ignore
Feature:
@one
Scenario:
* print 'one:', __loop
* call read('called.feature@two') rows
@two
Scenario:
* print 'two:', __loop
* print 'value of a:', a
这就是它在 new HTML report 中的样子(它在 0.9.6.RC2 中,可能需要更多微调),它展示了空手道如何支持 "nesting",即使在报告,这是 Cucumber 做不到的。也许您可以提供反馈并让我们知道它是否准备好发布 :)
我目前使用 junit5、wiremock 和 restassured 进行集成测试。空手道看起来很有前途,但我在设置数据驱动测试方面遇到了一些困难,因为我需要准备一个嵌套数据结构,在当前设置中,它如下所示:
abstract class StationRequests(val stations: Collection<String>): ArgumentsProvider {
override fun provideArguments(context: ExtensionContext): java.util.stream.Stream<out Arguments>{
val now = LocalDateTime.now()
val samples = mutableListOf<Arguments>()
stations.forEach { station ->
Subscription.values().forEach { subscription ->
listOf(
*Device.values(),
null
).forEach { device ->
Stream.Protocol.values().forEach { protocol ->
listOf(
null,
now.minusMinutes(5),
now.minusHours(2),
now.minusDays(1)
).forEach { startTime ->
samples.add(
Arguments.of(
subscription, device, station, protocol, startTime
)
)
}
}
}
}
}
return java.util.stream.Stream.of(*samples.toTypedArray())
}
}
有什么首选方法可以用空手道设置这种嵌套数据结构吗?我最初考虑定义 5 个不同的数组,其中包含订阅、设备、站点、协议和 startTime 的示例值,并将它们组合并合并到一个数组中,该数组将在 Examples:
部分中使用。
到目前为止我还没有成功,我想知道是否有更好的方法来准备这种嵌套的数据驱动测试?
除非绝对必要,否则我不建议嵌套。您可以 "flatten" 您的排列成一个 table,像这样:https://github.com/intuit/karate/issues/661#issue-402624580
也就是说,寻找 Examples:
的替代选项,它可能适合您的情况:https://github.com/intuit/karate#data-driven-features
这是一个简单的例子:
Feature:
Scenario:
* def data = [{ rows: [{a: 1},{a: 2}] }, { rows: [{a: 3},{a: 4}] }]
* call read('called.feature@one') data
这是:called.feature
:
@ignore
Feature:
@one
Scenario:
* print 'one:', __loop
* call read('called.feature@two') rows
@two
Scenario:
* print 'two:', __loop
* print 'value of a:', a
这就是它在 new HTML report 中的样子(它在 0.9.6.RC2 中,可能需要更多微调),它展示了空手道如何支持 "nesting",即使在报告,这是 Cucumber 做不到的。也许您可以提供反馈并让我们知道它是否准备好发布 :)