空手道API测试-在功能文件中跨方案访问变量值 [英] Karate API Testing - Access variable value across scenario in a feature file
问题描述
I am running into this situation (similar to this) when trying to do the following:
Scenario: Create User
Given path 'user'
And request read('user.json');
When method post
Then status 200
And def user_id = response.userId
Scenario: Test A
Given path 'user/' + user_id <- Received javascript error here
...
Scenario: Test B
Given path 'user/' + user_id <- Received javascript error here
...
Scenario: Test A
Given path 'user/' + user_id <- Received javascript error here
...
基本上,我想做的是首先在数据库中创建一个用户,然后通过一系列测试运行它,并使用最后一个方案删除该用户.因此,我需要在多个方案中共享user_id值.后台不适用于我,因为这意味着我必须为每种情况创建一个新用户.我在演示中看到,一种简单的方法是将所有测试置于1种情况下,但我认为将所有测试置于1种情况下是不正确的
Basically what i am trying to do is create a user in my database first, then run it through a series of test and use a last scenario to delete this user. Therefore, i need to share the user_id value across multiple scenarios. Background doesn't work for me because that means i have to create a new user for each scenario. I have seen in the demo that a simple way would be to put all test under 1 scenario but i don't feel that it is right to put all tests on 1 scenario
我已经查看了空手道演示,但是没有遇到任何可以帮助我解决问题的示例代码.我可以在空手道中知道正确的方法吗?谢谢.
I have reviewed the karate demo but i did not come across any sample code that will help my situation. May i know the right way to do this in Karate? Thanks.
推荐答案
我认为您缺少 c0> 关键字.了解它,然后看一下示例,例如:
I think you are missing the callonce
keyword. Understand it and then look at the demos, for example, this one: callonce.feature
.
您将需要将通用"代码移至一个单独的功能中,但这是正确的做法,因为您通常也希望多个功能文件也可重复使用该代码.
You will need to move the 'common' code into a separate feature, but that is the right thing to do, because you will typically want it re-usable by multiple feature files as well.
这篇关于空手道API测试-在功能文件中跨方案访问变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!