testng icon indicating copy to clipboard operation
testng copied to clipboard

Add Ability to support a new instance for every test method

Open jasoncorbett opened this issue 7 years ago • 2 comments

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");
    }

jasoncorbett avatar Jul 23 '18 22:07 jasoncorbett

@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 avatar Aug 09 '18 08:08 krmahadevan

@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

juherr avatar Aug 09 '18 09:08 juherr