可以将每个方案的相同断言放在单独的文件中,以避免空手道重复?

时间:2018-10-25 08:34:31

标签: karate

这是两种情况,一个接一个

Scenario: Positive - Create a discount with ABSOLUTE discount and ROOM_NIGHT_PRICE and search 

Given url baseUrl + SEARCH
And request changes
When method post
Then status 200 

And match $.data.hotels[0].transaction_discount.discounts[0].discount_id == discountId
And match $.data.hotels[0].transaction_discount.discounts[0].code == couponCode
And match $.data.hotels[0].transaction_discount.discounts[0].discount_value ==  incentive_value
And match $.data.hotels[0].transaction_discount.discounted_sell_price == (sellPrice-incentive_value)

Scenario: Positive - Create a discount with ABSOLUTE discount and TRANSACTION_PRICE and search 

Given url baseUrl + SEARCH
And request changes
When method post
Then status 200 

And match $.data.hotels[0].transaction_discount.discounts[0].discount_id == discountId
And match $.data.hotels[0].transaction_discount.discounts[0].code == couponCode
And match $.data.hotels[0].transaction_discount.discounts[0].discount_value ==  incentive_value
And match $.data.hotels[0].transaction_discount.discounted_sell_price == (sellPrice-incentive_value)

如果您发现这些场景的断言相同,那么我有20个场景具有完全相同的断言,是否可以将其放在单独的文件中以避免重复和易于维护?

如果是,那么如何? 如果否,那么还有其他方法可以避免空手道重复

1 个答案:

答案 0 :(得分:1)

我也看不到您的请求有任何变化。

如果您的方案中只有变化是有效载荷

您可以尝试使用Scenario Outline: 并从Examples:表中传递不同的有效载荷

Scenario Outline: Positive - Create a discount and search 
 Given url baseUrl + SEARCH 
 And request <changes> 
 When method post 
 Then status 200 
 And match $.data.hotels[0].transaction_discount.discounts[0].discount_id == discountId      
 And match $.data.hotels[0].transaction_discount.discounts[0].code == couponCode  
 And match $.data.hotels[0].transaction_discount.discounts[0].discount_value == incentive_value  
 And match $.data.hotels[0].transaction_discount.discounted_sell_price == (sellPrice-incentive_value)
Examples:
|  changes  |
|RNP_PAYLOAD|
|TXP_PAYLOAD|

您可以在Background:中创建这些有效负载实例,这可以帮助您避免场景重复。

OR

如果您仍然打算将此文件保存在单独的文件中

您可以创建一个将预期和实际JSON都作为输入的功能文件,并在其中执行匹配操作。

然后,您可以在所有场景中调用该功能文件,并将值传递给调用功能。