Package tlib :: Package base :: Module SmartTester
[hide private]
[frames] | no frames]

Source Code for Module tlib.base.SmartTester

  1  __author__ = 'ggegiya1' 
  2   
  3   
4 -class TestFailure(AssertionError):
5 - def __init__(self, message):
6 super(TestFailure, self).__init__(message)
7 8
9 -class SmartTester:
10 - def __init__(self):
11 self.argument = None 12 self.extra_arguments = None 13 self.validation_function = None
14
15 - def assertThat(self, arg=None, *arguments):
16 ## if the argument is a validation function 17 ## then validation function result will be validated 18 if hasattr(arg, "__call__"): 19 self.validation_function = arg 20 self.extra_arguments = arguments 21 else: 22 self.argument = arg 23 return self
24
25 - def equalsTo(self, argument2):
26 """ 27 Embedded method to compare the passed argument to the self.argument 28 :param argument2: 29 :return: True if arguments are equal, else throw an error 30 """ 31 if self.argument == argument2: 32 return True 33 self.throw_error("%s and %s are not equal" 34 % (self._short(self.argument), 35 self._short( argument2)))
36
37 - def contains(self, argument2):
38 if (not hasattr(self.argument, "__iter__")) and (type(self.argument) not in [str]): 39 raise ValueError("The first argument is not iterable") 40 if argument2 in self.argument: 41 return True 42 self.throw_error("The list [%s..] does not contain %s" 43 % (self._short(self.argument), 44 self._short(argument2)))
45
46 - def isTrue(self):
47 self.validate(True)
48
49 - def isFalse(self):
50 self.validate(False)
51
52 - def validate(self, expected=True):
53 """ 54 Validates the first argument or validation function 55 :param expected: Expected boolean value of the validation function 56 :return: True if validation function result == expected 57 """ 58 if self.argument is not None: 59 if bool(self.argument) is expected: 60 return True 61 else: self.throw_error("%s is not %s" % (self._short(self.argument), expected)) 62 elif self.validation_function is not None: 63 test_status, test_report = self.validation_function(*self.extra_arguments) 64 if test_status is expected: return True 65 self.throw_error(test_report) 66 else: 67 raise ValueError("Invalid arguments passed to assertion")
68
69 - def throw_error(self, message):
70 raise TestFailure("\r\n" * 2 + "-" * 40 + "\r\nTest Failed!!!\r\n" 71 + (message or "") 72 + "\r\n")
73
74 - def _short(self, arg, length=5):
75 """ 76 Shorten the string representation of the argument 77 :param arg: an arbitrary value 78 :param length: the length of the resulting string 79 :return: shortened string 80 """ 81 if hasattr(arg, "__iter__"): 82 result = ", ".join([str(arg[i]) for i in xrange(0, min(len(arg), length))]) 83 else: 84 result = str(arg)[:80] 85 return result
86 87 88 ##### this is an example of the validation function
89 -def equals(argument1, argument2):
90 if argument1 == argument2: 91 return True, "%s and %s are equal" % (argument1, argument2) 92 return False, "%s and %s are not equal" % (argument1, argument2)
93 94 95 96 #### just an example of usage
97 -class MyTester(SmartTester):
98 99 ## all those tests should fail 100
101 - def my_test_1(self):
102 self.assertThat([1,4,5,6,7,9,0]).contains(2)
103
104 - def my_test_2(self):
105 self.assertThat(2).equalsTo(3)
106
107 - def my_test_3(self):
108 self.assertThat(equals, 2, 2).isFalse()
109
110 - def my_test_4(self):
111 self.assertThat(False).isTrue()
112
113 - def my_test_5(self):
114 self.assertThat(equals, 2, 3).isTrue()
115