Skip to content

Commit df42418

Browse files
#13 - Python
1 parent a0ea535 commit df42418

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import unittest
2+
"""
3+
* EJERCICIO:
4+
* Crea una función que se encargue de sumar dos números y retornar
5+
* su resultado.
6+
* Crea un test, utilizando las herramientas de tu lenguaje, que sea
7+
* capaz de determinar si esa función se ejecuta correctamente.
8+
"""
9+
10+
def add(n1, n2):
11+
if not isinstance(n1, (int, float)) or not isinstance(n2, (int, float)):
12+
raise TypeError("Args must be integer or float numbers")
13+
return n1 + n2
14+
15+
16+
class TestAddMethod(unittest.TestCase):
17+
def test_postivite(self):
18+
self.assertEqual(add(10, 2), 12)
19+
self.assertEqual(add(5, 3), 8)
20+
self.assertEqual(add(5, -3), 2)
21+
22+
23+
def test_negative(self):
24+
self.assertEqual(add(-2, -3), -5)
25+
self.assertEqual(add(-5, 3), -2)
26+
27+
def test_str(self):
28+
with self.assertRaises(TypeError):
29+
add("5", 2)
30+
with self.assertRaises(TypeError):
31+
add(5, "2")
32+
with self.assertRaises(TypeError):
33+
add("5", "2")
34+
35+
# if __name__ == "__main__":
36+
# unittest.main()
37+
38+
"""
39+
* DIFICULTAD EXTRA (opcional):
40+
* Crea un diccionario con las siguientes claves y valores:
41+
* "name": "Tu nombre"
42+
* "age": "Tu edad"
43+
* "birth_date": "Tu fecha de nacimiento"
44+
* "programming_languages": ["Listado de lenguajes de programación"]
45+
* Crea dos test:
46+
* - Un primero que determine que existen todos los campos.
47+
* - Un segundo que determine que los datos introducidos son correctos.
48+
"""
49+
50+
info = dict(name="duban", age=25, birth_date="unknown",
51+
programming_languages=["Python", "Java"])
52+
53+
54+
class TestDictValues(unittest.TestCase):
55+
56+
def setUp(self):
57+
"""
58+
Setting before each proof.
59+
Create the dictionary 'info' with the proof data
60+
"""
61+
62+
self.info = dict(
63+
name="duban",
64+
age=25,
65+
birth_date="unknown",
66+
programming_languages=["Python", "Java"]
67+
)
68+
69+
def tearDown(self):
70+
"""
71+
Clean after each proof.
72+
Reset the dictionary (optional in this case but util that we need release space)
73+
"""
74+
self.info = None
75+
76+
77+
def test_keys_required(self):
78+
"""
79+
Verify that dictionary info contain the required keys
80+
"""
81+
keys_required = {"name", "age", "birth_date", "programming_languages"}
82+
self.assertTrue(keys_required.issubset(self.info.keys()))
83+
84+
def test_data_types(self):
85+
"""
86+
Verify that dictionary values are correct
87+
"""
88+
self.assertIsInstance(self.info["name"], str, "The name must be string")
89+
self.assertIsInstance(self.info["age"], int, "The age must be integer")
90+
self.assertIsInstance(self.info["birth_date"], str, "The birth date must be string")
91+
self.assertIsInstance(self.info["programming_languages"], list, "The programming languages must be list")
92+
93+
def test_programming_languages_noempty(self):
94+
"""
95+
Verify that list is not empty
96+
"""
97+
self.assertGreater(len(self.info["programming_languages"]), 0, "The programming languages" +
98+
"list must contain values")
99+
100+
def test_programming_lang_content(self):
101+
"""
102+
Verify the content of programming languages list
103+
"""
104+
for language in self.info["programming_languages"]:
105+
self.assertIsInstance(language, str, f"The language {language} is not string")
106+
107+
def test_data_correct(self):
108+
"""
109+
Verify that each dictionary values are correct
110+
"""
111+
self.assertEqual(self.info["name"], "duban", "The name is not correct")
112+
self.assertEqual(self.info["age"], 25, "The age is not correct")
113+
self.assertEqual(self.info["birth_date"], "unknown", "The birth date is not correct")
114+
self.assertEqual(self.info["programming_languages"], ["Python", "Java"],
115+
"The programming languages are not correct")
116+
117+
118+
if __name__ == "__main__":
119+
unittest.main()

0 commit comments

Comments
 (0)