# Day 3 in Andela bootcamp class(VI)

Do what i do but say the opposite”, a game we play at Andela sounds quite simple when you read it, however try moving left but say move right. Try it out, quite fun and challenging. The purpose of the game is to cultivate Adaptability. Imagine how strange it would be for drivers to drive on the right when accustomed to keeping left. As you can guess, this can cause a lot of havoc, unless one is able to unlearn and relearn the new rules as fast as possible.

Our FFT(food for thought), How to ensure code works? Was our topic of discussion, immediately we settled into class. Writing code is as simple as ‘ABCD’ however writing quality and “clean” code that not only runs but also does exactly what it is intended to do, is another thing altogether.

They are a number of way to ensure your code works:

  • Using plugins — these ensure minimal syntax errors that can be easily overlooked
  • Trying out your code — may not be very efficient for large blocks of code
  • Using debuggers (eg ipdb in python) — that aids in going over blocks of code and hopely catch and help one fix bugs
  • TDD- test driven development

Computers like humans need to be instructed on what to do. However, unlike humans computer can be programmed to compute values that meet certain criterias, they rarely output unexpected output (garbage in, garbage out) unless there is a problem with one’s code. This is the whole concept of TDD (Test driven development).

Test driven development is a software development process that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) automated test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards.

Example of a tree of a unittest folder i.e Main folder

.Main folder
├── code
│ ├── bank.py
│ ├── __init__.py
└── tests
│ ├── tests.py

To be honest they are a great number of things that i didn’t understand about reading and writing tests. I knew I had to import Unittest and pip install rednose. Ensure that that the my written code was in a folder that had an __init__.py file to make it a package. So that you can import in this case the (bank.py) in the tests.py file which is in another folder called tests. This knowledge only amounts to running the unittest.

Attention to detail

Example of unittests

class AccountBalanceTestCases(unittest.TestCase):
    def setUp(self):
       self.my_account = BankAccount(90)
    def test_balance(self):
       self.assertEqual(self.my_account.balance, 90, msg=’Account   Balance Invalid’)
  def test_deposit(self):
    self.my_account.deposit(90)
    self.assertEqual(self.my_account.balance, 180, msg=’Deposit method inaccurate’)
  def test_withdraw(self):
    self.my_account.withdraw(40)
    self.assertEqual(self.my_account.balance, 50, msg=’Withdraw          method inaccurate’)

Today i got to grasp that, the class , must always take in parameters unittest.TestCase to symbolize they it is inheriting from that parent and that your class (in this case AccountBalanceTestCases) is the child.

Given a chance to say how many tests are ran by the unittest above 4, may be your immediate answer. However , if you bare in mind that for a test to be counted as a test the function must begin with the word test (eg def test_balance). Then they are only 3 tests.

CHEERS!

I hope you have a few takeaways from this, thanks for reading check me out tomorrow.

Show your support

Clapping shows how much you appreciated Margaret A.O’s story.