1

I have a CRUD test case for an entity (eg Area1) defined in a scenario/feature file However i want to call this feature file for creating another entity (eg Area2). Area2 first needs an Area1 to be created, so i would need to call this Area1.feature file.

But Area1.feature also contains the Delete step, how can I effectively call this Area1.feature in Area2, but all clean the created Area1 at the end of the test?

Scenario: Create a site

Given url baseUrlSuite
And path 'sites'
And request siteupdated
And header Authorization = 'Bearer ' + tokenSuite
When method POST
Then status 201
And match response contains siteupdated

* def siteId = response.id
Then print 'siteId---',siteId
* def siteName = response.name
Then print 'siteName---',siteName
* def siteEtag = responseHeaders.ETag
Then print 'siteEtag---',siteEtag

Delete the created site in suite

Given url baseUrlSuite
And path 'sites' + '/' + siteId
And header Authorization = 'Bearer ' + tokenSuite
And header If-Match = siteEtag 
When method DELETE
Then status 204
Cerena
  • 43
  • 7

1 Answers1

0

Here's my very sincere recommendation. Don't attempt to re-use code.

Explained in great detail here: https://stackoverflow.com/a/54126724/143475

So just write a set of Scenario-s and it is fine to have some code-duplication. Especially in test-scripts.

That said, if you tried hard enough, you can maintain a bunch of Scenarios that only do GET, POST, DELETE etc. and then call them from here and there and worry about making them "generic" and how to pass "arguments" or parameterize them. In my experience it just creates more trouble than it is worth.

For how to call other Scenario-s refer: https://stackoverflow.com/a/46375154/143475

And if you want to make your tests "smarter" (often a bad idea) for e.g. to call the "delete" routine only if some condition is true, refer: https://stackoverflow.com/a/50350442/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248