Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
801 views
in Technique[技术] by (71.8m points)

karate - Dynamic scenario freezes when called using afterFeature hook

Strange behaviour when I call a feature file for test clean using afterFeature hook. The cleanup feature file is called correctly because I can see the print from Background section of the file, but for some reason the execution hangs for Scenario Outline.

I have tried running feature with Junit5 runner and also in IntelliJ IDE by right clicking on feature file but get the same issue, the execution hangs.

This is my main feature file:

Feature: To test afterFeature hook

  Background:
    * def num1 = 100
    * def num2 = 200
    * def num3 = 300

    * def dataForAfterFeature =
    """
    [
      {"id":'#(num1)'},
      {"id":'#(num2)'},
      {"id":'#(num3)'}
    ]
    """
  * configure afterFeature = function(){ karate.call('after.feature'); }

  Scenario: Test 1
    * print 'Hello World 1'

  Scenario: Test 2
    * print 'Hello World 2'

The afterFeature file:

@ignore

Feature: Called after calling feature run is completed

  Background:
    * def dynamicData = dataForAfterFeature
    * print 'dynamicData: ' + dynamicData

  Scenario Outline: Print dynamic data
    * print 'From after feature for id: ' + <id>

    Examples:
    | dynamicData |

The execution stalls at Scenario Outline. I can see the printed value for dynamicData variable in console but nothing happens after that.

Seems like the outline loop is not starting or has crashed? Was not able to get details from log as the test has not finished or there is no error reported. What else can I check or what might be the issue?

If not easily reproducible, what test cleanup workaround do you recommend?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yeah I personally recommend a strategy to pre-clean-up always, because you cannot guarantee that an "after" hook gets called, e.g. if the machine is switched off.

Sometimes the simplest option is to do this as plain old Java code in your JUnit test-suite. So maybe a one-line after using Runner is sufficient.

It gets tricky if you need to keep track of dynamic data that your tests have created. What I would do is write a Java singleton, use it in your tests to "collect" the ID-s that need to be deleted, and then use this in your JUnit class. You can use things like @AfterClass.

Please try and replicate using the instructions here: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue - because this can indeed be a bug with Scenario Outline.

Finally, you can evaluate ExecutionHook which has an afterSuite() callback: https://github.com/intuit/karate/issues/970#issuecomment-557443551


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...