Newbie — Learn TDD in 5 mins

Shrawan K Sharma
4 min readMar 14, 2023

--

What is TDD ?

WAIT -> TAKE DEEP BREATH -> THINK (ANALYS)-> ASK QUESTIONS -> ASK QUESTIONS -> ASK QUESTIONS -> IMPLEMENT

What is developer life without TDD ?

IMPLEMENT -> THINK (ANALYS) -> TAKE DEEP BREATH -> ASK QUESTIONS -> IMPLEMENT -> ASK QUESTIONS -> IMPLEMENT -> TAKE DEEP BREATH -> ASK QUESTIONS -> ASK QUESTIONS IN NEXT SPRINT

Life without TDD:

-> IMPLEMENT API CALL 


Create Viewmodel
Create ServiceLayer
Create payload
Create Response Object
Write businees logic in viewmodel
Write logic in Viewcontroller to show data

Ask manager:-
API is not giving "xyz" key what I should do ?
What will happen if there is no data ?
What will happen if there is no internet connection ?
What will happen if user is admin ?

Implement above cases.....

Give build to QA:-

Bugs reported by QA
UI issue
Text filed must contain minimum length
Invalid json format error
Why you are showing loader in the screen


Meeting with QA & Manger to discuss bugs
Let's have validation in text field
JSON might contain invalid format
Show loader while loading screen

Meeting with Backend, QA & Manger to discuss
Response from server

Implement

Life with TDD

-> IMPLEMENT API CALL 

Ask manager:-
API is not giving "xyz" key what I should do ?
What will happen if there is no data ?
What will happen if there is no internet connection ?
What will happen if user is admin ?

Ask QA :-
Text filed must contain minimum length ?
Invalid json format error?
Show loader in the screen ?

Meeting with QA & Manger to discuss scnerio:-
Let's have validation in text field ?
JSON might contain invalid format ?
Show loader while loading screen ?

Meeting with Backend, QA & Manger to discuss scnerio:-
Finilize error message

Write all test cases on each module
test cases -> Create Viewmodel
test cases -> Create ServiceLayer
test cases -> Create payload
test cases -> Create Response Object
test cases -> Write businees logic in viewmodel
test cases -> Write logic in Viewcontroller to show data

Before learning TDD, you should know/have courage to ask questions.

Let’s play around with one simple scenario:-

As a user, I will input 2 numbers and black box should return sum of 2 numbers

Ask questions to stack holder…

What is the range of number?
Can number be negative?
Does number contain float?
What is the precious after adding 2 float number?
How does rounding take place after sum?
Shall I round number before adding?
What happen when 2 large number is added?
What happens if memory out of range issue occur?
Does black box throw error ?
In there any case of exception?
What if multiple api call happens ?
Is there any generic error message ?
What should be error message?
Can you share list of error?
What happen system memory is full?
Is there case of race conditions ?
What is the maximum size number?
Is there any case where input is string ?
Shall I create generic black box ?
Shall I allow string and int sum?
……
…….
…….
…….
……..

Test cases

What is the range of number? -> testCaseCheckingRangeOfNumber()
Can number be negative? -> testCasecheckingNegetiveNumberSum()
Does number contain float? -> testCaseaddingFloatNumber()
What is the precious after adding 2 float number? -> testCaseaddDifferentPreciousNumber()
How does rounding take place after sum? -> testCaseroundingNumberAfterAdding()
Shall I round number before adding? -> testCaseroundingNumberBeforeAdding()
What happen when 2 large number is added? -> testCaseAdding2LargeNumber()
What happens if memory out of range issue occur? -> testCaseCheckingOutOfMemory()
Does black box throw error ? -> testCasethrowingError()
In there any case of exception? -> testCaseExceptionHandling()
What if multiple api call happens ? -> testCasemultipleAPIcall()
Is there any generic error message ? -> testCaseerrorMessageCheck()
What should be error message? -> testCaseErrorMessageChecking()
Can you share list of error? -> testCaseSwitchCaseForErrorInNumber()
What happen system memory is full? -> testsystmMemoryFullEdgeCase()
Is there case of race conditions ? -> testCasecheckRaceCondition()
What is the maximum size number? -> testCasemaximumSizeNumber()
Is there any case where input is string ? -> testCaseCheckingInputAsString()
Shall I create generic black box ? -> testCaseCheckGenericBlackBox()
Shall I allow string and int sum? -> testCaseOneInputAsIntAndOtherAsString()
……
…….
…….
…….
……..

I know you are smart developer. You can easily implement Black Box/Sum function and write above test cases using XCTest framework :)

Main motive of this article is get gist out of TDD

Summary

- Don’t simply say TDD is writing test case before development.
- Main reason for introducing TDD is to ANALYS different scenarios beforehand.
- If you are asking questions after developing feature then TDD cannot help you.

Thanks Again for reading article.

Let’s hope you enjoyed first part of TDD of iOS Applications. But I love to hear from you: what works, what doesn’t? Did I leave anything out? Are there any TDD strategies that you’d like to see included here? 🙏🙏

If you found this interesting, you will enjoy these related articles I wrote:

--

--

Shrawan K Sharma
Shrawan K Sharma

Written by Shrawan K Sharma

iOS App Developer - Frontend(ReactJS)

No responses yet