Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force TestNG create new instance of test class for each method if run mode is parallel="methods"

How to force TestNG create new instance of test class for each method if run mode is parallel="methods"?

JUnit does it automatically but TestNG reuses same instance between methods.

Is there any option to change this behavior?

I'm running selenium tests and create webdriver in @BeforeMethod method and store it to class variable to use it in test methods and clos on @AfterMethod.

And I want to have ability to run methods of one test class in parallel without sharing webdriver.

like image 294
Igor Sadovnikov Avatar asked Mar 26 '13 08:03

Igor Sadovnikov


People also ask

How do you run test classes in parallel using TestNG?

To trigger parallel test execution in TestNG, i.e., run tests on separate threads, we need to set the parallel attribute. This attribute accepts four values: methods – runs all methods with @Test annotation in parallel mode. tests – runs all test cases present inside <test> tag in the XML in parallel mode.

Does TestNG run tests in parallel?

TestNG allows the tests to run in parallel or concurrent mode. This means that based on the test suite configuration, different threads are started simultaneously and the test methods are executed in them.


2 Answers

There is no way to force testng to do that. A solution is to set webdriver instance to a threadlocal variable. What this would help doing is, it would create one webdriver object per thread, if u do a get on the variable, it. Would give u that thread's object only.

like image 72
niharika_neo Avatar answered Sep 28 '22 11:09

niharika_neo


Checkout Factory

From javadoc:

Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[].
like image 22
rajesh Avatar answered Sep 28 '22 12:09

rajesh