我有一个项目,我需要编写一个模块,其中包含一个函数来检查当前工作目录的内容并打印出每个扩展名(“.txt”、“.doc”等)有多少文件的计数
然后我需要编写一个单独的模块来通过测试该函数是否给出正确的结果来验证。
import os
from collections import Counter
filenames = {}
extensions = []
file_counts = {}
extensions2 = {}
def examine():
for filename in filenames:
f = open(filename, "w")
f.write("Some text\n")
f.close()
name, extension = filename.split('.')
extensions.append(extension)
extensions2 = dict(Counter(extensions))
return extensions2
这是测试:
import unittest
import tempfile
import os
import shutil
import examine_directory as examdir
class TestExamine(unittest.TestCase):
def setUp(self):
self.origdir = os.getcwd()
self.dirname = tempfile.mkdtemp("testdir")
os.chdir(self.dirname)
examdir.filenames = {"this.txt", "that.txt", "the_other.txt","this.doc","that.doc","this.pdf","first.txt","that.pdf"}
def test_dirs(self):
expected = {'pdf': 2, 'txt': 4, 'doc': 2}
self.assertEqual(examdir.extensions2, expected, "Creation of files not possible")
def tearDown(self):
os.chdir(self.origdir)
shutil.rmtree(self.dirname)
if __name__ == "__main__":
unittest.main()
我被困住了,我需要一些帮助。我收到一个 assertEqual 错误。
提前致谢。