def test_good_example_of_using_with_block(self): account = BankAccount("Test", 100, "BGN") # .... 100 more lines of code raise ValueError('Can you find me!') # and few more with self.assertRaises(ValueError): account.deposit(-10)
def test_history(self): account = BankAccount("Test", 0, "$") account.deposit(20) account.balance() int(account) expected = ["Account was created", "Deposited 20$", "Balance check -> 20$", "__int__ check -> 20$"] self.assertEqual(account.history(), expected)
def test_bad_example_of_using_with_block(self): with self.assertRaises(ValueError): account = BankAccount("Test", 100, "") # DON'T stuff everything inside a with block # b/c you may catch exceptions which are not coming # from the method under test and you will never know # about them raise ValueError('Can you find me!') # .... 100 more lines of code account.deposit(-10)
def test_deposit_in_not_empty_account(self): account = BankAccount("Ivo", 1000, "$") account.deposit(500) self.assertEqual(account.balance(), 1500)
class TestBankAccount(unittest.TestCase): def setUp(self): self.account = BankAccount("Rado", 0, "$") def test_can_create_bank_account(self): '''' Sanity test that nothing went wrong in the init method. Not really a very practical one but students are full of suprprizes ''' self.assertTrue(isinstance(self.account, BankAccount)) # which is the same as self.assertIsInstance(self.account, BankAccount) def test_initial_zero_balance(self): self.assertEqual(self.account.balance(), 0) def test_good_example_of_using_with_block(self): account = BankAccount("Test", 100, "BGN") # .... 100 more lines of code raise ValueError('Can you find me!') # and few more with self.assertRaises(ValueError): account.deposit(-10) def test_bad_example_of_using_with_block(self): with self.assertRaises(ValueError): account = BankAccount("Test", 100, "") # DON'T stuff everything inside a with block # b/c you may catch exceptions which are not coming # from the method under test and you will never know # about them raise ValueError('Can you find me!') # .... 100 more lines of code account.deposit(-10) def test_negative_initial_amount(self): with self.assertRaises(ValueError): acc = BankAccount("Test", -100, "$") BankAccount("Test", 100, "") # this is not possible b/c acc is available only # inside the with block. # print acc with self.assertRaises(ValueError): BankAccount("Test", 100, "") def test_init_with_empty_currency(self): with self.assertRaises(ValueError): BankAccount("Test", 100, "") def test_deposit_in_empty_account(self): self.account.deposit(500) self.assertEqual(self.account.balance(), 500) def test_deposit_in_not_empty_account(self): account = BankAccount("Ivo", 1000, "$") account.deposit(500) self.assertEqual(account.balance(), 1500) def test_deposit_negative_amount(self): with self.assertRaises(ValueError): self.account.deposit(-100) def test_withdraw_from_not_empty_account(self): self.account.deposit(100) result = self.account.withdraw(50) self.assertTrue(result) self.assertEqual(self.account.balance(), 50) def test_withdraw_from_empty_account(self): result = self.account.withdraw(50) self.assertIsNotNone(result) self.assertFalse(result) def test_history(self): account = BankAccount("Test", 0, "$") account.deposit(20) account.balance() int(account) expected = [ "Account was created", "Deposited 20$", "Balance check -> 20$", "__int__ check -> 20$" ] self.assertEqual(account.history(), expected)