Karate API Testing - Access variable value across scenario in a feature file

和自甴很熟 提交于 2019-12-10 23:05:49

问题


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
   ...

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.


回答1:


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.



来源:https://stackoverflow.com/questions/47629904/karate-api-testing-access-variable-value-across-scenario-in-a-feature-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!