Skip to main content

Posts

Showing posts with the label code test

Phases of TDD - Learn by example

I have already written on getting started with TDD . Let me extend it further and mention in theory what are all the phases involved in a TDD. Before jumping into this article, I hope you read the getting started with TDD article.   There might be more steps involved in TDD. However we're just going to discuss the phases of TDD and what do we do in each phase. /**  * @disclaimer  * I'm just preaching what I have learnt & practice   * Some parts of it could be wrong  * Please feel free to leave comments if am wrong  * And I would be happy to stand corrected */ The three phases of TDD are the, Red phase Green phase Blue phase Red phase This is the very first phase in TDD where we write a test for it to fail.  Green phase This is the second phase and immediate next phase to red phase where we write code for the test that we just wrote in the red phase to pass.  Blue phase This is the third and final phase of a test where you refactor the te...

TDD - How to go about it? - A quick start example

So, it's only recently I've been doing TDD. I have written unit tests before but those were all tests after writing the actual code. Confusing statement right? /**  * @disclaimer  * I'm just preaching what I practice and some parts of it could be wrong.  * Please feel free to leave comments if am wrong  * And I would be happy to stand corrected */ What is TDD? TDD, expanded as Test Driven Development could have this statement as one of the definitions. "TDD is nothing but the art of writing down the business use case one by one and write code based on it paralelly" It would feel like why do I need to write test for that? We could do the same with pen & paper and write codes. But then, you again circle back to write tests for the same. So why not do it this way?  Therefore, let's start with an example, Write a method that accepts two positive numbers as input and returns their sum Let's try and do a mock TDD for the above problem.  Case 1 - Function add...