When testing a single function with different inputs (some that are default), is it better practice to do:
def test_init(self):
page = HTMLGen("test", "path\\goes\\here")
self.assertEqual(page.path, "path\\goes\\here\\index.html")
page_2 = HTMLGen("test", "path\\goes\\here", "cool_page")
self.assertEqual(page_2.path, "path\\goes\\here\\cool_page.html")
or
def test_init(self):
page = HTMLGen("test", "path\\goes\\here")
self.assertEqual(page.path, "path\\goes\\here\\index.html")
def test_init_with_filename(self):
page = HTMLGen("test", "path\\goes\\here", "cool_page")
self.assertEqual(page.path, "path\\goes\\here\\cool_page.html")
asked Sep 5, 2017 at 12:26
Slaknation
1,9564 gold badges29 silver badges50 bronze badges
1 Answer 1
The second approach is better because if the first test fails, the second one will still have a chance to run. This can give you more information for tracking down exactly where the bug is happening and what is causing it.
Additionally, any cleanup/teardown code will be run between the tests which can help to guarantee that the tests are independent.
answered Sep 5, 2017 at 12:39
mgilson
312k70 gold badges658 silver badges723 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Slaknation
But wouldn't I not want to use setup or teardown in this situation? Because if I assign self.page = HTMLGen("test", "path\\goes\\here") in setUp then the problem with this is that I will just have to redefine another variable in the second function: page = HTMLGen("test", "path\\goes\\here", "cool_page") anyways?
lang-py
test_initis also renamed to reflect what behaviour you are testing as well.