python unit testing os.remove fails file system

Posted by hwjp on Stack Overflow See other posts from Stack Overflow or by hwjp
Published on 2010-06-14T10:30:05Z Indexed on 2010/06/14 10:32 UTC
Read the original article Hit count: 209

Filed under:
|

Am doing a bit of unit testing on a function which attempts to open a new file, but should fail if the file already exists. when the function runs sucessfully, the new file is created, so i want to delete it after every test run, but it doesn't seem to be working:

class MyObject_Initialisation(unittest.TestCase):
    def setUp(self):
        if os.path.exists(TEMPORARY_FILE_NAME):
            try:
                os.remove(TEMPORARY_FILE_NAME)
            except WindowsError:
                #TODO: can't figure out how to fix this...
                #time.sleep(3)
                #self.setUp() #this just loops forever
                pass

    def tearDown(self):
        self.setUp()

any thoughts? The Windows Error thrown seems to suggest the file is in use... could it be that the tests are run in parallel threads?

I've read elsewhere that it's 'bad practice' to use the filesystem in unit testing, but really? Surely there's a way around this that doesn't invole dummying the filesystem?

© Stack Overflow or respective owner

Related posts about python

Related posts about unit-testing