def test_transfer_positive_mount_should_work(self): account_1 = BankAccount('For testing', 100, '$') account_2 = BankAccount('In dollars', 10, '$') account_1.transfer_to(account_2, 50) self.assertEqual(account_1.balance(), 50) self.assertEqual(account_2.balance(), 60)
def test_transfer_more_than_vailable_balance_should_fail(self): account_1 = BankAccount('For testing', 100, '$') account_2 = BankAccount('In dollars', 10, '$') with self.assertRaises(Exception): account_1.transfer_to(account_2, 150) self.assertEqual(account_1.balance(), 100) self.assertEqual(account_2.balance(), 10)
def test_transfer_negative_amount(self): account_1 = BankAccount('For testing', 100, '$') account_2 = BankAccount('In dollars', 10, '$') with self.assertRaises(ValueError): account_1.transfer_to(account_2, -50) self.assertEqual(account_1.balance(), 100) self.assertEqual(account_2.balance(), 10)
def test_transfer_between_different_currencies_not_possible(self): leva_account = BankAccount('For testing', 100, 'BGN') dollar_account = BankAccount('In dollars', 10, '$') with self.assertRaises(TypeError): leva_account.transfer_to(dollar_account, 50) self.assertEqual(leva_account.balance(), 100) self.assertEqual(dollar_account.balance(), 10)
class TestBankAccount_transfer_to(unittest.TestCase): ''' It is also possible to create test classes that test a single method. In this case we want a separate class for testing the BankAccount.transfer_to() method because there are several scenarios and we want to better organize them. ''' def setUp(self): self.account = BankAccount('For testing', 100, 'BGN') def test_transfer_to_without_parameters(self): # raises when other account is missing with self.assertRaises(TypeError): self.account.transfer_to(1) # can be written as self.assertRaises(TypeError, self.account.transfer_to, 1) # raises when how_miuch is missing other_account = BankAccount('with zero balance', 0, '$') with self.assertRaises(TypeError): self.account.transfer_to(other_account) # this is not necessary ???? self.assertEqual(other_account.balance(), 0) self.assertEqual(self.account.balance(), 100) def test_transfer_between_different_currencies_not_possible(self): leva_account = BankAccount('For testing', 100, 'BGN') dollar_account = BankAccount('In dollars', 10, '$') with self.assertRaises(TypeError): leva_account.transfer_to(dollar_account, 50) self.assertEqual(leva_account.balance(), 100) self.assertEqual(dollar_account.balance(), 10) def test_transfer_negative_amount(self): account_1 = BankAccount('For testing', 100, '$') account_2 = BankAccount('In dollars', 10, '$') with self.assertRaises(ValueError): account_1.transfer_to(account_2, -50) self.assertEqual(account_1.balance(), 100) self.assertEqual(account_2.balance(), 10) def test_transfer_positive_mount_should_work(self): account_1 = BankAccount('For testing', 100, '$') account_2 = BankAccount('In dollars', 10, '$') account_1.transfer_to(account_2, 50) self.assertEqual(account_1.balance(), 50) self.assertEqual(account_2.balance(), 60) def test_transfer_more_than_vailable_balance_should_fail(self): account_1 = BankAccount('For testing', 100, '$') account_2 = BankAccount('In dollars', 10, '$') with self.assertRaises(Exception): account_1.transfer_to(account_2, 150) self.assertEqual(account_1.balance(), 100) self.assertEqual(account_2.balance(), 10)