def test_can_withdraw_money_sequentially(self): account = BankAccount() account.open() account.deposit(100) account.withdraw(20) account.withdraw(80) self.assertEqual(account.get_balance(), 0)
def test_open_already_opened_account(self): account = BankAccount() account.open() with self.assertRaises(ValueError) as err: account.open() self.assertEqual(type(err.exception), ValueError) self.assertEqual(err.exception.args[0], "account already open")
def test_deposit_into_closed_account(self): account = BankAccount() account.open() account.close() with self.assertRaisesWithMessage(ValueError): account.deposit(50)
def test_withdraw_from_closed_account(self): account = BankAccount() account.open() account.close() with self.assertRaisesWithMessage(ValueError): account.withdraw(50)
def test_reopened_account_does_not_retain_balance(self): account = BankAccount() account.open() account.deposit(50) account.close() account.open() self.assertEqual(account.get_balance(), 0)
def test_cannot_withdraw_more_than_deposited(self): account = BankAccount() account.open() account.deposit(25) with self.assertRaises(ValueError): account.withdraw(50)
def test_checking_balance_of_closed_account_throws_error(self): account = BankAccount() account.open() account.close() with self.assertRaisesWithMessage(ValueError): account.get_balance()
def test_can_deposit_money_sequentially(self): account = BankAccount() account.open() account.deposit(100) account.deposit(50) self.assertEqual(account.get_balance(), 150)
def test_cannot_withdraw_negative(self): account = BankAccount() account.open() account.deposit(100) with self.assertRaisesWithMessage(ValueError): account.withdraw(-50)
def test_can_withdraw_money(self): account = BankAccount() account.open() account.deposit(100) account.withdraw(50) self.assertEqual(account.get_balance(), 50)
def test_cannot_deposit_negative(self): account = BankAccount() account.open() with self.assertRaises(ValueError) as err: account.deposit(-50) self.assertEqual(type(err.exception), ValueError) self.assertEqual(err.exception.args[0], "amount must be greater than 0")
def test_can_handle_concurrent_transactions(self): account = BankAccount() account.open() account.deposit(1000) self.adjust_balance_concurrently(account) self.assertEqual(account.get_balance(), 1000)
def test_can_retrieve_previously_opened_account(): stored_account = BankAccount() stored_account.open() account_storage.store(stored_account) retrieved_account = account_storage.retrieve() assert stored_account.balance == retrieved_account.balance
def test_checking_balance_of_closed_account_throws_error(self): account = BankAccount() account.open() account.close() with self.assertRaises(ValueError) as err: account.get_balance() self.assertEqual(type(err.exception), ValueError) self.assertEqual(err.exception.args[0], "account not open")
def test_deposit_into_closed_account(self): account = BankAccount() account.open() account.close() with self.assertRaises(ValueError) as err: account.deposit(50) self.assertEqual(type(err.exception), ValueError) self.assertEqual(err.exception.args[0], "account not open")
def test_withdraw_from_closed_account(self): account = BankAccount() account.open() account.close() with self.assertRaises(ValueError) as err: account.withdraw(50) self.assertEqual(type(err.exception), ValueError) self.assertEqual(err.exception.args[0], "account not open")
def test_cannot_withdraw_more_than_deposited(self): account = BankAccount() account.open() account.deposit(25) with self.assertRaises(ValueError) as err: account.withdraw(50) self.assertEqual(type(err.exception), ValueError) self.assertEqual(err.exception.args[0], "amount must be less than balance")
def test_withdraw_below_minimum_balance_raises(): account = BankAccount(minimum_balance=1) account.open(amount=3) with pytest.raises( ValueError, match= "Cannot withdraw below minimum required balance of 1. You can withdraw a maximum of 2." ): account.withdraw(amount=3)
def test_cannot_open_account_with_less_than_minimum_balance( minimum_balance, amount): account = BankAccount(minimum_balance=minimum_balance) with pytest.raises( ValueError, match= f"Opening requires at least minimum balance of {minimum_balance}", ): account.open(amount=amount)
def retrieve() -> Optional[BankAccount]: try: with open(file='stored_account.yml', mode='r') as file_account: account_importer = yaml.load(file_account) account = BankAccount() account.open() account._balance = account_importer['_balance'] return account except FileNotFoundError: return None
class BankAccountTest(unittest.TestCase): """ The main bank account testing class. Here we define various methods which specifically test our module for bugs and errors. """ def setUp(self): self.account = BankAccount() def test_newly_opened_account_has_zero_balance(self): self.account.open() self.assertEqual(self.account.get_balance(), 0) def test_can_deposit_money(self): self.account.open() self.account.deposit(99) self.assertEqual(self.account.get_balance(), 99) def test_can_deposit_money_sequentially(self): self.account.open() self.account.deposit(20) self.account.deposit(30) self.assertEqual(self.account.get_balance(), 50) def test_can_withdraw_money(self): self.account.open() self.account.deposit(10) self.account.deposit(80) self.account.withdraw(30) self.account.withdraw(20) self.assertEqual(self.account.get_balance(), 40) def test_checking_balance_of_closed_account_throws_error(self): self.account.open() self.account.close() with self.assertRaises(ValueError): self.account.get_balance()
def test_open_already_opened_account(self): account = BankAccount() account.open() with self.assertRaisesWithMessage(ValueError): account.open()
def _create_account(amount): account = BankAccount() account.open() account.deposit(amount) return account
class BankAccountTest(unittest.TestCase): def setUp(self): self.account = BankAccount() def test_newly_opened_account_has_zero_balance(self): self.account.open() self.assertEqual(self.account.get_balance(), 0) def test_can_deposit_money(self): self.account.open() self.account.deposit(100) self.assertEqual(self.account.get_balance(), 100) def test_can_deposit_money_sequentially(self): self.account.open() self.account.deposit(100) self.account.deposit(50) self.assertEqual(self.account.get_balance(), 150) def test_can_withdraw_money(self): self.account.open() self.account.deposit(100) self.account.withdraw(50) self.assertEqual(self.account.get_balance(), 50) def test_can_withdraw_money_sequentially(self): self.account.open() self.account.deposit(100) self.account.withdraw(20) self.account.withdraw(80) self.assertEqual(self.account.get_balance(), 0) def test_checking_balance_of_closed_account_throws_error(self): self.account.open() self.account.close() with self.assertRaises(ValueError): self.account.get_balance() def test_deposit_into_closed_account(self): self.account.open() self.account.close() with self.assertRaises(ValueError): self.account.deposit(50) def test_withdraw_from_closed_account(self): self.account.open() self.account.close() with self.assertRaises(ValueError): self.account.withdraw(50) def test_cannot_withdraw_more_than_deposited(self): self.account.open() self.account.deposit(25) with self.assertRaises(ValueError): self.account.withdraw(50) def test_cannot_withdraw_negative(self): self.account.open() self.account.deposit(100) with self.assertRaises(ValueError): self.account.withdraw(-50) def test_cannot_deposit_negative(self): self.account.open() with self.assertRaises(ValueError): self.account.deposit(-50) def test_can_handle_concurrent_transactions(self): self.account.open() self.account.deposit(1000) for _ in range(10): self.adjust_balance_concurrently() def adjust_balance_concurrently(self): def transact(): self.account.deposit(5) time.sleep(0.001) self.account.withdraw(5) # Greatly improve the chance of an operation being interrupted # by thread switch, thus testing synchronization effectively try: sys.setswitchinterval(1e-12) except AttributeError: # For Python 2 compatibility sys.setcheckinterval(1) threads = [] for _ in range(1000): t = threading.Thread(target=transact) threads.append(t) t.start() for thread in threads: thread.join() self.assertEqual(self.account.get_balance(), 1000)
class BankAccountTest(unittest.TestCase): def setUp(self): self.account = BankAccount() # @unittest.skip def test_newly_opened_account_has_zero_balance(self): self.account.open() self.assertEqual(self.account.get_balance(), 0) # @unittest.skip def test_can_deposit_money(self): self.account.open() self.account.deposit(100) self.assertEqual(self.account.get_balance(), 100) # @unittest.skip def test_can_deposit_money_sequentially(self): self.account.open() self.account.deposit(100) self.account.deposit(50) self.assertEqual(self.account.get_balance(), 150) # @unittest.skip def test_can_withdraw_money(self): self.account.open() self.account.deposit(100) self.account.withdraw(50) self.assertEqual(self.account.get_balance(), 50) # @unittest.skip def test_can_withdraw_money_sequentially(self): self.account.open() self.account.deposit(100) self.account.withdraw(20) self.account.withdraw(80) self.assertEqual(self.account.get_balance(), 0) # @unittest.skip def test_checking_balance_of_closed_account_throws_error(self): self.account.open() self.account.close() with self.assertRaises(ValueError): self.account.get_balance() # @unittest.skip def test_deposit_into_closed_account(self): self.account.open() self.account.close() with self.assertRaises(ValueError): self.account.deposit(50) # @unittest.skip def test_withdraw_from_closed_account(self): self.account.open() self.account.close() with self.assertRaises(ValueError): self.account.withdraw(50) # @unittest.skip def test_cannot_withdraw_more_than_deposited(self): self.account.open() self.account.deposit(25) with self.assertRaises(ValueError): self.account.withdraw(50) # @unittest.skip def test_cannot_withdraw_negative(self): self.account.open() self.account.deposit(100) with self.assertRaises(ValueError): self.account.withdraw(-50) # @unittest.skip def test_cannot_deposit_negative(self): self.account.open() with self.assertRaises(ValueError): self.account.deposit(-50) # @unittest.skip def test_can_handle_concurrent_transactions(self): self.account.open() self.account.deposit(1000) for _ in range(10): self.adjust_balance_concurrently() # @unittest.skip def adjust_balance_concurrently(self): def transact(): self.account.deposit(5) time.sleep(0.001) self.account.withdraw(5) # Greatly improve the chance of an operation being interrupted # by thread switch, thus testing synchronization effectively try: sys.setswitchinterval(1e-12) except AttributeError: # For Python 2 compatibility sys.setcheckinterval(1) threads = [] for _ in range(1000): t = threading.Thread(target=transact) threads.append(t) t.start() for thread in threads: thread.join() self.assertEqual(self.account.get_balance(), 1000)
def test_newly_opened_account_has_zero_balance(self): account = BankAccount() account.open() self.assertEqual(account.get_balance(), 0)
def test_can_handle_initial_dollar_amount(self): account = BankAccount() account.open(amount=50) self.assertEqual(account.balance, 50)