Skip to content

Commit 9ff2fd0

Browse files
committed
pytest example
1 parent c4063c2 commit 9ff2fd0

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
ipdb==0.13.13
22
requests==2.32.3
33
Faker==33.3.0
4-
coverage==7.6.10
4+
coverage==7.6.10
5+
pytest==8.3.4

src/bank_account.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ def _log_transaction(self, message):
1414
f.write(f"{message}\n")
1515

1616
def deposit(self, amount):
17+
if amount < 0:
18+
raise ValueError("Amount cannot be negative")
1719
if amount > 0:
1820
self.balance += amount
1921
self._log_transaction(f"Deposited {amount}. New balance: {self.balance}")

src_tests/test_pytest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
from src.bank_account import BankAccount
3+
4+
5+
@pytest.mark.parametrize("ammount, expected", [
6+
(100, 1100),
7+
(3000, 4000),
8+
(4500, 5500),
9+
])
10+
def test_deposit_multiple_ammounts(ammount, expected):
11+
account = BankAccount(balance=1000, log_file="transactions.txt")
12+
new_balance = account.deposit(ammount)
13+
assert new_balance == expected
14+
15+
16+
# TODO: Create a test that does't allow the deposit to be less than zero
17+
def test_deposit_less_than_zero():
18+
account = BankAccount(balance=1000, log_file="transactions.txt")
19+
with pytest.raises(ValueError):
20+
account.deposit(-100)

0 commit comments

Comments
 (0)