我已经编写了一些测试用例来测试我编写的函数。功能是简单地统计特定目录中的文件数。最终我将有另一个函数,它会根据每个目录中有多少文件以某种方式运行。在这种情况下,我正在使用两个目录。这是我的功能:
dir_handler.py
from pathlib import Path
def count_files_in_dir(dirpath):
assert(dirpath.is_dir())
file_list = []
for file in dirpath.iterdir():
if file.is_file():
file_list.append(file)
return len(file_list)
这是我的测试用例:
test_dir_handler.py
from imports import *
import os
from main.dir_handler import count_files_in_dir
class DirHandlerTests(unittest.TestCase):
def test_return_count_of_zero_when_no_file_exists_in_input_dir(self):
self.assertEqual(0, count_files_in_dir(INPUT_FILE_PATH))
def test_return_count_of_zero_when_no_file_exists_in_output_dir(self):
self.assertEqual(0, count_files_in_dir(OUTPUT_FILE_PATH))
def test_return_count_of_one_when_one_file_exists_in_input_dir(self):
with open(str(INPUT_FILE_PATH)+ "/"+"input.csv", "w") as file:
self.assertEqual(1, count_files_in_dir(INPUT_FILE_PATH))
def test_return_count_of_one_when_one_file_exists_in_output_dir(self):
with open(str(OUTPUT_FILE_PATH)+ "/"+"output.csv", "w") as file:
self.assertEqual(1, count_files_in_dir(OUTPUT_FILE_PATH))
def test_return_count_of_two_when_two_files_exists_in_output_dir(self):
with open(str(OUTPUT_FILE_PATH)+ "/"+"output.csv", "w") as file:
with open(str(OUTPUT_FILE_PATH)+ "/"+"output2.csv", "w") as file:
self.assertEqual(2, count_files_in_dir(OUTPUT_FILE_PATH))
#clearing up testing files at the end of test
def tearDown(self):
try:
os.remove(str(INPUT_FILE_PATH)+ "/"+"input.csv")
except FileNotFoundError as e:
pass
try:
os.remove(str(OUTPUT_FILE_PATH)+ "/"+"output.csv")
except FileNotFoundError as e:
pass
try:
os.remove(str(OUTPUT_FILE_PATH)+ "/"+"output2.csv")
except FileNotFoundError as e:
pass
if __name__ == '__main__':
unittest.main()
如您所见,我不得不分别删除“input2.csv”和“output2.csv”,这不是很有效。 INPUT_FILE_PATH 和 OUTPUT_FILE_PATH 都在同一目录"file"下。所有测试都通过了,但我想要关于在测试结束时清理 INPUT_FILE_PATH 和 OUTPUT_FILE_PATH 目录的最佳方法的建议。谢谢
编辑:
使用@rockport 的建议,我已经实现了 setUp/tearDown 方法。该代码按预期工作,但仍然很困惑。它会在测试结束时清除 output_file 文件夹和 input_file 文件夹。我还实现了 pathlib 而不是 os,因为我将在 mac 和 windows 上运行和编辑代码。这是我的代码的一些更改
def setUp(self):
self.input_file = INPUT_FILE_PATH.joinpath("input.csv")
self.output_file = OUTPUT_FILE_PATH.joinpath("output.csv")
self.output_file2 = OUTPUT_FILE_PATH.joinpath("output2.csv")
def test_return_count_of_one_when_one_file_exists_in_output_dir(self):
with self.output_file.open(mode='w') as file:
self.assertEqual(1, count_files_in_dir(OUTPUT_FILE_PATH))
def test_return_count_of_two_when_two_files_exist_in_output_dir(self):
with self.output_file.open(mode='w') as file:
with self.output_file2.open(mode='w') as file:
self.assertEqual(2, count_files_in_dir(OUTPUT_FILE_PATH))
def tearDown(self):
for file in INPUT_FILE_PATH.iterdir():
try:
file.unlink()
except FileNotFoundError as e:
pass
for file in OUTPUT_FILE_PATH.iterdir():
try:
file.unlink()
except FileNotFoundError as e:
pass
最佳答案
你要的是shutil.rmtree
这将删除整个目录,包括其中的所有子目录和文件。之后,您可以使用 os.mkdir
或 os.makedirs
重新创建目录。这是一个例子:
import os
import shutil
shutil.rmtree(INPUT_FILE_PATH)
os.mkdir(INPUT_FILE_PATH)
关于python - 测试后清理文件的最有效方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52056787/