2

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.

Community
  • 1
  • 1
Alex Tereshenkov
  • 3,340
  • 8
  • 36
  • 61
  • Why are you trying to jam these two tests into one class? If you separate them, the `setUp` and `tearDown` will become much more intuitive. – jonrsharpe Oct 06 '15 at 10:14
  • Thanks for commenting. I thought that since those are similar operations, it would make sense to keep them in the same class. Do you think I should create separate classes (TestCreateFolder,TestDeleteFolder, etc)? And what should be the best approach to deal with the TestRenameFolder class? Would it be OK to create a folder in setUp and delete it in tearDown after testing renaming it? – Alex Tereshenkov Oct 06 '15 at 10:19
  • Yes, and yes; that's exactly what I'd suggest. Otherwise your tests depend on each other and the order in which they're run, which is very bad practice (should your `delete` command fail just because, even though the `create` succeeded, the `rename` didn't work as expected?) – jonrsharpe Oct 06 '15 at 10:21
  • OK, I got it. Would you think it would be OK to keep in the same class several tests that are absolutely independent of each other (say, changing attributes of an object)? I am a bit worried by the idea of having so many classes for each test that modifies an attribute :) ps. If you convert your comment to an answer, I'll be happy to accept it! – Alex Tereshenkov Oct 06 '15 at 10:27
  • If they need the same `setUp` and `tearDown` (including neither!) then it's fine to put them in the same class. – jonrsharpe Oct 06 '15 at 10:32
  • Great, thanks very much. I'll keep my tests structured as you suggested, this does make sense. – Alex Tereshenkov Oct 06 '15 at 10:33

0 Answers0