2

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") 
3
  • 1
    I would go for the second approach as that is more verbose, and also the purpose of the two scenarios are kinda different. The first case tests a blank init while the second tests for an argument input. Commented Sep 5, 2017 at 12:32
  • 1
    Definitely the second approach. You are testing two different scenarios, so your second approach is definitely what I think you should use. It is more explicit, helps document behaviour better. One comment, however. You should make sure that the test_init is also renamed to reflect what behaviour you are testing as well. Commented Sep 5, 2017 at 12:38
  • en.wikipedia.org/wiki/Data-driven_testing github.com/wolever/parameterized Commented Sep 5, 2017 at 13:21

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.