miscell cosmetic + pass the substrate object to TestBonding so it can compute an...
[tests.git] / qaapi / qa / tests / Test.py
1 import sys, os
2 from types import *
3 from pprint import pprint
4
5 # Add qa root directory to the path to make
6 # imports easier 
7 path = os.path.dirname(os.path.abspath(__file__))
8 path_parts = path.split(os.sep)
9 sys.path.append(os.sep.join(path_parts[:-2]))
10
11 from qa import utils
12 from qa.logger import log
13 from qa.Config import Config
14
15 class Test:
16     """
17     Base class for all QA test functions. At a minimum all tests
18     must define:
19
20     call(arg1, arg2, ...): method body
21     """
22
23     accepts = []
24     returns = bool
25     status = "current" 
26
27     def call(self, *args):
28         """
29         Method body for test functions. Must override.
30         """
31         return True
32
33     def __init__(self, config = None):
34         if not config: config = Config()
35         self.name = self.__class__.__name__
36         self.path=os.path.abspath(os.path.dirname(sys.argv[0]))
37         self.config = config
38         self.errors = []
39
40
41     def __call__(self, *args, **kwds):
42         """
43         Main entry point for test functions. logs methods
44         """
45
46         #(min_args, max_args, defaults) = self.args()
47         
48         # Check that the right number of arguments were passed in
49         #if len(args) < len(min_args) or len(args) > len(max_args):
50         #    raise Exception#, (len(args), len(min_args), len(max_args))
51
52         module = self.__class__.__module__.replace(".", os.sep)
53         file = self.path + os.sep + module + ".py"
54         result = self.call(*args, **kwds)
55  
56         return result   
57             
58
59     def args(self):
60         """
61         Returns a tuple:
62
63         ((arg1_name, arg2_name, ...),
64          (arg1_name, arg2_name, ..., optional1_name, optional2_name, ...),
65          (None, None, ..., optional1_default, optional2_default, ...))
66
67         That represents the minimum and maximum sets of arguments that
68         this function accepts and the defaults for the optional arguments.
69         """
70
71         # Inspect call. Remove self from the argument list.
72         max_args = self.call.func_code.co_varnames[1:self.call.func_code.co_argcount]
73         defaults = self.call.func_defaults
74         if defaults is None:
75             defaults = ()
76
77         min_args = max_args[0:len(max_args) - len(defaults)]
78         defaults = tuple([None for arg in min_args]) + defaults
79
80         return (min_args, max_args, defaults)