I'm trying to test my own version of antiweb, which can be found here. However, I am testing it with Pythons unittest module. Here is the code:
import unittest
from unittest.mock import patch
from antiweb import main
import sys
import os
import tempfile
import shutil
class Test_Antiweb(unittest.TestCase):
def setUp(self):
self.test_dir = tempfile.mkdtemp()
self.testfile_source ="#@start()\n\n#@include(test_area)\n\n#@start(test_area)\n#This is test_area text\n#@(test_area)"
with open(os.path.join(self.test_dir, "testfile.py"), "w") as test:
test.write(self.testfile_source)
def test_antiweb(self):
self.test_args = ['antiweb.py', '-i', "-o docs", os.path.join(self.test_dir, "testfile.py")]
with patch.object(sys, 'argv', self.test_args):
success = main()
self.assertTrue(success)
def tearDown(self):
shutil.rmtree(self.test_dir)
if __name__ == '__main__':
unittest.main()
Everything works fine, except for the tearDown
function. When executing the unittest without tearDown
, the temp folder and his content are perfectly created. But with the tearDown
function I get an error:
======================================================================
ERROR: test_antiweb (antiweb_tests.Test_Antiweb)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\<username>\Documents\GitHub\antiweb\antiweb_tests.py", line 29, in tearDown
shutil.rmtree(self.test_dir)
File "C:\Python34\lib\shutil.py", line 478, in rmtree
return _rmtree_unsafe(path, onerror)
File "C:\Python34\lib\shutil.py", line 377, in _rmtree_unsafe
onerror(os.rmdir, path, sys.exc_info())
File "C:\Python34\lib\shutil.py", line 375, in _rmtree_unsafe
os.rmdir(path)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\
<username>\\AppData\\Local\\Temp\\tmp3lk01fn5'
When I then look at the temp folder the folder itself is still there, but is empty now. It would be too much to include my antiweb file here, so I have it linked here again if you need it.
Just had this happen to myself. Your problem is in your setup
self.test_dir = tempfile.mkdtemp() returns a tuple of file descriptor and path. You need to close the file descriptor before deleting.
def setUp(self):
self.fd, self.test_dir = tempfile.mkdtemp()
...
def tearDown(self):
os.close(self.fd)
shutil.rmtree(self.test_dir)
See this article for a more detailed explaination.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With