mardi 6 octobre 2015

How to organize unittest test case for creating and then deleting object (Python)?

I am writing some unit tests with Python 2.7 (using unittest framework) for a service that can work with some objects.

To make it simple, say I want to test methods that create, rename, delete etc. folders.

class TestFolderMethods(unittest.TestCase):
    '''testing folders'''

    #----------------------------------------------------------------------
    def setUp(self):
        """prepare for tests"""

    #----------------------------------------------------------------------
    def test_create_folder(self):
        """creates a new folder"""
        #create folder code; check it's in the list of folders

    #----------------------------------------------------------------------
    def test_delete_folder(self):
        """deletes a folder"""
        #delete folder code; check it's not in the list of folders

I understand that the tests will be run in a certain order that is determined by sorting the test function names with respect to the built-in ordering for strings.

I am aware of monolithic tests, execution order modification, and setUp/tearDown methods.

Is it acceptable to put the "create folder" code into the test_delete_folder method? That is, I need to create a dummy folder first before I can test to delete it.

There will be lots of similar operations on other objects (create, modify, delete), so I am looking for the best way to organize my code.

Aucun commentaire:

Enregistrer un commentaire