我也可以对 Json Api 请求使用以下相同的功能吗

Can I use the same below feature for the Json Api request aswell

Xmlsample.feature
Feature: test A

Scenario:  test apple
    * table test_apple_one

    | payload_file              |
    | 'sample/ball.xml'         |
* def result = call read('classpath:........./samplereq.feature')  test_apple_one       



    Jsonsample.feature
Feature: test B

Scenario:  test Mango
    * table test_Mango_one

    | payload_file              |
    | 'sample/cat.`enter code here`json'         |



samplereq.feature

Feature: sample

  Background:
#Common Config load  
* def sampleURL = baseURL+'/sample/test'      
* xml payload = read('classpath:.....'+payload_file)   
#OAuth Signature generator
* def authorization = "oauth string"

  Scenario: Make the sample API call
Given url sampleURL`enter code here`
Given header Authorization = authorization
Given request payload
And header Content-Type = 'application/xml;charset=UTF-8'
And header Host = host
And header Accept = content_type
When method post    
Then match header Content-Type contains 'application/xml' 

我正在为 XML API 请求使用上述功能 samplereq.feature 并且 我想将此功能 samplereq.feature 保留为通用功能,并将其用于 Json Api request.can 我对 JSON(Jsonsample.feature ) API 请求,请建议

我看了你的问题好几遍,很抱歉我完全不明白你想做什么。另外我认为你没有正确理解空手道。所以请听我的建议,并以正确的精神接受它。

我认为您是在试图不必要地使您的测试复杂化。我真诚的建议是——请不要尝试这种极端的重复使用。我观察到,当团队试图创建一个超级通用的可重复使用的测试脚本时 - 它只会使事情复杂化并且变得难以维护。

所以请为 JSON 和 XML 使用不同的特征文件。对于每个测试,您可以有多个场景。现在,JSON 和 XML 的场景数据可以相同,您可以读取一个通用的 JSON 文件。请参阅此示例,了解如何从 JSON 中创建 XML:https://github.com/intuit/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/xml/xml.feature

如果您仍然坚持通用特征中的JSON和XML,请看这个例子:https://github.com/intuit/karate/tree/master/karate-demo/src/test/java/demo/loopcall

上面还有一个调用JavaScript函数的例子。另外请仔细阅读文档。