I will try to do it this way:
1) Save your abstract class in test_case.py
class TestCase:
def executeTest():
#do some logic for the test, then return if it passed or failed
2) Save all your child classes in in test_case_children.py
from test_case import TestCase
class Test_Case_1(TestCase):
def executeTest():
#overriden function
class Test_Case_2(TestCase):
def executeTest():
#overriden function
class Test_Case_3(TestCase):
def executeTest():
#overriden function
3) Save main function in main.py:
from test_case import TestCase
import test_case_children
def main():
#grab the all the elements in the script 'test_case_children'
items = test_case_children.__dict__
#build list of all 'TestCase' sub-classes
test_classes = []
for (key, value) in items.items():
try:
# check whether the item is a sub-class of 'TestCase' class
if TestCase.__subclasscheck__(value):
test_classes.append(value)
except TypeError: #if item is not of type 'TestCase', ignore it
pass
#run the tests
for test_class in test_classes:
test_runner = test_class()
test_runner.executeTest()
# this will run main() method, only when script is directly executed
# from shell or command prompt ...
if __name__ == "__main__":
main()
4) Execute the main.py script:
$ python main.py
Note: One more thing, the folder in which you will save these files should also contain an empty __init__.py file to make that folder as python app(something like packages in Java or namespaces in C++). If you don't then those import statements will not work, probably.
[ Update for running test_cases from different files ]
1) Keep the files in following heirarchy:
<root>/
------>test_case/
---------------->__init__.py
---------------->main.py
---------------->test_case.py
---------------->test_case_children/
--------------------------------->__init__.py
--------------------------------->test_case_1.py
--------------------------------->test_case_2.py
--------------------------------->test_case_3.py
2) Save your abstract class in test_case/test_case.py
class TestCase:
def executeTest():
#do some logic for the test, then return if it passed or failed
3) Save sub-classes like this:
File: test_case/test_case_children/test_case_1.py
from test_case.test_case import TestCase
class Test_Case_1(TestCase):
def executeTest():
#overriden function
File: test_case/test_case_children/test_case_2.py
from test_case.test_case import TestCase
class Test_Case_2(TestCase):
def executeTest():
#overriden function
File: test_case/test_case_children/test_case_3.py
from test_case.test_case import TestCase
class Test_Case_3(TestCase):
def executeTest():
#overriden function
4) Save main function in main.py:
from test_case import TestCase
from test_case import test_case_children
def main():
#grab the all the elements in the module 'test_case_children'
items = test_case_children.__dict__
#build list of all 'TestCase' sub-classes
test_classes = []
for (dict_key, dict_value) in items:
#check whether the type of item's value is a module,
# if its a module it's likely to contain a TestCase subclass...
if str(type(dict_value)) == "<type 'module'>":
for (key, value) in dict_value.items():
try:
# check whether the item is a sub-class of 'TestCase' class
if TestCase.__subclasscheck__(value):
test_classes.append(value)
except TypeError: #if item is not of type 'TestCase', ignore it
pass
#run the tests
for test_class in test_classes:
test_runner = test_class()
test_runner.executeTest()
# this will run main() method, only when script is directly executed
# from shell or command prompt ...
if __name__ == "__main__":
main()
5) Execute the main.py script:
$ cd test_case/
$ python main.py
I hope this would work for you.