SlideShare a Scribd company logo
Unit Testing - Golang
Sofian Hadiwijaya
@sofianhw
What is TDD?
Test-driven development (TDD)
is a software development
process that relies on the
repetition of a very short
development cycle:
requirements are turned into
very specific test cases, then the
software is improved to pass
the new tests, only. This is
opposed to software
development that allows
software to be added that is not
proven to meet requirements.
Why TDD Needed?
The primary goal of TDD is, it's specification and not
validation. In other words, it’s one way to think through your
requirements or design before your write your functional
code. The goal of TDD is to write clean code that works
without wasting much of the development time. Main
benefits are :
• Easy to understand the specifications
• Takes much less time for debugging
• Coder can prove that the code meets the requirements
• Shorter development cycles
TDD Cycle
Characteristics of a Good Unit Test
• It should run fast. If the tests are slow, they will not be run often.
• Separates or mock environmental dependencies such as databases,
file systems, networks etc. These dependencies will slow the test
process, and a failure does not give meaningful feedback about
what the problem actually is.
• The scope should be clear. If the test fails, it's obvious where to look
for the problem. It's important to only test one thing in a single test.
• If the tests require special environmental setup or fail unexpectedly,
then they are not good unit tests. Change them for simplicity and
reliability. Tests should run and pass on any machine. The "works on
my box" excuse doesn't work.
• Clearly reveals its intention. Another developer can look at the test
and understand what is expected of the production code.
Unit Test in Golang
• Go has a built-in testing command called go test and a package
testing which combine to give a minimal but complete testing
experience.
• The standard tool-chain also includes benchmarking and
statement-based code coverage
Good Unit Test
Characteristics of a Golang test function:
• The first and only parameter needs to be t *testing.T
• It begins with the word Test followed by a word or phrase
starting with a capital letter.(usually the method under test i.e.
TestValidateClient)
• Calls t.Error or t.Fail to indicate a failure (I called t.Errorf to
provide more details)t.Log can be used to provide non-failing
debug information
• Must be saved in a file named something_test.go such as:
addition_test.go
Writing Test
Writing Test
Test Table
Launching Test
There are two ways to launch tests for a package. These methods
work for unit tests and integration tests alike.
1. Within the same directory as the test:
go test
This picks up any files matching packagename_test.go
2. By fully-qualified package name
go test github.com/sofianhw/golang
Cover
The go test tool has built-in code-coverage for statements. To try it
with out example above type in:
Demo
Thanks !!!
www.sofianhw.com @sofianhw sofian@pinjam.co.id

More Related Content

What's hot

Unit Testing
Unit TestingUnit Testing
Unit Testing
Scott Leberknight
 
Karate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made SimpleKarate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made Simple
VodqaBLR
 
Recon like a pro
Recon like a proRecon like a pro
Recon like a pro
Nirmalthapa24
 
Secure Code Review 101
Secure Code Review 101Secure Code Review 101
Secure Code Review 101
Narudom Roongsiriwong, CISSP
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPTsuhasreddy1
 
Secure code
Secure codeSecure code
Secure code
ddeogun
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
ikhwanhayat
 
Robot Framework Dos And Don'ts
Robot Framework Dos And Don'tsRobot Framework Dos And Don'ts
Robot Framework Dos And Don'ts
Pekka Klärck
 
Sanity testing and smoke testing
Sanity testing and smoke testingSanity testing and smoke testing
Sanity testing and smoke testing
MUHAMMAD FARHAN ASLAM
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)
David Ehringer
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practicesnickokiss
 
TDD refresher
TDD refresherTDD refresher
TDD refresher
Kerry Buckley
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelines
Ankur Goyal
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
John Blum
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Sachithra Gayan
 
SAST vs. DAST: What’s the Best Method For Application Security Testing?
SAST vs. DAST: What’s the Best Method For Application Security Testing?SAST vs. DAST: What’s the Best Method For Application Security Testing?
SAST vs. DAST: What’s the Best Method For Application Security Testing?
Cigital
 
Security testing
Security testingSecurity testing
Security testingbaskar p
 
Secure coding presentation Oct 3 2020
Secure coding presentation Oct 3 2020Secure coding presentation Oct 3 2020
Secure coding presentation Oct 3 2020
Moataz Kamel
 
TDD - Agile
TDD - Agile TDD - Agile
TDD - Agile
harinderpisces
 
Software testing & Quality Assurance
Software testing & Quality Assurance Software testing & Quality Assurance
Software testing & Quality Assurance
Webtech Learning
 

What's hot (20)

Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Karate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made SimpleKarate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made Simple
 
Recon like a pro
Recon like a proRecon like a pro
Recon like a pro
 
Secure Code Review 101
Secure Code Review 101Secure Code Review 101
Secure Code Review 101
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
 
Secure code
Secure codeSecure code
Secure code
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
 
Robot Framework Dos And Don'ts
Robot Framework Dos And Don'tsRobot Framework Dos And Don'ts
Robot Framework Dos And Don'ts
 
Sanity testing and smoke testing
Sanity testing and smoke testingSanity testing and smoke testing
Sanity testing and smoke testing
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
 
TDD refresher
TDD refresherTDD refresher
TDD refresher
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelines
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
SAST vs. DAST: What’s the Best Method For Application Security Testing?
SAST vs. DAST: What’s the Best Method For Application Security Testing?SAST vs. DAST: What’s the Best Method For Application Security Testing?
SAST vs. DAST: What’s the Best Method For Application Security Testing?
 
Security testing
Security testingSecurity testing
Security testing
 
Secure coding presentation Oct 3 2020
Secure coding presentation Oct 3 2020Secure coding presentation Oct 3 2020
Secure coding presentation Oct 3 2020
 
TDD - Agile
TDD - Agile TDD - Agile
TDD - Agile
 
Software testing & Quality Assurance
Software testing & Quality Assurance Software testing & Quality Assurance
Software testing & Quality Assurance
 

Similar to TDD and Unit Testing in Golang

Test driven development
Test driven developmentTest driven development
Test driven development
Harry Potter
 
Test driven development
Test driven developmentTest driven development
Test driven development
Luis Goldster
 
Test driven development
Test driven developmentTest driven development
Test driven development
Tony Nguyen
 
Test driven development
Test driven developmentTest driven development
Test driven development
Young Alista
 
Test driven development
Test driven developmentTest driven development
Test driven development
James Wong
 
Test driven development
Test driven developmentTest driven development
Test driven development
Fraboni Ec
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentbhochhi
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Sergey Aganezov
 
Test driven development(tdd)
Test driven development(tdd)Test driven development(tdd)
Test driven development(tdd)
Omar Youssef Shiha
 
Test driven development
Test driven developmentTest driven development
Test driven development
namkha87
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven DevelopmentMeilan Ou
 
Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)
Abhijeet Vaikar
 
Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++
Hong Le Van
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
Anuj Arora
 
Week 14 Unit Testing.pptx
Week 14  Unit Testing.pptxWeek 14  Unit Testing.pptx
Week 14 Unit Testing.pptx
mianshafa
 
{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx
AmalEldhose2
 
Test-Driven Development Reference Card
Test-Driven Development Reference CardTest-Driven Development Reference Card
Test-Driven Development Reference CardSeapine Software
 
Tdd
TddTdd
Test driven development
Test driven developmentTest driven development
Test driven development
Nascenia IT
 

Similar to TDD and Unit Testing in Golang (20)

Test driven development
Test driven developmentTest driven development
Test driven development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test driven development(tdd)
Test driven development(tdd)Test driven development(tdd)
Test driven development(tdd)
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)
 
Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++
 
Lecture 21
Lecture 21Lecture 21
Lecture 21
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Week 14 Unit Testing.pptx
Week 14  Unit Testing.pptxWeek 14  Unit Testing.pptx
Week 14 Unit Testing.pptx
 
{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx
 
Test-Driven Development Reference Card
Test-Driven Development Reference CardTest-Driven Development Reference Card
Test-Driven Development Reference Card
 
Tdd
TddTdd
Tdd
 
Test driven development
Test driven developmentTest driven development
Test driven development
 

More from Sofian Hadiwijaya

Workshop Web3 Weekend Social Impact
Workshop Web3 Weekend Social ImpactWorkshop Web3 Weekend Social Impact
Workshop Web3 Weekend Social Impact
Sofian Hadiwijaya
 
Warung Pintar Social Impact Report 2018
Warung Pintar Social Impact Report 2018Warung Pintar Social Impact Report 2018
Warung Pintar Social Impact Report 2018
Sofian Hadiwijaya
 
Agile and Scrum 101
Agile and Scrum 101Agile and Scrum 101
Agile and Scrum 101
Sofian Hadiwijaya
 
Execute Idea
Execute Idea Execute Idea
Execute Idea
Sofian Hadiwijaya
 
Product market fit and Scale
Product market fit and ScaleProduct market fit and Scale
Product market fit and Scale
Sofian Hadiwijaya
 
Entrepreneur Story
Entrepreneur StoryEntrepreneur Story
Entrepreneur Story
Sofian Hadiwijaya
 
Pathway became data scientist
Pathway became data scientistPathway became data scientist
Pathway became data scientist
Sofian Hadiwijaya
 
Keynote Speaker PyConID 2018
Keynote Speaker PyConID 2018Keynote Speaker PyConID 2018
Keynote Speaker PyConID 2018
Sofian Hadiwijaya
 
Building Startups
Building StartupsBuilding Startups
Building Startups
Sofian Hadiwijaya
 
Big data and digital marketing
Big data  and digital marketingBig data  and digital marketing
Big data and digital marketing
Sofian Hadiwijaya
 
Data Driven Organization
Data Driven OrganizationData Driven Organization
Data Driven Organization
Sofian Hadiwijaya
 
Data Driven Company
Data Driven CompanyData Driven Company
Data Driven Company
Sofian Hadiwijaya
 
serverless web application
serverless web applicationserverless web application
serverless web application
Sofian Hadiwijaya
 
Startup 101
Startup 101Startup 101
Startup 101
Sofian Hadiwijaya
 
IoT and AI for Retail Industry
IoT and AI for Retail IndustryIoT and AI for Retail Industry
IoT and AI for Retail Industry
Sofian Hadiwijaya
 
Growth in Startup
Growth in StartupGrowth in Startup
Growth in Startup
Sofian Hadiwijaya
 
Technology Industry
Technology Industry Technology Industry
Technology Industry
Sofian Hadiwijaya
 
What you can get with data
What you can get with dataWhat you can get with data
What you can get with data
Sofian Hadiwijaya
 
DeepLearning with Neon
DeepLearning with NeonDeepLearning with Neon
DeepLearning with Neon
Sofian Hadiwijaya
 
How BigData Affects Business
How BigData Affects BusinessHow BigData Affects Business
How BigData Affects Business
Sofian Hadiwijaya
 

More from Sofian Hadiwijaya (20)

Workshop Web3 Weekend Social Impact
Workshop Web3 Weekend Social ImpactWorkshop Web3 Weekend Social Impact
Workshop Web3 Weekend Social Impact
 
Warung Pintar Social Impact Report 2018
Warung Pintar Social Impact Report 2018Warung Pintar Social Impact Report 2018
Warung Pintar Social Impact Report 2018
 
Agile and Scrum 101
Agile and Scrum 101Agile and Scrum 101
Agile and Scrum 101
 
Execute Idea
Execute Idea Execute Idea
Execute Idea
 
Product market fit and Scale
Product market fit and ScaleProduct market fit and Scale
Product market fit and Scale
 
Entrepreneur Story
Entrepreneur StoryEntrepreneur Story
Entrepreneur Story
 
Pathway became data scientist
Pathway became data scientistPathway became data scientist
Pathway became data scientist
 
Keynote Speaker PyConID 2018
Keynote Speaker PyConID 2018Keynote Speaker PyConID 2018
Keynote Speaker PyConID 2018
 
Building Startups
Building StartupsBuilding Startups
Building Startups
 
Big data and digital marketing
Big data  and digital marketingBig data  and digital marketing
Big data and digital marketing
 
Data Driven Organization
Data Driven OrganizationData Driven Organization
Data Driven Organization
 
Data Driven Company
Data Driven CompanyData Driven Company
Data Driven Company
 
serverless web application
serverless web applicationserverless web application
serverless web application
 
Startup 101
Startup 101Startup 101
Startup 101
 
IoT and AI for Retail Industry
IoT and AI for Retail IndustryIoT and AI for Retail Industry
IoT and AI for Retail Industry
 
Growth in Startup
Growth in StartupGrowth in Startup
Growth in Startup
 
Technology Industry
Technology Industry Technology Industry
Technology Industry
 
What you can get with data
What you can get with dataWhat you can get with data
What you can get with data
 
DeepLearning with Neon
DeepLearning with NeonDeepLearning with Neon
DeepLearning with Neon
 
How BigData Affects Business
How BigData Affects BusinessHow BigData Affects Business
How BigData Affects Business
 

Recently uploaded

ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
CatarinaPereira64715
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 

Recently uploaded (20)

ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 

TDD and Unit Testing in Golang

  • 1. Unit Testing - Golang Sofian Hadiwijaya @sofianhw
  • 2. What is TDD? Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: requirements are turned into very specific test cases, then the software is improved to pass the new tests, only. This is opposed to software development that allows software to be added that is not proven to meet requirements.
  • 3. Why TDD Needed? The primary goal of TDD is, it's specification and not validation. In other words, it’s one way to think through your requirements or design before your write your functional code. The goal of TDD is to write clean code that works without wasting much of the development time. Main benefits are : • Easy to understand the specifications • Takes much less time for debugging • Coder can prove that the code meets the requirements • Shorter development cycles
  • 5. Characteristics of a Good Unit Test • It should run fast. If the tests are slow, they will not be run often. • Separates or mock environmental dependencies such as databases, file systems, networks etc. These dependencies will slow the test process, and a failure does not give meaningful feedback about what the problem actually is. • The scope should be clear. If the test fails, it's obvious where to look for the problem. It's important to only test one thing in a single test. • If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests. Change them for simplicity and reliability. Tests should run and pass on any machine. The "works on my box" excuse doesn't work. • Clearly reveals its intention. Another developer can look at the test and understand what is expected of the production code.
  • 6. Unit Test in Golang • Go has a built-in testing command called go test and a package testing which combine to give a minimal but complete testing experience. • The standard tool-chain also includes benchmarking and statement-based code coverage
  • 7. Good Unit Test Characteristics of a Golang test function: • The first and only parameter needs to be t *testing.T • It begins with the word Test followed by a word or phrase starting with a capital letter.(usually the method under test i.e. TestValidateClient) • Calls t.Error or t.Fail to indicate a failure (I called t.Errorf to provide more details)t.Log can be used to provide non-failing debug information • Must be saved in a file named something_test.go such as: addition_test.go
  • 11. Launching Test There are two ways to launch tests for a package. These methods work for unit tests and integration tests alike. 1. Within the same directory as the test: go test This picks up any files matching packagename_test.go 2. By fully-qualified package name go test github.com/sofianhw/golang
  • 12. Cover The go test tool has built-in code-coverage for statements. To try it with out example above type in:
  • 13. Demo