-
Notifications
You must be signed in to change notification settings - Fork 27
/
test_util.py
62 lines (55 loc) · 2.6 KB
/
test_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#! /usr/bin/env python3
import unittest
class TestBaseClass(unittest.TestCase):
def get_failure_message_conditions(self, function, key_word_args):
message = (
"\n\n"
"Calling `{function_name}` with the following parameters:\n".format(
function_name = function.__name__)
)
for parameter_name, parameter_value in key_word_args.items():
message += "\t{parameter_name} = {parameter_value!r} {parameter_type}\n".format(
parameter_name = parameter_name,
parameter_value = parameter_value,
parameter_type = type(parameter_value))
return message
def get_failure_message_issue(self, expected_result, result):
message = (
"Expecting\n"
"\t{expected_result!r} {expected_type}\n"
"to be returned, but got\n"
"\t{result!r} {returned_type}\n".format(
expected_result = expected_result,
expected_type = type(expected_result),
result = result,
returned_type = type(result))
)
return message
def get_failure_message(self, function, key_word_args, expected_result,
result):
message = self.get_failure_message_conditions(function, key_word_args)
message += self.get_failure_message_issue(expected_result, result)
return message
def get_raise_failure_message(self, function, key_word_args, expected_exception):
message = self.get_failure_message_conditions(function, key_word_args)
message += "Expecting `{0}` to be raised, but it was not.".format(
expected_exception.__name__)
return message
def run_test_of_function(self, function, key_word_args, expected_result):
result = function(**key_word_args)
self.assertEqual(result, expected_result,
self.get_failure_message(
function = function,
key_word_args = key_word_args,
expected_result = expected_result,
result = result))
def run_test_of_function_raise(self, function, key_word_args, expected_exception):
try:
ret = function(**key_word_args)
# self.assertFail()
except expected_exception as e:
pass
else:
message = self.get_raise_failure_message(function, key_word_args,
expected_exception)
self.assertTrue(False, message)