如何为空手道中的数据驱动测试准备嵌套数据结构? [英] How to prepare a nested data structure for a data-driven test in Karate?

查看:32
本文介绍了如何为空手道中的数据驱动测试准备嵌套数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用 junit5、wiremock 和 restassured 进行集成测试.空手道看起来很有前途,但我在数据驱动测试的设置上有点挣扎,因为我需要准备一个嵌套的数据结构,在当前设置中,它如下所示:

抽象类 StationRequests(val station: Collection): ArgumentsProvider {覆盖 fun provideArguments(context: ExtensionContext): java.util.stream.Stream{val now = LocalDateTime.now()val 样本 = mutableListOf()station.forEach { 站 ->Subscription.values().forEach { 订阅 ->列表(*Device.values(),空值).forEach { 设备 ->Stream.Protocol.values().forEach { 协议 ->列表(空值,现在减去分钟(5),now.minusHours(2),现在减去天数(1)).forEach { startTime ->样品.添加(参数.of(订阅、设备、站点、协议、开始时间))}}}}}返回 java.util.stream.Stream.of(*samples.toTypedArray())}}

有没有什么首选的方法可以用空手道设置这样的嵌套数据结构?我最初想定义 5 个不同的数组,其中包含订阅、设备、站、协议和开始时间的样本值,并将它们组合并合并到一个数组中,该数组将在 Examples: 部分中使用.

到目前为止我还没有成功,我想知道是否有更好的方法来准备这种嵌套的数据驱动测试?

解决方案

除非绝对必要,否则我不建议嵌套.您可以将您的排列展平"为一个表格,如下所示:

I currently use junit5, wiremock and restassured for my integration tests. Karate looks very promising, yet I am struggling with the setup of data-driven tests a bit as I need to prepare a nested data structures which, in the current setup, looks like the following:

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())
    }
}

Is there any preferred way how to setup such nested data structures with karate? I initially thought about defining 5 different arrays with sample values for subscription, device, station, protocol and startTime and to combine and merge them into a single array which would be used in the Examples: section.

I did not succeed so far though and I am wondering if there is a better way to prepare such nested data driven tests?

解决方案

I don't recommend nesting unless absolutely necessary. You may be able to "flatten" your permutations into a single table, something like this: https://github.com/intuit/karate/issues/661#issue-402624580

That said, look out for the alternate option to Examples: which just might work for your case: https://github.com/intuit/karate#data-driven-features

Here's a simple example:

Feature:

Scenario:
* def data = [{ rows: [{a: 1},{a: 2}] }, { rows: [{a: 3},{a: 4}] }]
* call read('called.feature@one') data

and this is: 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

This is how it looks like in the new HTML report (which is in 0.9.6.RC2 and may need more fine tuning) and it shows off how Karate can support "nesting" even in the report, which Cucumber cannot do. Maybe you can provide feedback and let us know if it is ready for release :)

这篇关于如何为空手道中的数据驱动测试准备嵌套数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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