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

Karate 每个场景的相同断言可以放在单独的文件中,以避免在空手道中重复?,karate,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[

这里有两个场景,一个接一个

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个类似的场景,它们的断言完全相同,我可以将其放在一个单独的文件中以避免重复并易于维护吗

如果是,那怎么办?
如果没有,那么有没有其他方法来避免空手道重复练习

我也看不到你的要求有任何变化

如果您的场景中的变化只是有效负载的话

您可以尝试使用
场景大纲:
并从
示例:
表中传递不同的有效载荷

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|
场景大纲:正面-创建折扣并搜索
给定url baseUrl+搜索
请求
当方法发布时
然后状态200
并匹配$.data.hotels[0]。事务处理\u折扣。折扣[0]。折扣\u id==折扣id
并匹配$.data.hotels[0]。事务处理\u折扣。折扣[0]。代码==couponCode
并匹配$.data.hotels[0]。交易折扣。折扣[0]。折扣价值==激励价值
并匹配$.data.hotels[0]。交易\u折扣。折扣\u售价==(售价-激励值)
示例:
|变化|
|RNP_有效载荷|
|TXP_有效载荷|
您可以在
后台:
中创建这些有效负载实例,这可以帮助您避免场景重复

如果您仍打算将其单独归档

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


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

我没有把整个代码放在一起以避免混淆,但是除了它所断言的值之外,有两种情况是不同的。我的问题是,我想把断言保存在一个单独的文件中,这可能吗?@SourabhChapali-请记住,这些是测试,而不是生产代码。复制在一定程度上是可以的。因为写断言是多么容易。根据经验,当您开始尝试添加层时,就会引入其他问题。但是,是的,如果您真的想这样做,只需将所有断言移动到一个单独的功能文件(单个场景),然后执行
调用