首页 > 解决方案 > 从一个功能设置空手道中的全局变量以用于另一个功能

问题描述

问题

我有 3 个不同的功能文件。一个用于 EndToEndTest.feature(集成测试),一个用于 create-service.feature(基本 api 调用),另一个用于 create-tariff(需要 create-service.feature 响应的 api 调用)

EndToEndTest.feature

Feature: End To End Standard

  Background:
  * def result = call read('../get-user-token.feature')
  * def service = call read('create-service.feature')
  * def location = service.responseHeaders['Location'][0]
  * def serviceId = location.substring(location.lastIndexOf('/') + 1)

  # I want serviceId to be set globally so I can access it into the create-tariff.feature

  * def tariff = call read('create-tariff.feature')

创建关税特征

Feature: Create Tariff

  Background:
  * def result = call read('../../get-user-token.feature')
  * def service = serviceId

很明显,这里的第二个 def (service=serviceid) 将失败,因为它将无法从第一个获取 serviceId 集

我的问题是,从 EndToEndTest.feature 传递一个变量(serviceId)以分配给 create-tariff.feature 中的另一个变量(服务)的最佳方法是什么

标签: dslkarate

解决方案


使功能相互依赖是一种非常糟糕的做法,我不建议这样做。你甚至不应该让Scenario-s 依赖于其他的。

请阅读:https ://github.com/intuit/karate#script-structure

请注意,您有钩子- 特别karate.callSingle()是但它仅适用于您登录并获取令牌然后需要重新使用它的情况。


推荐阅读