Karate 空手道-如何在调用的功能中配置afterScenario钩子?

Karate 空手道-如何在调用的功能中配置afterScenario钩子?,karate,Karate,我有以下用例。顶层功能文件(test1.feature)调用另外两个功能文件(test2.feature、test3.feature),如下所示: test1.1功能: Feature: Background: * print 'this is a background 1' Scenario: test1 * print 'this is test1' * call read('test2.feature') * call read('test3.featu

我有以下用例。顶层功能文件(test1.feature)调用另外两个功能文件(test2.feature、test3.feature),如下所示:

test1.1功能:

Feature:

Background:
    * print 'this is a background 1'

Scenario: test1
    * print 'this is test1'
    * call read('test2.feature')
    * call read('test3.feature')
Feature:

Background:
    * print 'this is a background 1'
    * def testIds = [222, 333]
    * call read('../resources/write-test-results.js') testIds

Scenario: test1
    * print 'this is test1'
    * call read('test2.feature')
    * karate.write({ id: testId, errorMessage: null }, 'test-id-' + testId + '.json')

    * call read('test3.feature')
test2.1功能:

Feature:

Background:
    * print 'this is a background 2'
    * def testId = 222
    * configure afterScenario = function(){ karate.write({ id: testId, errorMessage: karate.info.errorMessage }, 'test-id-' + testId + '.json'); }

Scenario: test2
    * print 'this is test2'
test3.1功能:

Feature:

Background:
    * print 'this is a background 3'
    * def testId = 333
    * configure afterScenario = function(){ karate.write({ id: testId, errorMessage: karate.info.errorMessage }, 'test-id-' + testId + '.json'); }

Scenario: test3
    * print 'this is test3'
我意识到不建议写入文件,但在这种特殊情况下,我需要捕获测试结果以进行执行后处理(说来话长!)。我还知道,每个
功能/场景
都会加载
背景
。因此,一旦调用的特性(test1.feature)调用第一个被调用的特性(test2.feature),它的
Background
就会接管。这就是我希望调用被调用特性中的
afterScenario
hook的地方,但是由于调用特性(test1.feature)尚未完成,因此它继续调用最后一个特性(test3.feature)。类似地,上次调用的特性(test3.feature)中的
Background
也会被加载,由于调用特性(test1.feature)现在已经完成,它会调用上次调用的特性(test3.feature)中的
afterScenario
钩子,因此,只有test-id-333.json文件被写入
目标
文件夹。以下是打印输出:

23:53:49.613 [ForkJoinPool-1-worker-1] INFO  com.intuit.karate - [print] this is a background 1
23:53:49.617 [ForkJoinPool-1-worker-1] INFO  com.intuit.karate - [print] this is test1
23:53:49.631 [ForkJoinPool-1-worker-1] INFO  com.intuit.karate - [print] this is a background 2
23:53:49.637 [ForkJoinPool-1-worker-1] INFO  com.intuit.karate - [print] this is test2
23:53:49.652 [ForkJoinPool-1-worker-1] INFO  com.intuit.karate - [print] this is a background 3
23:53:49.654 [ForkJoinPool-1-worker-1] INFO  com.intuit.karate - [print] this is test3
我知道这是预期的行为,但我想知道是否有方法在被调用的特性(test2.feature)中调用
afterScenario
hook,即使调用的特性(test1.feature)还有工作要做。 如果我将test1.feature转换为
场景大纲
,那么
test-id-222.json
test-id-333.json
两个文件都会被写入,因为现在它们都是单独的场景,但不幸的是,这个选项在这个用例中不可行

如果在这个场景之后有一个
,或者有一个参数要传递给一个被调用的特性,即
*调用read('test2.feature')和afterhook
,这将确保调用after hook,即使调用特性仍然没有完成


提前感谢您的建议

这设计起来有点棘手,因为您甚至可以在全局
karate config.js中执行此操作:

karate.configure('afterScenario', function(){ karate.log('after scenario') });
我们还有一条规则,赛后
afterScenario
只适用于“顶级”功能,而不会对“所谓”功能执行任何操作

因此,我提出了两种选择

  • 我们有一个
    RuntimeHook
    (以前被称为)的概念,但是您需要编写一些Java代码,这是一些没有文档记录的代码,面向高级用户。但是你 具有完全控制权,可以读取运行时元数据,如功能名称、场景名称等

  • 定义一个可重用函数,该函数将
    测试id
    作为参数,并在每个代码块的末尾手动调用它。但我想我明白你的意思了,你可能想让它开火,即使有错误。您可以提交我一直在考虑的
    onError
    hook的特性请求


  • 我解决这个问题的方法如下:

    test1.1功能:

    Feature:
    
    Background:
        * print 'this is a background 1'
    
    Scenario: test1
        * print 'this is test1'
        * call read('test2.feature')
        * call read('test3.feature')
    
    Feature:
    
    Background:
        * print 'this is a background 1'
        * def testIds = [222, 333]
        * call read('../resources/write-test-results.js') testIds
    
    Scenario: test1
        * print 'this is test1'
        * call read('test2.feature')
        * karate.write({ id: testId, errorMessage: null }, 'test-id-' + testId + '.json')
    
        * call read('test3.feature')
    
    write-test-results.js(位于测试/参考资料中):

    由于调用了
    后台
    部分中的write-test-results.js函数

    • 然后,下一个“调用”功能(test2.feature)将重新加载
      后台文件
      ,如果通过,则“顶级”功能将用以下行覆盖test-id-222.json文件:
    • 最后,它将调用最后一个“被调用”的特性(test3.feature),由于它的
      后台将接管,
      afterScenario
      钩子将用实际的测试结果覆盖test-id-333.json文件
    因此,如果test2.feature失败,test-id-222.json文件将被实际错误覆盖,test-id-333.json将保持不变,并反映test3.feature的结果,即
    跳过

    {"id":333,"errorMessage":"Skipped"}
    

    彼得,谢谢你的及时回复!我已经提交了一个
    onError
    hook here->的功能请求,同时,我将查看
    RuntimeHook
    @trebor请将答案标记为“已接受”或至少向上投票!
    {"id":333,"errorMessage":"Skipped"}