[英]Using python's unittest to test multiple files
我正在尝试在 python 中编写一个测试脚本来测试包含相同功能(编码分配)的多个文件。 我目前有一个测试脚本,它导入用于测试的文件并使用继承自 unittest.TestCase 的类运行多个测试:
from file_to_test import *
class Testing(unittest.TestCase):
<tests...>
我想知道是否有一种方法可以编写另一种代码来迭代所有应该测试的文件,从而允许我们在一次运行中测试它们。 这可以以某种方式完成吗? 我想过将所有输出写入一个文件,然后使用 diff,但我想知道是否有不同的解决方案。
多谢!
这对我有用:
test_import_format = "import {} as tested_script\n"
for file in os.listdir(DIR_PATH):
if file.endswith(".py") and not file.endswith(
"test_name.py") and not file.endswith("THIS_FILE.py"):
cur_test_import = test_import_format.format(
ntpath.basename(file).split(".")[0])
with open("test_name.py", 'r') as test, open("temp.py", 'w') as temp:
for i, line in enumerate(test):
if i == 0:
temp.write(cur_test_import)
else:
temp.write(line)
subprocess.call([sys.executable, "temp.py"])
os.remove(r"DIR_PATH/temp.py")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.