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.
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.
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.
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.
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[].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With