Any real word example about how setUp() and tearDown() should be used in PHPUnit?

前端 未结 4 1552
温柔的废话
温柔的废话 2021-02-02 08:27

Methods setUp() and tearDown() are invoked before and after each test. But really, is there any real word example about why should I need this?

4条回答
  •  走了就别回头了
    2021-02-02 08:43

    There is a memory leak in provided example in accepted answer. You should add tearDown:

    protected function tearDown()
    {
        $this->_mockedService = null;
    }
    

    PHPUnit creates new test case object for every test method call. So if there are 4 test method's - there are will be 4 objects, and 4 mockedService's will be created. And they wouldn't removed until the end of the script (entire test suite). So you need to delete all objects and unset all variables in tearDown to prevent memory leak.

提交回复
热议问题