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?
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.