I have written one python script for each test case. Now I want to have another script that runs all the test cases one by one, and output the result of each test case to a file. How should I start?

Assuming each script has a function called run_test:

import os, sys

folder = os.path.abs("pathtoscriptsfolder")
sys.path.append(folder)

for script in os.listdir(folder):
    if script.endswith(".py"):
        mod = __import__(script)
        res = mod.run_test()
        
        fh = open("tests.res", "a")
        fh.writeline(str(res))

Thanks! I got an error:
ImportError: No module named test1.py
module body in master.py at line 9

mod = __import__(script)

Assuming each script has a function called run_test:

import os, sys

folder = os.path.abs("pathtoscriptsfolder")
sys.path.append(folder)

for script in os.listdir(folder):
    if script.endswith(".py"):
        mod = __import__(script)
        res = mod.run_test()
        
        fh = open("tests.res", "a")
        fh.writeline(str(res))

woops, that should be

mod = __import__(script[:-3]) #removes the .py from the filename

Yep, I realized that. Thanks!

woops, that should be

mod = __import__(script[:-3]) #removes the .py from the filename

Looks like the if one test case fails, the rest of the test cases won't be executed. How should I hand it if I want it continues to next test case.
Thanks!

Assuming each script has a function called run_test:

import os, sys

folder = os.path.abs("pathtoscriptsfolder")
sys.path.append(folder)

for script in os.listdir(folder):
    if script.endswith(".py"):
        mod = __import__(script)
        res = mod.run_test()
        
        fh = open("tests.res", "a")
        fh.writeline(str(res))

Use a try, except statement:

import os, sys

folder = os.path.abs("pathtoscriptsfolder")
sys.path.append(folder)

for script in os.listdir(folder):
    if script.endswith(".py"):
        try:
            mod = __import__(script)
            res = mod.run_test()
        
            fh = open("tests.res", "a")
            fh.writeline(str(res))
        except TypeOfErrorHere:
            print "Test case didnt work... continuing"

I hope that helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.