Karate报告中如何展示被调用feature文件的详细步骤?
问题解决:让Karate报告展示被调用feature的步骤
核心问题原因
Karate默认仅展示当前执行的主feature步骤:callonce/call调用的子feature步骤会被折叠,afterFeature钩子内的执行逻辑则不会被纳入主报告统计。
具体解决方法
1. 让preTest.feature的步骤显示在报告中
方法一:开启报告详细展示配置
在tests.feature开头添加报告配置,强制展开所有子步骤:
Feature: Test suite test cases * configure report = { showAllSteps: true, showLog: true } Background: * print 'all test cases' * def dataReturnedFromPreSetupTests = callonce read('classpath:preTest.feature') * configure afterFeature = function(){ karate.call('classpath:postTest.feature', preSetupData); } # 原有Scenario...
添加后,报告中callonce对应的步骤会出现展开按钮,点击即可查看preTest.feature的所有详细步骤。
方法二:将preTest作为独立测试执行
通过测试套件(如JUnit/TestNG)按顺序执行preTest→tests→postTest,每个feature的步骤会在报告中单独展示。以JUnit 5为例:
class TestSuite { @Karate.Test Karate preTest() { return Karate.run("preTest").relativeTo(getClass()); } @Karate.Test Karate tests() { return Karate.run("tests").relativeTo(getClass()); } @Karate.Test Karate postTest() { return Karate.run("postTest").relativeTo(getClass()); } }
2. 让postTest.feature的步骤显示在报告中
afterFeature钩子属于后台执行逻辑,默认不生成报告步骤,可通过两种方式解决:
方法一:将postTest作为独立后置测试执行
沿用上述测试套件方案,将postTest放在最后执行即可。
方法二:将postTest调用移到主feature的Scenario中
把清理逻辑从钩子中移出,作为tests.feature的最后一个Scenario执行:
Feature: Test suite test cases * configure report = { showAllSteps: true, showLog: true } Background: * print 'all test cases' * def dataReturnedFromPreSetupTests = callonce read('classpath:preTest.feature') Scenario: Test case 1 Given url URL When method 'GET' Then status == 200 Scenario: Test case 2 Given url URL When method 'GET' Then status == 200 Scenario: Post test cleanup * call read('classpath:postTest.feature')
此时postTest的步骤会作为一个独立Scenario展示在tests.feature的报告中。
额外注意
不要在Background中用When等Gherkin关键字包裹callonce,Karate中call/callonce属于动作语句,直接用*定义即可,添加Gherkin关键字不会改变报告逻辑,反而不符合语法规范。
内容的提问来源于stack exchange,提问作者Madhu Gupta
相关产品推荐
相关产品推荐

