SlideShare a Scribd company logo
1 of 33
TDD in Go with 
Ginkgo and Gomega 
Eddy Reyes - eddy@containable.io 
http://www.tasqr.io
About Me 
Eddy Reyes. Cofounder of Tasqr 
(www.tasqr.io) 
● Automates application deployment / 
configuration without code or config files. 
Previously hacked lots of code at: 
● IBM, Lifesize, Click Security
Overview of Presentation 
● Explain what TDD is 
o Not necessarily proselytizing that you must do it. 
o I dislike many TDD-related presentations, they don’t 
match my experience, they don’t stress what’s 
important IMO. 
● What Tools Does Go Have to Help You Do 
TDD? 
o Ginkgo and Gomega, many others
Further TDD Reading 
Uncle Bob Martin 
● http://blog.8thlight.com/uncle-bob/archive.html 
Kent Beck 
● Extreme Programming Explained (book) 
Is TDD Dead? (video debates) 
● Kent Beck, Martin Fowler, DHH 
● https://www.youtube.com/watch?v=z9quxZsLcfo (part 1)
What Isn’t TDD? 
● NOT just having written tests 
● NOT a way to feel like you’re going fast 
● NOT the Silver Bullet that saves software 
engineering 
● NOT like Axe, which makes you irresistible 
to the opposite sex :-)
What Is TDD? 
● A process you follow when writing software. 
● The process goes like this: 
o You write a test that defines an expected outcome in 
your program. 
o You watch the test fail. 
o You change the program until your test passes. 
● Adding tests to an already substantial 
untested program isn’t necessarily TDD.
How Do You Do TDD? 
● In order to effectively follow the TDD pattern, 
you must first understand your design. 
o Tests are rigid. Learning is free-form. 
o You learn by hacking and breaking things. 
o Tests ensure things don’t break. 
● TDD helps you SHIP, not LEARN
Learn vs. Ship 
● Learning is free-form exploration. 
Experiment, play, break things. 
o DO NOT SHIP THIS CODE! 
o After you learn your design, step away from the 
keyboard, and start anew. 
● Shipping 
o Code ends up in a product used by customers. 
o Quality is important!
Tests Are Design! 
● If tests are comprehensive, they are a formal 
specification of your program. 
● Writing a test is expressing the design of 
your program’s functionality. 
● To truly serve as a spec, tests must be: 
o Clean, readable 
o Follow some consistent notation
Test/Spec Notation 
GIVEN 
● Precondition 1...N 
WHEN 
● Call into your program 
THEN 
● Assert Condition 1...N
A Test Framework Needs... 
● Clean, simple notation 
● Clean setup/teardown of dependencies 
o Idempotent, independent tests! 
● Clean separation from tested code 
● Simple runner mechanism 
● Good reporting 
o test results 
o coverage maps
Testing in Go 
● Test runner 
o go test 
● Separates test code from product 
o *_test.go 
● Comes with a test writing library 
o “testing” package 
● Comes with coverage reporting tools 
o Beautiful HTML output
“testing” Package 
● Allows your to write test functions. 
● Nice benchmarking utility 
● Tests are basically blocks of code :( 
func TestXXX(t *testing.T) { 
// setup stuff 
// call your code 
if something.State != WhatIWant { 
t.Fail() 
} 
}
Better Test-Writing Library 
● go command has great tooling. 
● Need a better notation for specifying tests. 
… 
Ginkgo and Gomega is a step forward! 
… but not perfect!
Ginkgo and Gomega 
● Ginkgo 
o Compatible with “go test” but also comes with its 
own test runner 
o BDD-style test notation 
o Expressive structuring of tests 
● Gomega 
o “Matching” library 
o Allows for expressive assert-like statements
Ginkgo 
● Tests expressed as English statements 
o In the BDD tradition of Cucumber, et. al. 
var _ = Describe(“My component”, func() { 
It(“Does something special”, func() { 
MyFunction() 
Expect(MyVar).To(Equal(10)) 
}) 
})
Gomega 
● Assertion language 
Expect(number).To(Equal(10)) 
Expect(value).To(BeNumerically(“<”, 20)) 
Expect(someMap).To(Equal(otherMap)) // does reflect.DeepEqual() 
Expect(someMap).To(BeEquivalent(otherMap)) // allows different types 
Expect(myPtr).ToNot(BeNil()) 
Expect(flag).To(BeFalse()) 
Expect(err).ToNot(HaveOccurred()) 
Expect(err).To(MatchError(“my error message”))
Ginkgo and Dependencies 
● setup/teardown: 
var _ = Describe(“my config object”, func() { 
var ( 
configObj *config.MyConfig 
tempDir string 
) 
BeforeEach(func() { 
var err error 
tempDir, err = ioutil.TempDir(“”, “test-dir”) 
Expect(err).ToNot(HaveOccurred()) 
configObj = config.NewConfig() 
})
Ginkgo and Dependencies 
It(“Saves itself to file”, func() { 
configObj.Save(tempDir) 
restoredConfig := config.FromFile(configObj.Filename) 
Expect(*restoredConfig).To(Equal(*configObj)) 
}) 
AfterEach(func() { 
os.RemoveAll(tempDir) 
}) 
}) // end Describe()
Ginkgo and Dependencies 
● Ginkgo heavily relies on closures and 
closure variables to manage dependencies. 
o Resist the temptation to share state among tests! 
● Nested dependencies 
o Within a Describe block, you can nest: 
 Context block 
 More It’s and Before/AfterEach’s
Nested Dependencies 
var _ = Describe(“My component”) 
var configObj config.MyConfig 
BeforeEach(func() { 
configObj = config.NewConfig() 
}) 
It(“Does test1”, func() { 
// configObj is setup 
})
Nested Dependencies 
Context(“With a fake HTTP server configured”, func() { 
var testServer httptest.Server 
BeforeEach(func() { 
testServer = httptest.NewServer(<fake handler>) 
}) 
It(“Does test2”, func() { 
configObj.ServerUrl = testServer.URL 
configObj.DoSomething() ... 
}) 
})
Testing Notation + Ginkgo 
● Obviously, Ginkgo was not designed with my 
exact notation in mind… 
● However, it maps to it without too much 
difficulty.
Testing Notation + Ginkgo 
var _ = Describe(“my component”, func() { 
// GIVEN 
BeforeEach(func () { 
}) 
It(“Does something useful”, func() { 
// WHEN 
CallSomething() 
// THEN 
Expect(stuff).To((Equal(something)) 
}) 
})
Alternate Notation Style 
var _ = Describe(“my component”, func() { 
BeforeEach(func() {...}) // 
GIVEN 
It(“Does something useful”, func() { // WHEN 
closureVar = CallSomething() 
}) 
AfterEach(func() { 
Expect(closureVar).To(Equal(something)) // THEN 
}) 
})
Ginkgo Bootstrap 
● Ginkgo integrates with go test 
● You must use the ginkgo command to 
generate boilerplate code: 
$ cd mypackage/ # within $GOPATH/src 
$ ginkgo bootstrap 
… 
$ ls 
mypackage_suite_test.go
Ginkgo Bootstrap 
package mypackage_test // NOT THE SAME AS mypackage!!! 
import ( 
. "github.com/onsi/ginkgo" 
. "github.com/onsi/gomega" 
"testing" 
) 
func TestConfig(t *testing.T) { // go test integration boilerplate! 
RegisterFailHandler(Fail) 
RunSpecs(t, "Mypackage Suite") 
}
Ginkgo Generate 
● The generated suite calls the generated test 
files: 
$ ginkgo generate 
… 
$ ls 
mypackage_suite_test.go mypackage_test.go 
… 
$ cat mypackage_test.go 
...
Ginkgo Generate 
package mypackage_test // NOT THE SAME AS mypackage!! 
import ( 
. “mypackage” 
. “github.com/onsi/ginkgo” 
. “github.com/onsi/gomega” 
) 
var _ = Describe(“Mypackage test”, func() { 
})
Assessing Ginkgo/Gomega 
Test Notation 
● The BDD-style english annotations help 
readability a little bit... 
● Closures are a slippery slope! 
o Too much code: declare closure vars, must also 
initialize in BeforeEach to ensure test 
idempotency!
Assessing Ginkgo/Gomega 
● Complex dependencies are doable 
o Accomplished by nesting Describe/Context/ 
BeforeEach’s 
o However, your tests must be inserted in ever-more 
nested closures 
 Can get kinda complicated! 
● Gomega assertions 
o Flow nicely when read aloud 
o Too complex as a formal notation
Assessing Ginkgo/Gomega 
● Overall test notation grade: C+/B-o 
Depends on my mood and how complicated my test 
needs to get. 
o Worst part about it is that your test file easily grows 
way too long, and thus harder to comprehend as a 
spec. 
● Good enough to get our job done with high 
quality at Tasqr.
Go TDD Roundup 
Questions?

More Related Content

What's hot

POUG Meetup 1st MArch 2019 - utPLSQL v3 - Testing Framework for PL/SQL
POUG Meetup 1st MArch 2019 - utPLSQL v3 - Testing Framework for PL/SQLPOUG Meetup 1st MArch 2019 - utPLSQL v3 - Testing Framework for PL/SQL
POUG Meetup 1st MArch 2019 - utPLSQL v3 - Testing Framework for PL/SQLJacek Gebal
 
Write unit test from scratch
Write unit test from scratchWrite unit test from scratch
Write unit test from scratchWen-Shih Chao
 
Ukoug webinar - testing PLSQL APIs with utPLSQL v3
Ukoug webinar - testing PLSQL APIs with utPLSQL v3Ukoug webinar - testing PLSQL APIs with utPLSQL v3
Ukoug webinar - testing PLSQL APIs with utPLSQL v3Jacek Gebal
 
Presentation_C++UnitTest
Presentation_C++UnitTestPresentation_C++UnitTest
Presentation_C++UnitTestRaihan Masud
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy CodeAndrea Polci
 
Debugging tricks you wish you knew - Tamir Dresher
Debugging tricks you wish you knew  - Tamir DresherDebugging tricks you wish you knew  - Tamir Dresher
Debugging tricks you wish you knew - Tamir DresherTamir Dresher
 
Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008Andrea Francia
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestSeb Rose
 
Integration Group - Robot Framework
Integration Group - Robot Framework Integration Group - Robot Framework
Integration Group - Robot Framework OpenDaylight
 
Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)vilniusjug
 
Cpp Testing Techniques Tips and Tricks - Cpp Europe
Cpp Testing Techniques Tips and Tricks - Cpp EuropeCpp Testing Techniques Tips and Tricks - Cpp Europe
Cpp Testing Techniques Tips and Tricks - Cpp EuropeClare Macrae
 
Creating a reasonable project boilerplate
Creating a reasonable project boilerplateCreating a reasonable project boilerplate
Creating a reasonable project boilerplateStanislav Petrov
 
ES3-2020-P2 Bowling Game Kata
ES3-2020-P2 Bowling Game KataES3-2020-P2 Bowling Game Kata
ES3-2020-P2 Bowling Game KataDavid Rodenas
 
20111018 boost and gtest
20111018 boost and gtest20111018 boost and gtest
20111018 boost and gtestWill Shen
 
Robolectric android taipei
Robolectric   android taipeiRobolectric   android taipei
Robolectric android taipeiRichard Chang
 
The Evil Tester's Guide to HTTP proxies Tutorial
The Evil Tester's Guide to HTTP proxies TutorialThe Evil Tester's Guide to HTTP proxies Tutorial
The Evil Tester's Guide to HTTP proxies TutorialAlan Richardson
 

What's hot (20)

POUG Meetup 1st MArch 2019 - utPLSQL v3 - Testing Framework for PL/SQL
POUG Meetup 1st MArch 2019 - utPLSQL v3 - Testing Framework for PL/SQLPOUG Meetup 1st MArch 2019 - utPLSQL v3 - Testing Framework for PL/SQL
POUG Meetup 1st MArch 2019 - utPLSQL v3 - Testing Framework for PL/SQL
 
Write unit test from scratch
Write unit test from scratchWrite unit test from scratch
Write unit test from scratch
 
Ukoug webinar - testing PLSQL APIs with utPLSQL v3
Ukoug webinar - testing PLSQL APIs with utPLSQL v3Ukoug webinar - testing PLSQL APIs with utPLSQL v3
Ukoug webinar - testing PLSQL APIs with utPLSQL v3
 
Unit testing (eng)
Unit testing (eng)Unit testing (eng)
Unit testing (eng)
 
Presentation_C++UnitTest
Presentation_C++UnitTestPresentation_C++UnitTest
Presentation_C++UnitTest
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy Code
 
Debugging tricks you wish you knew - Tamir Dresher
Debugging tricks you wish you knew  - Tamir DresherDebugging tricks you wish you knew  - Tamir Dresher
Debugging tricks you wish you knew - Tamir Dresher
 
Tdd presentation
Tdd presentationTdd presentation
Tdd presentation
 
Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008Google C++ Testing Framework in Visual Studio 2008
Google C++ Testing Framework in Visual Studio 2008
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
 
Cursus phpunit
Cursus phpunitCursus phpunit
Cursus phpunit
 
Integration Group - Robot Framework
Integration Group - Robot Framework Integration Group - Robot Framework
Integration Group - Robot Framework
 
Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)
 
Cpp Testing Techniques Tips and Tricks - Cpp Europe
Cpp Testing Techniques Tips and Tricks - Cpp EuropeCpp Testing Techniques Tips and Tricks - Cpp Europe
Cpp Testing Techniques Tips and Tricks - Cpp Europe
 
Creating a reasonable project boilerplate
Creating a reasonable project boilerplateCreating a reasonable project boilerplate
Creating a reasonable project boilerplate
 
ES3-2020-P2 Bowling Game Kata
ES3-2020-P2 Bowling Game KataES3-2020-P2 Bowling Game Kata
ES3-2020-P2 Bowling Game Kata
 
20111018 boost and gtest
20111018 boost and gtest20111018 boost and gtest
20111018 boost and gtest
 
Php unit (eng)
Php unit (eng)Php unit (eng)
Php unit (eng)
 
Robolectric android taipei
Robolectric   android taipeiRobolectric   android taipei
Robolectric android taipei
 
The Evil Tester's Guide to HTTP proxies Tutorial
The Evil Tester's Guide to HTTP proxies TutorialThe Evil Tester's Guide to HTTP proxies Tutorial
The Evil Tester's Guide to HTTP proxies Tutorial
 

Similar to TDD in Go with Ginkgo and Gomega

Test Driven Development en Go con Ginkgo y Gomega
Test Driven Development en Go con Ginkgo y GomegaTest Driven Development en Go con Ginkgo y Gomega
Test Driven Development en Go con Ginkgo y GomegaSoftware Guru
 
Behavior Driven Education: A Story of Learning ROR
Behavior Driven Education: A Story of Learning RORBehavior Driven Education: A Story of Learning ROR
Behavior Driven Education: A Story of Learning RORSmartLogic
 
Test Driven Development in CQ5/AEM
Test Driven Development in CQ5/AEMTest Driven Development in CQ5/AEM
Test Driven Development in CQ5/AEMSagar Sane
 
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017Ortus Solutions, Corp
 
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016Gavin Pickin
 
Test Driven Development in AEM/CQ5
Test Driven Development in AEM/CQ5Test Driven Development in AEM/CQ5
Test Driven Development in AEM/CQ5rtpaem
 
Behaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About TestingBehaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About Testingdn
 
Bdd and-testing
Bdd and-testingBdd and-testing
Bdd and-testingmalcolmt
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017Xavi Hidalgo
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkPeter Kofler
 
Hadoop: Big Data Stacks validation w/ iTest How to tame the elephant?
Hadoop:  Big Data Stacks validation w/ iTest  How to tame the elephant?Hadoop:  Big Data Stacks validation w/ iTest  How to tame the elephant?
Hadoop: Big Data Stacks validation w/ iTest How to tame the elephant?Dmitri Shiryaev
 
Deliberate Practice, New Learning Styles (2015)
Deliberate Practice, New Learning Styles (2015)Deliberate Practice, New Learning Styles (2015)
Deliberate Practice, New Learning Styles (2015)Peter Kofler
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScriptJorg Janke
 
Getting started with karate dsl
Getting started with karate dslGetting started with karate dsl
Getting started with karate dslKnoldus Inc.
 
Test driven development - Zombie proof your code
Test driven development - Zombie proof your codeTest driven development - Zombie proof your code
Test driven development - Zombie proof your codePascal Larocque
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019Paulo Clavijo
 
Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)Lars Thorup
 

Similar to TDD in Go with Ginkgo and Gomega (20)

Test Driven Development en Go con Ginkgo y Gomega
Test Driven Development en Go con Ginkgo y GomegaTest Driven Development en Go con Ginkgo y Gomega
Test Driven Development en Go con Ginkgo y Gomega
 
Spock pres
Spock presSpock pres
Spock pres
 
Behavior Driven Education: A Story of Learning ROR
Behavior Driven Education: A Story of Learning RORBehavior Driven Education: A Story of Learning ROR
Behavior Driven Education: A Story of Learning ROR
 
Test Driven Development in CQ5/AEM
Test Driven Development in CQ5/AEMTest Driven Development in CQ5/AEM
Test Driven Development in CQ5/AEM
 
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
 
How to write Testable Javascript
How to write Testable JavascriptHow to write Testable Javascript
How to write Testable Javascript
 
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
 
Test Driven Development in AEM/CQ5
Test Driven Development in AEM/CQ5Test Driven Development in AEM/CQ5
Test Driven Development in AEM/CQ5
 
Behaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About TestingBehaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About Testing
 
Bdd and-testing
Bdd and-testingBdd and-testing
Bdd and-testing
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test Framework
 
Hadoop: Big Data Stacks validation w/ iTest How to tame the elephant?
Hadoop:  Big Data Stacks validation w/ iTest  How to tame the elephant?Hadoop:  Big Data Stacks validation w/ iTest  How to tame the elephant?
Hadoop: Big Data Stacks validation w/ iTest How to tame the elephant?
 
UPC Plone Testing Talk
UPC Plone Testing TalkUPC Plone Testing Talk
UPC Plone Testing Talk
 
Deliberate Practice, New Learning Styles (2015)
Deliberate Practice, New Learning Styles (2015)Deliberate Practice, New Learning Styles (2015)
Deliberate Practice, New Learning Styles (2015)
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScript
 
Getting started with karate dsl
Getting started with karate dslGetting started with karate dsl
Getting started with karate dsl
 
Test driven development - Zombie proof your code
Test driven development - Zombie proof your codeTest driven development - Zombie proof your code
Test driven development - Zombie proof your code
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019
 
Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)
 

Recently uploaded

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile EnvironmentVictorSzoltysek
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 

Recently uploaded (20)

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 

TDD in Go with Ginkgo and Gomega

  • 1. TDD in Go with Ginkgo and Gomega Eddy Reyes - eddy@containable.io http://www.tasqr.io
  • 2. About Me Eddy Reyes. Cofounder of Tasqr (www.tasqr.io) ● Automates application deployment / configuration without code or config files. Previously hacked lots of code at: ● IBM, Lifesize, Click Security
  • 3. Overview of Presentation ● Explain what TDD is o Not necessarily proselytizing that you must do it. o I dislike many TDD-related presentations, they don’t match my experience, they don’t stress what’s important IMO. ● What Tools Does Go Have to Help You Do TDD? o Ginkgo and Gomega, many others
  • 4. Further TDD Reading Uncle Bob Martin ● http://blog.8thlight.com/uncle-bob/archive.html Kent Beck ● Extreme Programming Explained (book) Is TDD Dead? (video debates) ● Kent Beck, Martin Fowler, DHH ● https://www.youtube.com/watch?v=z9quxZsLcfo (part 1)
  • 5. What Isn’t TDD? ● NOT just having written tests ● NOT a way to feel like you’re going fast ● NOT the Silver Bullet that saves software engineering ● NOT like Axe, which makes you irresistible to the opposite sex :-)
  • 6. What Is TDD? ● A process you follow when writing software. ● The process goes like this: o You write a test that defines an expected outcome in your program. o You watch the test fail. o You change the program until your test passes. ● Adding tests to an already substantial untested program isn’t necessarily TDD.
  • 7. How Do You Do TDD? ● In order to effectively follow the TDD pattern, you must first understand your design. o Tests are rigid. Learning is free-form. o You learn by hacking and breaking things. o Tests ensure things don’t break. ● TDD helps you SHIP, not LEARN
  • 8. Learn vs. Ship ● Learning is free-form exploration. Experiment, play, break things. o DO NOT SHIP THIS CODE! o After you learn your design, step away from the keyboard, and start anew. ● Shipping o Code ends up in a product used by customers. o Quality is important!
  • 9. Tests Are Design! ● If tests are comprehensive, they are a formal specification of your program. ● Writing a test is expressing the design of your program’s functionality. ● To truly serve as a spec, tests must be: o Clean, readable o Follow some consistent notation
  • 10. Test/Spec Notation GIVEN ● Precondition 1...N WHEN ● Call into your program THEN ● Assert Condition 1...N
  • 11. A Test Framework Needs... ● Clean, simple notation ● Clean setup/teardown of dependencies o Idempotent, independent tests! ● Clean separation from tested code ● Simple runner mechanism ● Good reporting o test results o coverage maps
  • 12. Testing in Go ● Test runner o go test ● Separates test code from product o *_test.go ● Comes with a test writing library o “testing” package ● Comes with coverage reporting tools o Beautiful HTML output
  • 13. “testing” Package ● Allows your to write test functions. ● Nice benchmarking utility ● Tests are basically blocks of code :( func TestXXX(t *testing.T) { // setup stuff // call your code if something.State != WhatIWant { t.Fail() } }
  • 14. Better Test-Writing Library ● go command has great tooling. ● Need a better notation for specifying tests. … Ginkgo and Gomega is a step forward! … but not perfect!
  • 15. Ginkgo and Gomega ● Ginkgo o Compatible with “go test” but also comes with its own test runner o BDD-style test notation o Expressive structuring of tests ● Gomega o “Matching” library o Allows for expressive assert-like statements
  • 16. Ginkgo ● Tests expressed as English statements o In the BDD tradition of Cucumber, et. al. var _ = Describe(“My component”, func() { It(“Does something special”, func() { MyFunction() Expect(MyVar).To(Equal(10)) }) })
  • 17. Gomega ● Assertion language Expect(number).To(Equal(10)) Expect(value).To(BeNumerically(“<”, 20)) Expect(someMap).To(Equal(otherMap)) // does reflect.DeepEqual() Expect(someMap).To(BeEquivalent(otherMap)) // allows different types Expect(myPtr).ToNot(BeNil()) Expect(flag).To(BeFalse()) Expect(err).ToNot(HaveOccurred()) Expect(err).To(MatchError(“my error message”))
  • 18. Ginkgo and Dependencies ● setup/teardown: var _ = Describe(“my config object”, func() { var ( configObj *config.MyConfig tempDir string ) BeforeEach(func() { var err error tempDir, err = ioutil.TempDir(“”, “test-dir”) Expect(err).ToNot(HaveOccurred()) configObj = config.NewConfig() })
  • 19. Ginkgo and Dependencies It(“Saves itself to file”, func() { configObj.Save(tempDir) restoredConfig := config.FromFile(configObj.Filename) Expect(*restoredConfig).To(Equal(*configObj)) }) AfterEach(func() { os.RemoveAll(tempDir) }) }) // end Describe()
  • 20. Ginkgo and Dependencies ● Ginkgo heavily relies on closures and closure variables to manage dependencies. o Resist the temptation to share state among tests! ● Nested dependencies o Within a Describe block, you can nest:  Context block  More It’s and Before/AfterEach’s
  • 21. Nested Dependencies var _ = Describe(“My component”) var configObj config.MyConfig BeforeEach(func() { configObj = config.NewConfig() }) It(“Does test1”, func() { // configObj is setup })
  • 22. Nested Dependencies Context(“With a fake HTTP server configured”, func() { var testServer httptest.Server BeforeEach(func() { testServer = httptest.NewServer(<fake handler>) }) It(“Does test2”, func() { configObj.ServerUrl = testServer.URL configObj.DoSomething() ... }) })
  • 23. Testing Notation + Ginkgo ● Obviously, Ginkgo was not designed with my exact notation in mind… ● However, it maps to it without too much difficulty.
  • 24. Testing Notation + Ginkgo var _ = Describe(“my component”, func() { // GIVEN BeforeEach(func () { }) It(“Does something useful”, func() { // WHEN CallSomething() // THEN Expect(stuff).To((Equal(something)) }) })
  • 25. Alternate Notation Style var _ = Describe(“my component”, func() { BeforeEach(func() {...}) // GIVEN It(“Does something useful”, func() { // WHEN closureVar = CallSomething() }) AfterEach(func() { Expect(closureVar).To(Equal(something)) // THEN }) })
  • 26. Ginkgo Bootstrap ● Ginkgo integrates with go test ● You must use the ginkgo command to generate boilerplate code: $ cd mypackage/ # within $GOPATH/src $ ginkgo bootstrap … $ ls mypackage_suite_test.go
  • 27. Ginkgo Bootstrap package mypackage_test // NOT THE SAME AS mypackage!!! import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "testing" ) func TestConfig(t *testing.T) { // go test integration boilerplate! RegisterFailHandler(Fail) RunSpecs(t, "Mypackage Suite") }
  • 28. Ginkgo Generate ● The generated suite calls the generated test files: $ ginkgo generate … $ ls mypackage_suite_test.go mypackage_test.go … $ cat mypackage_test.go ...
  • 29. Ginkgo Generate package mypackage_test // NOT THE SAME AS mypackage!! import ( . “mypackage” . “github.com/onsi/ginkgo” . “github.com/onsi/gomega” ) var _ = Describe(“Mypackage test”, func() { })
  • 30. Assessing Ginkgo/Gomega Test Notation ● The BDD-style english annotations help readability a little bit... ● Closures are a slippery slope! o Too much code: declare closure vars, must also initialize in BeforeEach to ensure test idempotency!
  • 31. Assessing Ginkgo/Gomega ● Complex dependencies are doable o Accomplished by nesting Describe/Context/ BeforeEach’s o However, your tests must be inserted in ever-more nested closures  Can get kinda complicated! ● Gomega assertions o Flow nicely when read aloud o Too complex as a formal notation
  • 32. Assessing Ginkgo/Gomega ● Overall test notation grade: C+/B-o Depends on my mood and how complicated my test needs to get. o Worst part about it is that your test file easily grows way too long, and thus harder to comprehend as a spec. ● Good enough to get our job done with high quality at Tasqr.
  • 33. Go TDD Roundup Questions?