小编Phi*_*hil的帖子

PermissionError:[WinError 32]进程无法访问该文件,因为它正由另一个进程使用:

我正在尝试测试我自己的反网络版本,可以在这里找到.但是,我正在使用Pythons unittest模块进行测试.这是代码:

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()
Run Code Online (Sandbox Code Playgroud)

一切正常,除了tearDown功能.在没有执行unittest时tearDown,temp文件夹和他的内容被完美地创建.但是使用该tearDown函数我得到一个错误:

======================================================================
ERROR: test_antiweb (antiweb_tests.Test_Antiweb)
----------------------------------------------------------------------
Traceback …
Run Code Online (Sandbox Code Playgroud)

python shutil python-unittest

6
推荐指数
1
解决办法
4341
查看次数

标签 统计

python ×1

python-unittest ×1

shutil ×1