This is a practical scenario we are facing in using karate for testing -

We have a graph API which we test using karate.

Imagine this - each karate feature creates a parent and child nodes in graph to validate certain business logic.

Then imagine that there's a suite of 100 tests which means 100 small graphs are created (database records).

We want to cleanup all of these 100 small graphs to avoid polluting the system with stale test data.

Could anyone suggest a cleanup strategy here? AfterFeature which is supposed to help in cleanup is of no use because there's no state/variable sharing between any 2 features i.e. we would never know the IDs of the parents/children who were created in previous feature

Assigning values to a global variable(defined in Background section) in each Scenario also gets reset and can not maintain IDs to be cleaned up.

3 Answers

1
Adrien On

You can call afterFeature in each feature to delete the corresponding graph. I don't really see what's blocking you from doing that?

Or you can cleanup by calling some code in Java, after the tests are completed, to delete all the graphs. If you create a variable in karate-config.js, I guess you could add the ID of each node you create to this variable, in order to know which ones to delete after all the tests have passed.

1
Peter Thomas On

Personally I recommend a strategy to delete before start.

But my suggestion is use a Java singleton which you can neatly init via karate-config.js:

config.SuiteUtils = Java.type('com.myco.SuiteUtils')

In each scenario, you can call methods on it.

* eval SuiteUtils.addId('someId')

And then you can grab it in the JUnit runner (since it is a singleton) and do whatever you want.

0
Kuldip Patel On

Facing same issue. Is there a way to pass parameter to AfterScenario?

Because, In afterscenario whatever ID we set in config variable or background variable is being reset.

If I clean all the graphs in afterScenario or afterFeature, then I can not do parallelization.

Also, In your example above , this statement written in karate-config.js gives error. "config.SuiteUtils = Java.type('com.myco.SuiteUtils') "