/
hero_test.py
220 lines (163 loc) · 5.92 KB
/
hero_test.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import pytest
import io
import sys
import superheroes
# Helper Function
def capture_console_output(function_body):
# _io.StringIO object
string_io = io.StringIO()
sys.stdout = string_io
function_body()
sys.stdout = sys.__stdout__
return string_io.getvalue()
# Test Abilities Class
def test_ability_instance():
# Test instantiation without error
big_strength = superheroes.Ability("Overwhelming Strength", 300)
assert big_strength
def test_ability_name():
# Test for Correct Name
big_strength = superheroes.Ability("Overwhelming Strength", 300)
assert big_strength.name == "Overwhelming Strength"
def test_ability_update_attack():
big_strength = superheroes.Ability("Overwhelming Strength", 300)
big_strength.update_attack(400)
test_runs = 100
attack = 0
for _ in range(0, test_runs):
attack += big_strength.attack()
assert attack <= (400 * test_runs) and attack >= (200 * test_runs)
def test_ability_attack():
# Test for correct attack value
test_runs = 100
big_strength = superheroes.Ability("Overwhelming Strength", 400)
for _ in range(0, test_runs):
attack = big_strength.attack()
assert attack <= 400 and attack >= 200
# Test Weapons Class
def test_weapon_instance():
big_stick = superheroes.Weapon("Overwhelming Stick", 200)
assert "Weapon" in str(big_stick)
def test_weapon_attack():
big_stick = superheroes.Weapon("Overwhelming Stick", 200)
test_runs = 100
for _ in range(0, test_runs):
attack = big_stick.attack()
assert attack <= 200 and attack >= 0
# Test Heroes Class
def test_hero_instance():
Athena = superheroes.Hero("Athena")
assert Athena
def test_hero_add_ability():
big_strength = superheroes.Ability("Overwhelming Strength", 300)
Athena = superheroes.Hero("Athena")
assert len(Athena.abilities) == 0
Athena.add_ability(big_strength)
assert len(Athena.abilities) == 1
# Check for correct type
assert "Ability" in str(Athena.abilities[0])
assert Athena.abilities[0].name == "Overwhelming Strength"
def test_hero_add_multi_ability():
big_strength = superheroes.Ability("Overwhelming Strength", 300)
speed = superheroes.Ability("Lightning Speed", 500)
Athena = superheroes.Hero("Athena")
assert len(Athena.abilities) == 0
Athena.add_ability(big_strength)
assert len(Athena.abilities) == 1
Athena.add_ability(speed)
assert len(Athena.abilities) == 2
# Check for correct type
assert "Ability" in str(Athena.abilities[0])
assert Athena.abilities[0].name == "Overwhelming Strength"
def test_hero_attack_ability():
big_strength = superheroes.Ability("Overwhelming Strength", 30000)
athena = superheroes.Hero("Athena")
assert athena.attack() == 0
athena.add_ability(big_strength)
attack = athena.attack()
assert attack <= 30000 and attack >= 15000
def test_hero_attack_weapon():
big_strength = superheroes.Ability("Overwhelming Strength", 200)
Athena = superheroes.Hero("Athena")
Athena.add_ability(big_strength)
test_runs = 100
for _ in range(0, test_runs):
attack = big_strength.attack()
assert attack <= 200 and attack >= 0
def test_hero_multi_weapon_attack():
strength = superheroes.Weapon("Overwhelming Strength", 200)
sword_of_truth = superheroes.Weapon("Sword of Truth", 700)
Athena = superheroes.Hero("Athena")
Athena.add_ability(strength)
Athena.add_ability(sword_of_truth)
assert len(Athena.abilities) == 2
test_runs = 100
for _ in range(0, test_runs):
attack = Athena.attack()
assert attack <= 900 and attack >= 0
def test_hero_weapon_ability_attack():
quickness = superheroes.Ability("Quickness", 1300)
sword_of_truth = superheroes.Weapon("Sword of Truth", 700)
Athena = superheroes.Hero("Athena")
Athena.add_ability(quickness)
Athena.add_ability(sword_of_truth)
assert len(Athena.abilities) == 2
attack_avg(Athena, 0, 2000)
def attack_avg(object, low, high):
test_runs = 100
for _ in range(0, test_runs):
attack = object.attack()
assert attack <= high and attack >= low
# Test Teams
def test_team_instance():
team = superheroes.Team("One")
assert team
def test_team_name():
team = superheroes.Team("One")
assert team.name == "One"
def test_team_hero():
team = superheroes.Team("One")
jodie = superheroes.Hero("Jodie Foster")
team.add_hero(jodie)
assert len(team.heroes) == 1
assert team.heroes[0].name == "Jodie Foster"
def test_team_remove_hero():
team = superheroes.Team("One")
jodie = superheroes.Hero("Jodie Foster")
team.add_hero(jodie)
assert team.heroes[0].name == "Jodie Foster"
team.remove_hero("Jodie Foster")
assert len(team.heroes) == 0
def test_team_remove_unlisted():
# Test that if no results found return 0
team = superheroes.Team("One")
jodie = superheroes.Hero("Jodie Foster")
team.add_hero(jodie)
code = team.remove_hero("Athena")
assert code == 0
def test_team_remove_empty_list():
team = superheroes.Team("One")
assert team.remove_hero("Athena") == 0
def test_find_hero():
team = superheroes.Team("One")
jodie = superheroes.Hero("Jodie Foster")
team.add_hero(jodie)
hero = team.find_hero("Jodie Foster")
assert hero.name == "Jodie Foster"
def test_find_unlisted_hero():
team = superheroes.Team("One")
jodie = superheroes.Hero("Jodie Foster")
team.add_hero(jodie)
assert team.find_hero("Alexa") == 0
def test_find_empty_hero():
team = superheroes.Team("One")
assert team.find_hero("Alexa") == 0
def test_print_heroes():
team = superheroes.Team("One")
jodie = superheroes.Hero("Jodie Foster")
team.add_hero(jodie)
athena = superheroes.Hero("Athena")
team.add_hero(athena)
output_string = capture_console_output(team.view_all_heroes)
assert "Jodie Foster" in output_string
assert "Athena" in output_string