jeudi 1 décembre 2016

Is reusing a tested function in another test a bad practice?

Someone taught me that unit tests are supposed to be independent and simple. So, it's not advised to use a function that is tested in another test.

Sometimes though, it's very convenient to use a tested function inside another test, especially if it is some sort of "display" function, to observe the effects of a procedure.

Example :

def test_section_inherit_mode_basic(self):
    res = ['father', 'grandpa']
    res.sort()
    test = self.x.options('sect6')
    test.sort()
    self.assertEquals(test, res)

Here I am testing a procedure get_corresponding_sections() (the results are not visible unless you use a "display" function) which is used in options(). The options() function is a function that I tested before, so, if a test on this function fails, it can potentially make other tests where options() is used fail.

Here is the code of options() (ignore the _) :

def _options(self, section):
    """ Returns a list of option names for the given section and its
    parents. """

    res = []
    sections = self.get_corresponding_sections(section)
    for s in sections:
        res += super(ExtendedConfigParser, self).options(s)
    if(self.default_section is not None):
        res += list(self.default_section.keys())
    if(self.father is not None):
        res += list(self.father.keys())

Is this a big problem knowing that options() is a "display" function that can only fail if the argument is invalid? Is it risky?

Before you tell me, I read this post and it's a possible duplicate but it's not entirely answering my question. Thanks you for reading.

Aucun commentaire:

Enregistrer un commentaire