How to force TestNG create new instance of test cl

2020-02-26 06:38发布

问题:

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.

回答1:

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.



回答2:

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[].


回答3:

I've used TestNG to run multiple selenium webdriver tests in parallel. To set up a new WebDriver for each method, use the @DataProvider annotation and its associated attribute in the @Test annotation.