-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shapes_tests.py
131 lines (113 loc) · 4.19 KB
/
Shapes_tests.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# -*- coding: UTF-8 -*-
#-------------------------------------------------------------------------------
# Name: exercise_template_tests.py
#
# Purpose: Test functions for functions in exercise_template.py
#
# Author: David Viljoen
#
# Created: 24/11/2017
#-------------------------------------------------------------------------------
import sys
import inspect
import Shapes as shp
import math
# Add import statement for the module under test as follows:
# import module_under_test as alias
# For example:
# import world_pop_explorer as wpe
# reload(wpe)
def main():
# Find and call all functions that begin with "test"
test_funcs = get_test_functions()
for test_func in test_funcs:
test_func()
# Copy/paste/change the test template below to create new test functions, where:
# - the test function name must begin with the word "test"
# - desc = Description of the test being made
# - expected = Expected result from calling the function
# - actual = Actual result from calling the function
# - func = Function being tested (the actual function, not the name)"""
#
def template_for_test_functions():
desc = ""
expected = ""
actual = ""
print_test_results(func, desc, expected, actual)
# ------------------------------------------------------------------------------
# Create test functions here using the template_for_test_functions above.
# The name of the test functions needs to begin with "test"
def blank():
pass
def test_circleArea():
circle1 = shp.Circle()
circle1.radius = 2.5
expected = (math.pi * circle1.radius**2)
desc = "Returns area of circle from radius."
actual = circle1.area
print_test_results(blank, desc, expected, actual)
print str(circle1) , '\n'
def test_SquareArea():
square1 = shp.Square()
square1.side_length = 3
expected = (square1.side_length ** 2)
desc = "Returns area of square from side length."
actual = square1.area
print_test_results(blank, desc, expected, actual)
print str(square1), '\n'
def test_RectangleArea():
rect1 = shp.Rectangle()
rect1.width = 4
rect1.height = 2
expected = (rect1.height * rect1.width)
desc = "Returns area of a rectangle from length and height."
actual = rect1.area
print_test_results(blank, desc, expected, actual)
print str(rect1)
# ------------------------------------------------------------------------------
# Test template helper functions. Code in this section should not need to
# modified.
#
def get_test_functions():
"""Returns a list of functions that begin with the word test in the order
they appear in this file."""
test_funcs = [obj for name,obj in inspect.getmembers(sys.modules[__name__])
if (inspect.isfunction(obj) and name.startswith('test'))]
src = inspect.getsource(sys.modules[__name__])
lines = src.split('\n')
# Create a dictionary with key=function name and value is 0-based order
# in the module
ordered_func_names = dict()
ordered_funcs = list()
func_index = 0
for line in lines:
if line.find("def test") == 0:
func_name = line.split("(")[0].split()[1]
ordered_func_names[func_name] = func_index
# Create an empty list with sampe number of elements as test
# functions
ordered_funcs.append('')
func_index += 1
for test_func in test_funcs:
index = ordered_func_names[test_func.__name__]
ordered_funcs[index] = test_func
return ordered_funcs
def print_test_results(func_tested, desc, expected, actual):
"""func_tested is the function being tested
desc = Test description
expected = Expected result of test
actual = Actual result of test """
if not callable(func_tested):
raise Exception("{} is not a function".format(func_tested))
func_name = func_tested.__name__
if expected == actual:
print "PASSED: {}".format(func_name)
print "Detail: {}".format(desc)
else:
print "FAILED: {}".format(func_name)
print "Desc: {}".format(desc)
print "Expect: {}".format(expected)
print "Actual: {}".format(actual)
print ""
if __name__ == '__main__':
main()