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

前端 未结 4 1555
温柔的废话
温柔的废话 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:50

    You can instantiate a bunch of fixture objects and have them available as instance variables in each test instead of constructing them individually for each test.

    You can create resources like a file handle in the setUp then make sure you close them in tearDown. If you're writing temporary files, you can make sure you delete them. If you open a database connection, you can close it (though you might want to do that elsewhere - setupBeforeClass / tearDownAfterClass which get called for every test file, not for every test case.)

    It's just a before/after hook which is a groovy thing to have in general. Use it to make your life easier, or don't use it.

提交回复
热议问题