Tests pass when run individually but not when the whole test class run

后端 未结 5 1350
感动是毒
感动是毒 2021-01-02 04:38

I have solved a topCoder problem for which all the tests pass when I run them on their own. Nonetheless when I run the whole test class some of them fail. Could you, please,

5条回答
  •  死守一世寂寞
    2021-01-02 05:06

    You are sharing a single instance of the class under test across all tests. I'd remove the initial assignment and add this:

    private GameOfStones gameOfStones; // Don't create an instance here
    
    @BeforeMethod
    public void setUp() {
        gameOfStones = new GameOfStones();
    }
    

    ... which will use a new instance for each test. Good practice would also be to clean up after each test:

    @AfterMethod
    public void tearDown() {
        gameOfStones = null;
    }
    

    In the example given here, fixing the class scoped variable causing the problem to be method scoped instead would also fix the issue, but as the software under test gets more complex it's good to start doing proper test set up and tear down.

提交回复
热议问题