开发者

python unit testing os.remove fails file system

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...
     开发者_StackOverflow中文版           #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?


If you're just looking for a temporary file, have a look at tempfile - this should handle the clean-up all on its own.


Do you remember to explicitly close file handler that operates on TEMPORARY_FILE_NAME?

From Python Documentation:

On Windows, attempting to remove a file that is in use causes an exception to be raised;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜