Add Ability to support a new instance for every test method
TestNG Version
Latest
Expected behavior
Each test can run without interference from other tests
Actual behavior
When using member variables tests can modify state. Particularly when parallel=methods @BeforeMethod methods become useless without ThreadLocal storage (which clutters code)
Test case sample
Running this with parallel=methods will cause failures. This is the simplest case, but on real world it's much harder to track down.
public class ParallelTest {
private int member;
private ThreadLocal<Integer> expected = new ThreadLocal<>();
@BeforeMethod
public void setup() {
member = ThreadLocalRandom.current().nextInt(1, 51);
expected.set(member);
}
@Test
public void first() throws Exception {
Thread.sleep(ThreadLocalRandom.current().nextLong(100, 800));
System.out.println(member);
Assert.assertEquals(member, expected.get().intValue(), "member variable and thread local do not match");
}
@Test
public void second() throws Exception {
Thread.sleep(ThreadLocalRandom.current().nextLong(100, 800));
System.out.println(member);
Assert.assertEquals(member, expected.get().intValue(), "member variable and thread local do not match");
}
@juherr - Based on these comments from https://github.com/cbeust/testng/pull/1873#issuecomment-408962987 do you think we can go ahead and have this issue closed out ?
@krmahadevan I think the feature request is still valid but not its first implementation. BTW JUnit5 added the 1 instance by class feature, we can add the 1 instance by method feature too :p