我的unittest拆卸代码无法删除文件
test_utility1.dat
我什至尝试了os.remove,也没有运气。我不明白为什么我的进程在出现错误后仍坚持使用该文件。我在python 3.6上运行此程序,并使用Pycharm作为我的IDE。我的所有测试都通过了,除了拆卸。
我的意思是我尝试嘲笑,但是我没有运气(新手,我只是无法理解或理解这个概念)。因此,我不得不创建一个临时文件并将其删除。我有同样的问题,所以我认为如果尝试使用textfixture库,效果会更好。但同样的错误。
更新我的帖子
请帮忙。谢谢
protoype7.py
import sys
import pandas
def create_utility_config_dataframe(a):
cols = ['Name', 'D', 'L', 'J', 'H', 'E', 'M', 'RF', 'AF']
try:
return pandas.read_csv(a, sep='\t', usecols=cols)
except Exception as ve:
ve = (str(ve) + "\n\nPlease ensure utility config '%s' exist and is correctly formatted (tab delimited)" %a)
sys.exit(ve)
test2_prototype7.py
from prototype7 import create_utility_config_dataframe
import os
import unittest
class TestCreateUtilityConfigDataFrame(unittest.TestCase):
@classmethod
def tearDownClass(cls):
os.remove('test_utility1.dat')
def test_when_parameters_is_invalid(self):
self.assertRaises(SystemExit, create_utility_config_dataframe,'test_utility1.dat')
错误日志
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'test_utility1.dat'
test_utility1.dat
Name.D.L.J.H.E.M.RF.AF
line1.150.4.2.150.2.Copper.1.true
好吧,分辨率如下。
初始代码段
self.assertRaises(SystemExit, create_utility_config_dataframe, a)
新代码段
with self.assertRaises(SystemExit):
create_utility_config_dataframe(a)
这解决了问题。这是assertRaise
代码的问题。我不完全确定为什么,但是如果我使用该with
语句,现在可以正常工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句