3

As per cucumber 4 with TestNG:

When using TestNG in parallel mode, scenarios can be executed in separate threads irrespective of which feature file it belongs too. Different rows in a scenario outline can be also executed in separate threads. The two scenarios in feature1.feature file will be executed by two threads in two browsers. The single feature2.feature will be executed by another thread in a separate browser.

Now suppose I have a scenario like below in feature1:

1st scenario : Create an user with some details.

2nd scenario : Edit an user with some details.

Now if in TestNG if both scenario invoke at the same time then my 2nd scenario will fail for sure as the user is not created yet.

Do I just switch to Junit as:

When using JUnit in parallel mode, all scenarios in a feature file will be executed in the same thread. The two scenarios in feature1.feature file will be executed in one browser. The single feature2.feature will be executed by another thread in a separate browser.

Below function just having the parameter to run it as parallel.

@Override
@DataProvider(parallel = true)
public Object[][] scenarios() {
    return super.scenarios();
}

So my main question is how to configure my test in parallel so my test can run systematically. i.e execute parallel per feature file, or any tag which can mark scenario depended on another like we have in TestNG @Test(dependsOnMethods = { "testTwo" }).

Kindly suggest any configuration setting for cucumber or strategy which can be use for same.

Community
  • 1
  • 1
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • You can implement a "setup" and "teardown" strategy so any preconditions necessary for a test will be completed in the setup and the teardown will return the environment to a predefined starting position. I would the run each test on a different but equal test environment. – Jortega Dec 18 '19 at 13:38
  • but how this will communicate cucumber to skip it for a while, as per your suggestion suppose I create an boolean value in setup and now I will only change it value once my scenario 1 completed but cucumber will run my 2nd sceanrio of same feature and my Boolean just skip the code as value is not updated yet. can you provide me some example in case I am misunderstanding the concept – Shubham Jain Dec 18 '19 at 13:42
  • There could be a flag in a database that the 1st scenario marks and the 2nd scenario can just not run of the database flag is not set. Trying to run dependent tests in parallel is not a good idea in the first place. I would say a precondition to running tests in parallel is that the tests are independent. – Jortega Dec 18 '19 at 14:04
  • @Jortego - thanks for your comment. flag can be set anywhere, but the main question is how cucumber-jvm get to know the test are depended? – Shubham Jain Dec 18 '19 at 14:06
  • 2nd scenario can just not run of the database flag is not set - how to pass the flag to cucumber that do not start this scenario? – Shubham Jain Dec 18 '19 at 14:08
  • 1
    @ShubhamJain It is not a good idea to have dependent scenarios. U could create the whole new user in a starting step or background for the second scenario. But if u wanna go down this route, create a 'normal' testng runner that does not overwrite the scenarios method. This runner should only run this specific feature file by using tags or folder structure. – Grasshopper Dec 18 '19 at 14:16
  • I think I found something that you might be able to use. "@Before" and "@After": https://stackoverflow.com/questions/51192858/how-to-run-code-before-after-cucumber-suite – Jortega Dec 18 '19 at 14:16
  • 1
    @ShubhamJain U could also modify the parallel setting on surefire or failsafe plugin to run the multiple runner classes in parallel. – Grasshopper Dec 18 '19 at 14:17
  • @Grasshopper - Thanks for your reply.. that could be a key. can you please provide one example how to set the failsafe for same, does after specify the multiple runner testng run parallel featrue file wise? – Shubham Jain Dec 18 '19 at 14:21
  • @ShubhamJain The second comment needs to be used along with the first comment. Should have added in one comment. – Grasshopper Dec 18 '19 at 14:28
  • but if I does not override the scenarios method how I pass the @DataProvider(parallel = true) which communicate cucumber to run parallelly, this way I think I parallel execution will be happen using testng not by cucumber 4. I guess your suggestion for not making scenario dependent is better way or shifting to Junit as it will execute scenaio parallely as per different feature file not from same feature file like testng – Shubham Jain Dec 18 '19 at 14:32
  • @ShubhamJain Do u want junit behavior for all feature files or specific ones? – Grasshopper Dec 18 '19 at 14:35
  • I havnt try Junit, by theoretically as - The two scenarios in feature1.feature file will be executed in one browser. That seem that two scenario will run in same browser one after another, what I understood from it each feature file will run in separate thread and each scenario one after another . If my understanding is correct then l like to know junit behavior for all feature file so each feature run individually parallelly – Shubham Jain Dec 18 '19 at 14:39
  • @Grasshopper - kindly correct me if my understanding is not correct. – Shubham Jain Dec 18 '19 at 14:49
  • 1
    @ShubhamJain junit will give u the desired behavior.. Though driver management to keep open for whole feature file needs to be coded by you using threadlocal. – Grasshopper Dec 18 '19 at 14:51
  • yes i have implemented the threadlocal with pico dependency injection and it is working all good for me. I was having only this doubt. thanks for clarification . you can add explanation in answer .. I will accept it :) – Shubham Jain Dec 18 '19 at 14:57
  • @ShubhamJain can you please update this question with the solution, This will great help then – Rajith Kariyawsam Jul 01 '21 at 10:15

0 Answers0