Skip to content

Commit c4063c2

Browse files
committed
coverage tests examples
1 parent 783e822 commit c4063c2

File tree

12 files changed

+66
-19
lines changed

12 files changed

+66
-19
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,20 @@ python3.12 -m unittest discover -v -s tests
1010
# Run suites
1111
PYTHONPATH=. python3.12 tests/suites.py
1212

13+
# run tests
14+
```
15+
python3.12 -m unittest discover -v -s tests
16+
```
17+
18+
# Coverage
19+
```
20+
coverage run --source src -m unitest
21+
coverage report
22+
coverage html
23+
```
24+
25+
26+
# Bugs
27+
```bash
28+
# this comand not run if not change folder tests to other unique name
29+
coverage run --source ./src_tests -m unittest

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
ipdb==0.13.13
22
requests==2.32.3
33
Faker==33.3.0
4+
coverage==7.6.10

src/api_client.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import requests
22

3+
34
def get_location(ip):
45
url = f"https://freeipapi.com/api/json/{ip}"
56
response = requests.get(url)
@@ -9,7 +10,4 @@ def get_location(ip):
910
"country": data["countryName"],
1011
"region": data["regionName"],
1112
"city": data["cityName"],
12-
}
13-
14-
if __name__ == "__main__":
15-
print(get_location("8.8.8.8"))
13+
}
File renamed without changes.

tests/suites.py renamed to src_tests/suites.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from tests.test_bank_account import BankAccountTests
3+
from src_tests.test_bank_account import BankAccountTests
44

55

66
def bank_account_suite():
File renamed without changes.
Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ def test_deposit_increases_balance_by_deposit_amount(self):
2121
new_balance = self.account.deposit(500)
2222
self.assertEqual(new_balance, 1500, "El balance no es igual")
2323

24-
def test_withdraw_decreases_balance_by_withdraw_amount(self):
24+
@patch("src.bank_account.datetime")
25+
def test_withdraw_decreases_balance_by_withdraw_amount(self, mock_datetime):
26+
mock_datetime.now.return_value.hour = 10
2527
new_balance = self.account.withdraw(200)
2628
self.assertEqual(new_balance, 800, "El balance no es igual")
2729

@@ -37,7 +39,9 @@ def test_withdraw_logs_each_transaction(self):
3739
self.account.deposit(500)
3840
self.assertEqual(self._count_lines(self.account.log_file), 2)
3941

40-
def test_withdraw_raises_error_when_insufficient_funds(self):
42+
@patch("src.bank_account.datetime")
43+
def test_withdraw_raises_error_when_insufficient_funds(self, mock_datetime):
44+
mock_datetime.now.return_value.hour = 10
4145
with self.assertRaises(InsufficientFundsError):
4246
self.account.withdraw(2000)
4347

@@ -57,4 +61,18 @@ def test_withdraw_disallow_before_bussines_hours(self, mock_datetime):
5761
def test_withdraw_disallow_after_bussines_hours(self, mock_datetime):
5862
mock_datetime.now.return_value.hour = 18
5963
with self.assertRaises(WithdrawalTimeRestrictionError):
60-
self.account.withdraw(100)
64+
self.account.withdraw(100)
65+
66+
67+
def test_deposit_multiple_ammounts(self):
68+
69+
test_cases = [
70+
{"ammount": 100, "expected": 1100},
71+
{"ammount": 3000, "expected": 4000},
72+
{"ammount": 4500, "expected": 5500},
73+
]
74+
for case in test_cases:
75+
with self.subTest(case=case):
76+
self.account = BankAccount(balance=1000, log_file="transactions.txt")
77+
new_balance = self.account.deposit(case["ammount"])
78+
self.assertEqual(new_balance, case["expected"])

src_tests/test_calculator.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest
2+
3+
from src.calculator import sum, subtract, multiply, divide
4+
5+
6+
class CalculatorTests(unittest.TestCase):
7+
8+
def test_sum(self):
9+
assert sum(2, 3) == 5
10+
11+
def test_subtract(self):
12+
assert subtract(10, 5) == 5
13+
14+
def test_multiply(self):
15+
assert multiply(3, 2) == 6
16+
17+
def test_divide(self):
18+
result = divide(10, 2)
19+
expected = 5
20+
assert result == expected
21+
22+
def test_divide_by_zero(self):
23+
with self.assertRaises(ValueError):
24+
divide(10, 0)

0 commit comments

Comments
 (0)