Skip to content

Commit 1cfdc73

Browse files
committed
Added Testing - Exercise
1 parent ab1c48c commit 1cfdc73

File tree

15 files changed

+231
-0
lines changed

15 files changed

+231
-0
lines changed
Binary file not shown.

OOP/A.Testing/Testing - Exercise/01. Mammal/test/__init__.py

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from unittest import TestCase, main
2+
from project.mammal import Mammal
3+
4+
5+
class TestMammal(TestCase):
6+
7+
def setUp(self) -> None:
8+
self.mammal = Mammal("Rex", "Lion", "ROAR!!!")
9+
10+
def test_successful_initialization(self):
11+
self.assertEqual("Rex", self.mammal.name)
12+
self.assertEqual("Lion", self.mammal.type)
13+
self.assertEqual("ROAR!!!", self.mammal.sound)
14+
15+
def test_make_sound(self):
16+
self.assertEqual("Rex makes ROAR!!!", self.mammal.make_sound())
17+
18+
def test_get_kingdom(self):
19+
self.assertEqual("animals", self.mammal.get_kingdom())
20+
21+
def test_info(self):
22+
self.assertEqual("Rex is of type Lion", self.mammal.info())
23+
24+
25+
if __name__ == '__main__':
26+
main()
Binary file not shown.

OOP/A.Testing/Testing - Exercise/02. Vehicle/test/__init__.py

Whitespace-only changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from unittest import TestCase, main
2+
from project.vehicle import Vehicle
3+
4+
5+
class TestVehicle(TestCase):
6+
7+
def setUp(self) -> None:
8+
self.vehicle = Vehicle(150.50, 200)
9+
10+
def test_successful_initialization(self):
11+
self.assertEqual(150.50, self.vehicle.fuel)
12+
self.assertEqual(self.vehicle.fuel, self.vehicle.capacity)
13+
self.assertEqual(200, self.vehicle.horse_power)
14+
self.assertEqual(1.25, self.vehicle.fuel_consumption)
15+
16+
def test_unsuccessful_drive_not_enough_fuel(self):
17+
with self.assertRaises(Exception) as ex:
18+
self.vehicle.drive(200)
19+
20+
self.assertEqual("Not enough fuel", str(ex.exception))
21+
22+
def test_successful_drive(self):
23+
self.vehicle.drive(10)
24+
result = 150.50 - 12.5
25+
self.assertEqual(result, self.vehicle.fuel)
26+
27+
def test_unsuccessful_refuel_too_much_fuel(self):
28+
with self.assertRaises(Exception) as ex:
29+
self.vehicle.refuel(3)
30+
31+
self.assertEqual("Too much fuel", str(ex.exception))
32+
33+
def test_successful_refuel(self):
34+
self.vehicle.capacity = self.vehicle.fuel * 2
35+
self.vehicle.refuel(50)
36+
self.assertEqual(200.50, self.vehicle.fuel)
37+
38+
def test_str_method_successful_string_return(self):
39+
wanted_result = f"The vehicle has 200 " \
40+
f"horse power with 150.5 fuel left and 1.25 fuel consumption"
41+
self.assertEqual(wanted_result, str(self.vehicle))
42+
43+
44+
if __name__ == '__main__':
45+
main()
Binary file not shown.

OOP/A.Testing/Testing - Exercise/03. Hero/test/__init__.py

Whitespace-only changes.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
from unittest import TestCase, main
2+
from project.hero import Hero
3+
4+
5+
class TestHero(TestCase):
6+
7+
def setUp(self) -> None:
8+
self.hero = Hero("Superman", 7, 100, 15)
9+
10+
def test_successful_initialization(self):
11+
self.assertEqual("Superman", self.hero.username)
12+
self.assertEqual(7, self.hero.level)
13+
self.assertEqual(100, self.hero.health)
14+
self.assertEqual(15, self.hero.damage)
15+
16+
def test_battle_fighting_yourself_raises_exception(self):
17+
enemy = Hero("Superman", 7, 100, 15)
18+
19+
with self.assertRaises(Exception) as ex:
20+
self.hero.battle(enemy)
21+
22+
self.assertEqual("You cannot fight yourself", str(ex.exception))
23+
24+
def test_battle_with_zero_health_raises_value_error(self):
25+
enemy = Hero("Batman", 19, 180, 65)
26+
self.hero.health = 0
27+
28+
with self.assertRaises(ValueError) as ve:
29+
self.hero.battle(enemy)
30+
31+
self.assertEqual("Your health is lower than or equal to 0. You need to rest", str(ve.exception))
32+
33+
def test_battle_with_less_than_zero_health_raises_value_error(self):
34+
enemy = Hero("Batman", 19, 180, 65)
35+
self.hero.health = -1
36+
37+
with self.assertRaises(ValueError) as ve:
38+
self.hero.battle(enemy)
39+
40+
self.assertEqual("Your health is lower than or equal to 0. You need to rest", str(ve.exception))
41+
42+
def test_battle_with_enemy_zero_health_raises_value_error(self):
43+
enemy = Hero("Batman", 19, 0, 65)
44+
45+
with self.assertRaises(ValueError) as ve:
46+
self.hero.battle(enemy)
47+
48+
self.assertEqual("You cannot fight Batman. He needs to rest", str(ve.exception))
49+
50+
def test_battle_with_less_than_zero_enemy_health_raises_value_error(self):
51+
enemy = Hero("Batman", 19, -1, 65)
52+
53+
with self.assertRaises(ValueError) as ve:
54+
self.hero.battle(enemy)
55+
56+
self.assertEqual("You cannot fight Batman. He needs to rest", str(ve.exception))
57+
58+
def test_battle_draw(self):
59+
enemy = Hero("Batman", 7, 100, 15)
60+
result = self.hero.battle(enemy)
61+
health = 100 - (7 * 15)
62+
self.assertEqual(health, self.hero.health)
63+
self.assertEqual(health, enemy.health)
64+
self.assertEqual("Draw", result)
65+
66+
def test_hero_wins(self):
67+
enemy = Hero("Batman", 2, 1, 1)
68+
69+
result = self.hero.battle(enemy)
70+
enemy_health = 1 - (7 * 15)
71+
hero_level = 7 + 1
72+
hero_health = (100 - 2) + 5
73+
hero_damage = 15 + 5
74+
75+
self.assertEqual(enemy_health, enemy.health)
76+
self.assertEqual(hero_level, self.hero.level)
77+
self.assertEqual(hero_health, self.hero.health)
78+
self.assertEqual(hero_damage, self.hero.damage)
79+
self.assertEqual("You win", result)
80+
81+
def test_enemy_wins(self):
82+
enemy = Hero("Batman", 7, 100, 15)
83+
self.hero = Hero("Superman", 2, 1, 1)
84+
85+
result = self.hero.battle(enemy)
86+
hero_health = 1 - (7 * 15)
87+
enemy_level = 7 + 1
88+
enemy_health = (100 - 2) + 5
89+
enemy_damage = 15 + 5
90+
91+
self.assertEqual(hero_health, self.hero.health)
92+
self.assertEqual(enemy_level, enemy.level)
93+
self.assertEqual(enemy_health, enemy.health)
94+
self.assertEqual(enemy_damage, enemy.damage)
95+
self.assertEqual("You lose", result)
96+
97+
def test_str_method_successful_string_return(self):
98+
result = f"Hero Superman: 7 lvl\n" \
99+
f"Health: 100\n" \
100+
f"Damage: 15\n"
101+
102+
self.assertEqual(result, str(self.hero))
103+
104+
105+
if __name__ == '__main__':
106+
main()
Binary file not shown.

OOP/A.Testing/Testing - Exercise/04. Student/tests/__init__.py

Whitespace-only changes.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from unittest import TestCase, main
2+
from project.student import Student
3+
4+
5+
class TestStudent(TestCase):
6+
7+
def setUp(self) -> None:
8+
self.student = Student("Dimitar", {"Python-OOP": ["Exam prep", "Exam prep", "Reg exam"]})
9+
10+
def test_successful_initialization(self):
11+
self.assertEqual("Dimitar", self.student.name)
12+
self.assertEqual({"Python-OOP": ["Exam prep", "Exam prep", "Reg exam"]}, self.student.courses)
13+
14+
def test_enroll_course_exists(self):
15+
result = self.student.enroll("Python-OOP", ["Retake exam"])
16+
self.assertEqual(["Exam prep", "Exam prep", "Reg exam", "Retake exam"], self.student.courses["Python-OOP"])
17+
self.assertEqual("Course already added. Notes have been updated.", result)
18+
19+
def test_enroll_add_course_notes(self):
20+
result = self.student.enroll("PY WEB", ["Django", "Flask"])
21+
self.assertEqual(["Django", "Flask"], self.student.courses["PY WEB"])
22+
self.assertEqual("Course and course notes have been added.", result)
23+
24+
25+
def test_enroll_without_notes(self):
26+
result = self.student.enroll("Basics", ["Some Note"], "No")
27+
self.assertEqual([], self.student.courses["Basics"])
28+
self.assertEqual("Course has been added.", result)
29+
30+
def test_add_notes_course_not_found(self):
31+
with self.assertRaises(Exception) as ex:
32+
self.student.add_notes("Python-Fund", ["Some", "Notes"])
33+
34+
self.assertEqual("Cannot add notes. Course not found.", str(ex.exception))
35+
36+
def test_successful_add_notes(self):
37+
result = self.student.add_notes("Python-OOP", "Workshop")
38+
self.assertEqual(["Exam prep", "Exam prep", "Reg exam", "Workshop"], self.student.courses["Python-OOP"])
39+
self.assertEqual("Notes have been updated", result)
40+
41+
def test_leave_course_exception_course_not_found(self):
42+
with self.assertRaises(Exception) as ex:
43+
self.student.leave_course("Python-Fund")
44+
45+
self.assertEqual("Cannot remove course. Course not found.", str(ex.exception))
46+
47+
def test_successful_leave_course(self):
48+
result = self.student.leave_course("Python-OOP")
49+
self.assertEqual({}, self.student.courses)
50+
self.assertEqual("Course has been removed", result)
51+
52+
53+
if __name__ == '__main__':
54+
main()

0 commit comments

Comments
 (0)