lundi 19 septembre 2016

Passing parameters to tearDown method

Suppose I have entity that creates SVN branch during its work. To perform functional testing I create multiple almost same methods (I use python unittest framework but question relates to any test framework):

class Tester(unittest.TestCase):

def test_valid1_url(self):
    url="valid1"
    BranchCreator().create_branch(url)
    self.assertUrlExists(url) # assume I have this method implemented

def test_valid2_url(self):
    url="valid2"
    BranchCreator().create_branch(url)
    self.assertUrlExists(url) # assume I have this method implemented

def test_invalid_url(self):
    url="invalid"
    self.assertRaises(ValueError, BranchCreator().create_branch, url)

After each test I want to remove the resulting branch or do nothing if test failed. Ideally I would use something like following:

@teardown_params(url='valid1')
def test_valid1_url(self):

def tearDown(self, url):
    if (url_exists(url)): remove_branch(url)

But tearDown does not accept any parameter. I see few quite dirty solutions:

a) create field "used_url" in Tester, set it in every method and use in tearDown:

def test_valid1_url(self):
    self.used_url="valid1"
    BranchCreator().create_branch(self.used_url)
    self.assertUrlExists(url) 
...
def tearDown(self):
    if (url_exists(self.used_url)): remove_branch(self.used_url)

It should work because (at least in my environment) all tests are run sequentally so there would be no conflicts. But this solution violates tests independency principle due to shared variable, and if I would manage to launch tests simultaneously, it will not work.

b) Use separate method like cleanup(self, url) and call it from every method

Is there any other approach?

Aucun commentaire:

Enregistrer un commentaire