Slaknation
Slaknation

Reputation: 1926

Python unit testing: multiple test functions to test single function with different inputs?

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")

Upvotes: 2

Views: 782

Answers (1)

mgilson
mgilson

Reputation: 310257

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.

Upvotes: 3

Related Questions