I have this directory structure.
In my project root directory I have a main python file say main.py. Also in the this root direcory there is folder called Tests. This test folder will have series of test files (known only at run time) e.g test1.py,test2.py etc…
In my testfiles e.g Test1.py I have this sort of structure:
from TestBase import TestBase
class Test1(TestBase):
def TestFunction(self):
# do_some_stuff
In my main.py I want to call this TestFunction(). How do I do this ?
# use sys.path.append() to add Tests directory our path
x= import(testfilename)
result = x.TestFunction() # something like this method from class in testfilename
Starting from Python 2.7, the functionality you need is included in the built-in
unittestmodule. For earlier versions of Python this module has been backported under the nameunittest2.If for some reason you want to roll your own test discovery, here is a basic approach: