I'm trying to write a testing script in python to test multiple files containing the same functions (coding assignments). I currently have a single test script that imports the file destined for testing and runs several tests using a class that inherits from unittest.TestCase:
from file_to_test import *
class Testing(unittest.TestCase):
<tests...>
I was wondering if there is a way to write another code that iterates over all files that should be tested, allowing us to test them all in one run. can this be done somehow? I thought about writing all outputs to a file and then using diff, but I wonder if there is a different solution.
thanks a lot!
this worked for me:
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")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.