SlideShare a Scribd company logo
1 of 29
Download to read offline
TDD / BDD kickstart
Lars Thorup
ZeaLake Software Consulting
March, 2016
Lars Thorup
● Software developer/architect
● JavaScript, C#
● Test Driven Development
● Coach
● Agile engineering practices
and tools
● Founder
● BestBrains
● ZeaLake
● @larsthorup
Basic unit testing
● Assertions
● Fixtures
● Runners
● Debugging
● Exercise
Assertions - Equality
Assert.AreEqual("San Francisco, California", location.ToDisplayString());
expect(location.toDisplayString()).toEqual('San Francisco, California');
assertThat(location.toDisplayString(), equalTo("San Francisco, California"));
Assertions - Sameness
● Microsoft
● Jasmine
● JUnit
Assert.AreSame(expectedLocation, actualLocation);
expect(actualLocation).toBe(expectedLocation);
assertThat(actualLocation, sameInstance(expectedLocation));
Assertions - special values
● Microsoft
● Jasmine
● JUnit
Assert.IsNull(location);
Assert.IsTrue(location.IsValid());
expect(location).toBeNull();
expect(location.isValid()).toBeTruthy();
assertThat(location, nullValue());
Assertions - floating point
● Microsoft
● Jasmine
● JUnit
Assert.AreEqual(10.23, location.Diameter(), 0.01);
expect(location.diameter()).toBeCloseTo(10.23, 2);
assertThat(location.diameter(), closeTo(10.23, 0.01));
Assertions - collections
● Microsoft
● Jasmine
● JUnit
CollectionAssert.AreEquivalent(new [] {"a", "b"}, names);
expect(names).toEqual(['a', 'b']);
assertThat(names, containsInAnyOrder("a", "b"))
Assertions - exceptions
● Microsoft
● Jasmine
● JUnit
[ExpectedException(typeof(ArgumentException))]
public void SomeTest () { DivideBy(0); }
expect(function () { divideBy(0); }).toThrow(ArgumentError);
@Test(expected = ArgumentException.class)
public void someTest() { divideBy(0); }
Assertions - exceptions
● Microsoft
try
{
DivideBy(0);
Assert.Fail();
}
catch(ArgumentException ex)
{
Assert.AreEqual("Cannot divide by 0", ex.Message);
}
Fixtures
● Microsoft
● Jasmine
● JUnit
[TestInitialize] public void Initialize() { ... }
[TestCleanup] public void Cleanup() { ... }
beforeEach(function () { ... });
afterEach(function () { ... });
@Before public void before() { ... }
@After public void after() { ... }
Runners
● Microsoft
● Jasmine
● JUnit
Visual Studio
Test | Run | All Tests
Grunt & Karma
npm test
IntelliJ IDEA
Run | Configurations | JUnit
Debugging
● Microsoft
● Jasmine
● JUnit
Visual Studio
Test | Debug | All Tests
Grunt & Karma & Chrome
npm run test:chrome
IntelliJ IDEA
Debug | Configurations | JUnit
Exercise - unit testing
● Try out assertions
● Equality and sameness
● Floating point
● Collections
● Exceptions
● Verify that fixture methods run for every test
● Debug a test
Workflow of TDD / BDD
Failing
test
Succeeding
test
Good
design Refactor
Code
Test
Behavior
Think, talk
TDD Demo - IP# -> Location display
● Convert this JSON ● To a display string
San Francisco, California
Farum, Denmark
Philippines
{
CountryCode = "US",
Country = "USA",
Region = "California",
City = "San Francisco"
}
{
CountryCode = "DK",
Country = "Denmark",
Region = "Hovedstaden",
City = "Farum"
}
{
CountryCode = "PH",
Country = "Philippines",
Region = "",
City = ""
}
Why TDD?
Well-designed
Just Works™
Refactorable
No debugger
Documented
Productive!
Exercise - TDD
● Input: a date
● Output: the time since that date, human readable
● "2 minutes ago"
● "1week ago"
● "3 months ago"
● Think first about the precise behavior
● Then write one test for the simplest case not covered yet
● and make it fail for the right reason
● Then write the code to make the test pass
● ...repeat :)
● Enjoy more efficient and predictable course of development
● Find and fix bugs faster
● Prevent bugs from reappearing
● Improve the design of our software
● Reliable documentation
● Way more fun :)
Why is TDD a good thing?
Questions!
TDD - questions
● How fine grained is the TDD cycle?
● Do we always write tests before code?
● Why can't we just write the tests afterwards?
● How many tests are enough?
● How do we test private methods?
● Do we have to setup all dependencies?
● What about our legacy code?
What is good design?
● One element of good design is loose coupling
● Use interfaces (for static languages)
● Inject dependencies
● Avoid using new:
● Inject dependencies instead:
private IEmailSvc emailSvc;
public Notifier(IEmailSvc emailSvc)
{
this.emailSvc = emailSvc;
}
public void Trigger()
{
emailSvc.SendEmail();
public void Trigger()
{
var emailSvc = new EmailSvc();
emailSvc.SendEmail();
}
Stubs and mocks
● When testing an object X, that depends on an object Y
● replace the real Y with a fake Y
● Benefits
● Only test one thing (X) at a time
● Faster tests (Y may be slow)
● Simpler (Y may depend on Z etc)
● Examples:
● Time
● Database
● Email
● HttpContext
Notifier
EmailSvc
IEmailSvc
EmailSvcStub
NotifierTest
Stubs
● Hand crafted
● More effort to write
● Easier to maintain
● Can be more "black box" than mocks
Mocks
● Mocks are automatically generated stubs
● Easy to use
● More "magical", may be slower
● More effort to maintain
● Will be more "white-box" than stubs
● Example frameworks:
● C#: NSubstitute
● JavaScript: Sinon.JS
● Java: Easymock / Powermock
Stubs - example
public class EmailSvcStub : IEmailSvc
{
public int NumberOfEmailsSent { get; set; }
public void SendEmail()
{
++NumberOfEmailsSent;
}
}
[Test]
public void Trigger()
{
// setup
var emailSvc = new EmailSvcStub();
var notifier = new Notifier(emailSvc);
// invoke
notifier.Trigger();
// verify
Assert.That(emailSvc.NumberOfEmailsSent, Is.EqualTo(1));
}
Mocks - example
[Test]
public void Trigger()
{
// setup
var emailSvc = Substitute.For<IEmailSvc>();
var notifier = new Notifier(emailSvc);
// invoke
notifier.Trigger();
// verify
emailSvc.Received(1).SendEmail();
}
Test data builder - example
[Test]
public void GetResponseMedia()
{
// given
var stub = new StubBuilder
{
Questions = new [] {
new QuestionBuilder { Name = "MEDIA" },
},
Participants = new[] {
new ParticipantBuilder { Name = "Lars", Votes = new [] {
new VoteBuilder { Question = "MEDIA", Responses =
new ResponseBuilder(new byte [] {1, 2, 3}) },
}},
},
}.Build();
var voteController = new VoteController(stub.Session);
// when
var result = voteController.GetResponseMedia(vote.Id, true) as MediaResult;
// then
Assert.That(result.Download, Is.True);
Assert.That(result.MediaLength, Is.EqualTo(3));
Assert.That(TfResponse.ReadAllBytes(result.MediaStream), Is.EqualTo(new byte[] {1, 2, 3}));
}
Legacy code
● Add pinning tests
● special kinds of unit tests for legacy
code
● verifies existing behaviour
● acts as a safety net
● Can be driven by change requests
● Refactor the code to be able to write
unit tests
● Add unit test for the change request
● Track coverage trend for existing
code
● and make sure it grows

More Related Content

Similar to Tddbdd workshop

Testing and validating spark programs - Strata SJ 2016
Testing and validating spark programs - Strata SJ 2016Testing and validating spark programs - Strata SJ 2016
Testing and validating spark programs - Strata SJ 2016Holden Karau
 
Javascript unit testing with QUnit and Sinon
Javascript unit testing with QUnit and SinonJavascript unit testing with QUnit and Sinon
Javascript unit testing with QUnit and SinonLars Thorup
 
Developer Test - Things to Know
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Knowvilniusjug
 
Advanced Javascript Unit Testing
Advanced Javascript Unit TestingAdvanced Javascript Unit Testing
Advanced Javascript Unit TestingLars Thorup
 
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
 
Effective testing for spark programs scala bay preview (pre-strata ny 2015)
Effective testing for spark programs scala bay preview (pre-strata ny 2015)Effective testing for spark programs scala bay preview (pre-strata ny 2015)
Effective testing for spark programs scala bay preview (pre-strata ny 2015)Holden Karau
 
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 Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In ActionJon Kruger
 
Polyglot persistence with Spring Data
Polyglot persistence with Spring DataPolyglot persistence with Spring Data
Polyglot persistence with Spring DataCorneil du Plessis
 
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
 
TDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and GomegaTDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and GomegaEddy Reyes
 
TDD super mondays-june-2014
TDD super mondays-june-2014TDD super mondays-june-2014
TDD super mondays-june-2014Alex Kavanagh
 
Beyond unit tests: Deployment and testing for Hadoop/Spark workflows
Beyond unit tests: Deployment and testing for Hadoop/Spark workflowsBeyond unit tests: Deployment and testing for Hadoop/Spark workflows
Beyond unit tests: Deployment and testing for Hadoop/Spark workflowsDataWorks Summit
 
Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
 Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark... Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...Databricks
 
2014 11 20 Drupal 7 -> 8 test migratie
2014 11 20 Drupal 7 -> 8 test migratie2014 11 20 Drupal 7 -> 8 test migratie
2014 11 20 Drupal 7 -> 8 test migratiehcderaad
 
Testing and validating distributed systems with Apache Spark and Apache Beam ...
Testing and validating distributed systems with Apache Spark and Apache Beam ...Testing and validating distributed systems with Apache Spark and Apache Beam ...
Testing and validating distributed systems with Apache Spark and Apache Beam ...Holden Karau
 
Test-Driven Development.pptx
Test-Driven Development.pptxTest-Driven Development.pptx
Test-Driven Development.pptxTomas561914
 
Java script unit testing
Java script unit testingJava script unit testing
Java script unit testingMats Bryntse
 

Similar to Tddbdd workshop (20)

Testing and validating spark programs - Strata SJ 2016
Testing and validating spark programs - Strata SJ 2016Testing and validating spark programs - Strata SJ 2016
Testing and validating spark programs - Strata SJ 2016
 
Javascript unit testing with QUnit and Sinon
Javascript unit testing with QUnit and SinonJavascript unit testing with QUnit and Sinon
Javascript unit testing with QUnit and Sinon
 
Developer Test - Things to Know
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Know
 
Advanced Javascript Unit Testing
Advanced Javascript Unit TestingAdvanced Javascript Unit Testing
Advanced Javascript Unit Testing
 
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)
 
Effective testing for spark programs scala bay preview (pre-strata ny 2015)
Effective testing for spark programs scala bay preview (pre-strata ny 2015)Effective testing for spark programs scala bay preview (pre-strata ny 2015)
Effective testing for spark programs scala bay preview (pre-strata ny 2015)
 
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 Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
Polyglot persistence with Spring Data
Polyglot persistence with Spring DataPolyglot persistence with Spring Data
Polyglot persistence with Spring Data
 
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
 
TDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and GomegaTDD in Go with Ginkgo and Gomega
TDD in Go with Ginkgo and Gomega
 
TDD super mondays-june-2014
TDD super mondays-june-2014TDD super mondays-june-2014
TDD super mondays-june-2014
 
Object Oriented Javascript
Object Oriented JavascriptObject Oriented Javascript
Object Oriented Javascript
 
Beyond unit tests: Deployment and testing for Hadoop/Spark workflows
Beyond unit tests: Deployment and testing for Hadoop/Spark workflowsBeyond unit tests: Deployment and testing for Hadoop/Spark workflows
Beyond unit tests: Deployment and testing for Hadoop/Spark workflows
 
Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
 Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark... Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
 
2014 11 20 Drupal 7 -> 8 test migratie
2014 11 20 Drupal 7 -> 8 test migratie2014 11 20 Drupal 7 -> 8 test migratie
2014 11 20 Drupal 7 -> 8 test migratie
 
Testing and validating distributed systems with Apache Spark and Apache Beam ...
Testing and validating distributed systems with Apache Spark and Apache Beam ...Testing and validating distributed systems with Apache Spark and Apache Beam ...
Testing and validating distributed systems with Apache Spark and Apache Beam ...
 
Test-Driven Development.pptx
Test-Driven Development.pptxTest-Driven Development.pptx
Test-Driven Development.pptx
 
Java script unit testing
Java script unit testingJava script unit testing
Java script unit testing
 

More from BestBrains

Psykologien i agile teams
Psykologien i agile teamsPsykologien i agile teams
Psykologien i agile teamsBestBrains
 
Bliv en haj til nedbrydning okt 2016
Bliv en haj til nedbrydning okt 2016 Bliv en haj til nedbrydning okt 2016
Bliv en haj til nedbrydning okt 2016 BestBrains
 
Vsm best brains presentation_ september 2016_v4 2
Vsm best brains presentation_ september 2016_v4 2Vsm best brains presentation_ september 2016_v4 2
Vsm best brains presentation_ september 2016_v4 2BestBrains
 
Lars thorup-react-and-redux-2016-09
Lars thorup-react-and-redux-2016-09Lars thorup-react-and-redux-2016-09
Lars thorup-react-and-redux-2016-09BestBrains
 
BestBrains café-møde: Kanban med Lego ved Jesper Thaning
BestBrains café-møde: Kanban med Lego ved Jesper ThaningBestBrains café-møde: Kanban med Lego ved Jesper Thaning
BestBrains café-møde: Kanban med Lego ved Jesper ThaningBestBrains
 
Projektleder i agilt setup, cafemøde hos BestBrains, april 2016
Projektleder i agilt setup, cafemøde hos BestBrains, april 2016Projektleder i agilt setup, cafemøde hos BestBrains, april 2016
Projektleder i agilt setup, cafemøde hos BestBrains, april 2016BestBrains
 
BestBrains café-møde d. 14. april: Retrospektiv antipatterns
BestBrains café-møde d. 14. april: Retrospektiv antipatternsBestBrains café-møde d. 14. april: Retrospektiv antipatterns
BestBrains café-møde d. 14. april: Retrospektiv antipatternsBestBrains
 
Gør urværket synligt for dine teams
Gør urværket synligt for dine teamsGør urværket synligt for dine teams
Gør urværket synligt for dine teamsBestBrains
 
Craftsmanship 2016 -BestBrains Café-møder
Craftsmanship 2016 -BestBrains Café-møderCraftsmanship 2016 -BestBrains Café-møder
Craftsmanship 2016 -BestBrains Café-møderBestBrains
 
Best brains kanban med lego januar 2016 handout
Best brains kanban med lego januar 2016 handoutBest brains kanban med lego januar 2016 handout
Best brains kanban med lego januar 2016 handoutBestBrains
 
Bliv en ørn til estimering nov 2015
Bliv en ørn til estimering nov 2015Bliv en ørn til estimering nov 2015
Bliv en ørn til estimering nov 2015BestBrains
 
Den agile transformation november 2015
Den agile transformation november 2015Den agile transformation november 2015
Den agile transformation november 2015BestBrains
 
Sandheden om agile udviklingsteams
Sandheden om agile udviklingsteamsSandheden om agile udviklingsteams
Sandheden om agile udviklingsteamsBestBrains
 
Intro til agile 31 aug 2015
Intro til agile 31 aug 2015Intro til agile 31 aug 2015
Intro til agile 31 aug 2015BestBrains
 
Lær 3 agile metoder på en aften, august 2015
Lær 3 agile metoder på en aften, august 2015Lær 3 agile metoder på en aften, august 2015
Lær 3 agile metoder på en aften, august 2015BestBrains
 
Bliv en haj til nedbrydning, aug 2015.
Bliv en haj til nedbrydning, aug 2015.Bliv en haj til nedbrydning, aug 2015.
Bliv en haj til nedbrydning, aug 2015.BestBrains
 
Haj til nedbrydning juni 2015
Haj til nedbrydning juni 2015Haj til nedbrydning juni 2015
Haj til nedbrydning juni 2015BestBrains
 
Motivation - fedt, farligt & flygtigt.
Motivation - fedt, farligt & flygtigt.Motivation - fedt, farligt & flygtigt.
Motivation - fedt, farligt & flygtigt.BestBrains
 
Switch -den_agile_omstilling
Switch  -den_agile_omstillingSwitch  -den_agile_omstilling
Switch -den_agile_omstillingBestBrains
 
Retrospectives er spild af tid!
Retrospectives er spild af tid!Retrospectives er spild af tid!
Retrospectives er spild af tid!BestBrains
 

More from BestBrains (20)

Psykologien i agile teams
Psykologien i agile teamsPsykologien i agile teams
Psykologien i agile teams
 
Bliv en haj til nedbrydning okt 2016
Bliv en haj til nedbrydning okt 2016 Bliv en haj til nedbrydning okt 2016
Bliv en haj til nedbrydning okt 2016
 
Vsm best brains presentation_ september 2016_v4 2
Vsm best brains presentation_ september 2016_v4 2Vsm best brains presentation_ september 2016_v4 2
Vsm best brains presentation_ september 2016_v4 2
 
Lars thorup-react-and-redux-2016-09
Lars thorup-react-and-redux-2016-09Lars thorup-react-and-redux-2016-09
Lars thorup-react-and-redux-2016-09
 
BestBrains café-møde: Kanban med Lego ved Jesper Thaning
BestBrains café-møde: Kanban med Lego ved Jesper ThaningBestBrains café-møde: Kanban med Lego ved Jesper Thaning
BestBrains café-møde: Kanban med Lego ved Jesper Thaning
 
Projektleder i agilt setup, cafemøde hos BestBrains, april 2016
Projektleder i agilt setup, cafemøde hos BestBrains, april 2016Projektleder i agilt setup, cafemøde hos BestBrains, april 2016
Projektleder i agilt setup, cafemøde hos BestBrains, april 2016
 
BestBrains café-møde d. 14. april: Retrospektiv antipatterns
BestBrains café-møde d. 14. april: Retrospektiv antipatternsBestBrains café-møde d. 14. april: Retrospektiv antipatterns
BestBrains café-møde d. 14. april: Retrospektiv antipatterns
 
Gør urværket synligt for dine teams
Gør urværket synligt for dine teamsGør urværket synligt for dine teams
Gør urværket synligt for dine teams
 
Craftsmanship 2016 -BestBrains Café-møder
Craftsmanship 2016 -BestBrains Café-møderCraftsmanship 2016 -BestBrains Café-møder
Craftsmanship 2016 -BestBrains Café-møder
 
Best brains kanban med lego januar 2016 handout
Best brains kanban med lego januar 2016 handoutBest brains kanban med lego januar 2016 handout
Best brains kanban med lego januar 2016 handout
 
Bliv en ørn til estimering nov 2015
Bliv en ørn til estimering nov 2015Bliv en ørn til estimering nov 2015
Bliv en ørn til estimering nov 2015
 
Den agile transformation november 2015
Den agile transformation november 2015Den agile transformation november 2015
Den agile transformation november 2015
 
Sandheden om agile udviklingsteams
Sandheden om agile udviklingsteamsSandheden om agile udviklingsteams
Sandheden om agile udviklingsteams
 
Intro til agile 31 aug 2015
Intro til agile 31 aug 2015Intro til agile 31 aug 2015
Intro til agile 31 aug 2015
 
Lær 3 agile metoder på en aften, august 2015
Lær 3 agile metoder på en aften, august 2015Lær 3 agile metoder på en aften, august 2015
Lær 3 agile metoder på en aften, august 2015
 
Bliv en haj til nedbrydning, aug 2015.
Bliv en haj til nedbrydning, aug 2015.Bliv en haj til nedbrydning, aug 2015.
Bliv en haj til nedbrydning, aug 2015.
 
Haj til nedbrydning juni 2015
Haj til nedbrydning juni 2015Haj til nedbrydning juni 2015
Haj til nedbrydning juni 2015
 
Motivation - fedt, farligt & flygtigt.
Motivation - fedt, farligt & flygtigt.Motivation - fedt, farligt & flygtigt.
Motivation - fedt, farligt & flygtigt.
 
Switch -den_agile_omstilling
Switch  -den_agile_omstillingSwitch  -den_agile_omstilling
Switch -den_agile_omstilling
 
Retrospectives er spild af tid!
Retrospectives er spild af tid!Retrospectives er spild af tid!
Retrospectives er spild af tid!
 

Recently uploaded

Neha +91-9537192988-Friendly Ahmedabad Call Girls has Complete Authority for ...
Neha +91-9537192988-Friendly Ahmedabad Call Girls has Complete Authority for ...Neha +91-9537192988-Friendly Ahmedabad Call Girls has Complete Authority for ...
Neha +91-9537192988-Friendly Ahmedabad Call Girls has Complete Authority for ...Niya Khan
 
VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...Suhani Kapoor
 
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best sexual serviceanilsa9823
 
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证obuhobo
 
Call Girl in Low Price Delhi Punjabi Bagh 9711199012
Call Girl in Low Price Delhi Punjabi Bagh  9711199012Call Girl in Low Price Delhi Punjabi Bagh  9711199012
Call Girl in Low Price Delhi Punjabi Bagh 9711199012sapnasaifi408
 
内布拉斯加大学林肯分校毕业证录取书( 退学 )学位证书硕士
内布拉斯加大学林肯分校毕业证录取书( 退学 )学位证书硕士内布拉斯加大学林肯分校毕业证录取书( 退学 )学位证书硕士
内布拉斯加大学林肯分校毕业证录取书( 退学 )学位证书硕士obuhobo
 
Résumé (2 pager - 12 ft standard syntax)
Résumé (2 pager -  12 ft standard syntax)Résumé (2 pager -  12 ft standard syntax)
Résumé (2 pager - 12 ft standard syntax)Soham Mondal
 
CALL ON ➥8923113531 🔝Call Girls Husainganj Lucknow best Female service 🧳
CALL ON ➥8923113531 🔝Call Girls Husainganj Lucknow best Female service  🧳CALL ON ➥8923113531 🔝Call Girls Husainganj Lucknow best Female service  🧳
CALL ON ➥8923113531 🔝Call Girls Husainganj Lucknow best Female service 🧳anilsa9823
 
Final Completion Certificate of Marketing Management Internship
Final Completion Certificate of Marketing Management InternshipFinal Completion Certificate of Marketing Management Internship
Final Completion Certificate of Marketing Management InternshipSoham Mondal
 
Production Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbjProduction Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbjLewisJB
 
Preventing and ending sexual harassment in the workplace.pptx
Preventing and ending sexual harassment in the workplace.pptxPreventing and ending sexual harassment in the workplace.pptx
Preventing and ending sexual harassment in the workplace.pptxGry Tina Tinde
 
CALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual serviceanilsa9823
 
Delhi Call Girls South Ex 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Ex 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls South Ex 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Ex 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call GirlsDelhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girlsshivangimorya083
 
VIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service Bhilai
VIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service BhilaiVIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service Bhilai
VIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service BhilaiSuhani Kapoor
 
VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...Suhani Kapoor
 
CFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector ExperienceCFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector ExperienceSanjay Bokadia
 
VIP Call Girls Service Cuttack Aishwarya 8250192130 Independent Escort Servic...
VIP Call Girls Service Cuttack Aishwarya 8250192130 Independent Escort Servic...VIP Call Girls Service Cuttack Aishwarya 8250192130 Independent Escort Servic...
VIP Call Girls Service Cuttack Aishwarya 8250192130 Independent Escort Servic...Suhani Kapoor
 
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service BhiwandiVIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service BhiwandiSuhani Kapoor
 
Dubai Call Girls Naija O525547819 Call Girls In Dubai Home Made
Dubai Call Girls Naija O525547819 Call Girls In Dubai Home MadeDubai Call Girls Naija O525547819 Call Girls In Dubai Home Made
Dubai Call Girls Naija O525547819 Call Girls In Dubai Home Madekojalkojal131
 

Recently uploaded (20)

Neha +91-9537192988-Friendly Ahmedabad Call Girls has Complete Authority for ...
Neha +91-9537192988-Friendly Ahmedabad Call Girls has Complete Authority for ...Neha +91-9537192988-Friendly Ahmedabad Call Girls has Complete Authority for ...
Neha +91-9537192988-Friendly Ahmedabad Call Girls has Complete Authority for ...
 
VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Saharanpur Aishwarya 8250192130 Independent Escort Ser...
 
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Nishatganj Lucknow best sexual service
 
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
女王大学硕士毕业证成绩单(加急办理)认证海外毕业证
 
Call Girl in Low Price Delhi Punjabi Bagh 9711199012
Call Girl in Low Price Delhi Punjabi Bagh  9711199012Call Girl in Low Price Delhi Punjabi Bagh  9711199012
Call Girl in Low Price Delhi Punjabi Bagh 9711199012
 
内布拉斯加大学林肯分校毕业证录取书( 退学 )学位证书硕士
内布拉斯加大学林肯分校毕业证录取书( 退学 )学位证书硕士内布拉斯加大学林肯分校毕业证录取书( 退学 )学位证书硕士
内布拉斯加大学林肯分校毕业证录取书( 退学 )学位证书硕士
 
Résumé (2 pager - 12 ft standard syntax)
Résumé (2 pager -  12 ft standard syntax)Résumé (2 pager -  12 ft standard syntax)
Résumé (2 pager - 12 ft standard syntax)
 
CALL ON ➥8923113531 🔝Call Girls Husainganj Lucknow best Female service 🧳
CALL ON ➥8923113531 🔝Call Girls Husainganj Lucknow best Female service  🧳CALL ON ➥8923113531 🔝Call Girls Husainganj Lucknow best Female service  🧳
CALL ON ➥8923113531 🔝Call Girls Husainganj Lucknow best Female service 🧳
 
Final Completion Certificate of Marketing Management Internship
Final Completion Certificate of Marketing Management InternshipFinal Completion Certificate of Marketing Management Internship
Final Completion Certificate of Marketing Management Internship
 
Production Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbjProduction Day 1.pptxjvjbvbcbcb bj bvcbj
Production Day 1.pptxjvjbvbcbcb bj bvcbj
 
Preventing and ending sexual harassment in the workplace.pptx
Preventing and ending sexual harassment in the workplace.pptxPreventing and ending sexual harassment in the workplace.pptx
Preventing and ending sexual harassment in the workplace.pptx
 
CALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Gosainganj Lucknow best sexual service
 
Delhi Call Girls South Ex 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Ex 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls South Ex 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls South Ex 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call GirlsDelhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
Delhi Call Girls In Atta Market 9711199012 Book Your One night Stand Call Girls
 
VIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service Bhilai
VIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service BhilaiVIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service Bhilai
VIP Call Girl Bhilai Aashi 8250192130 Independent Escort Service Bhilai
 
VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...
VIP Call Girls Service Jamshedpur Aishwarya 8250192130 Independent Escort Ser...
 
CFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector ExperienceCFO_SB_Career History_Multi Sector Experience
CFO_SB_Career History_Multi Sector Experience
 
VIP Call Girls Service Cuttack Aishwarya 8250192130 Independent Escort Servic...
VIP Call Girls Service Cuttack Aishwarya 8250192130 Independent Escort Servic...VIP Call Girls Service Cuttack Aishwarya 8250192130 Independent Escort Servic...
VIP Call Girls Service Cuttack Aishwarya 8250192130 Independent Escort Servic...
 
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service BhiwandiVIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
VIP Call Girl Bhiwandi Aashi 8250192130 Independent Escort Service Bhiwandi
 
Dubai Call Girls Naija O525547819 Call Girls In Dubai Home Made
Dubai Call Girls Naija O525547819 Call Girls In Dubai Home MadeDubai Call Girls Naija O525547819 Call Girls In Dubai Home Made
Dubai Call Girls Naija O525547819 Call Girls In Dubai Home Made
 

Tddbdd workshop

  • 1. TDD / BDD kickstart Lars Thorup ZeaLake Software Consulting March, 2016
  • 2. Lars Thorup ● Software developer/architect ● JavaScript, C# ● Test Driven Development ● Coach ● Agile engineering practices and tools ● Founder ● BestBrains ● ZeaLake ● @larsthorup
  • 3. Basic unit testing ● Assertions ● Fixtures ● Runners ● Debugging ● Exercise
  • 4. Assertions - Equality Assert.AreEqual("San Francisco, California", location.ToDisplayString()); expect(location.toDisplayString()).toEqual('San Francisco, California'); assertThat(location.toDisplayString(), equalTo("San Francisco, California"));
  • 5. Assertions - Sameness ● Microsoft ● Jasmine ● JUnit Assert.AreSame(expectedLocation, actualLocation); expect(actualLocation).toBe(expectedLocation); assertThat(actualLocation, sameInstance(expectedLocation));
  • 6. Assertions - special values ● Microsoft ● Jasmine ● JUnit Assert.IsNull(location); Assert.IsTrue(location.IsValid()); expect(location).toBeNull(); expect(location.isValid()).toBeTruthy(); assertThat(location, nullValue());
  • 7. Assertions - floating point ● Microsoft ● Jasmine ● JUnit Assert.AreEqual(10.23, location.Diameter(), 0.01); expect(location.diameter()).toBeCloseTo(10.23, 2); assertThat(location.diameter(), closeTo(10.23, 0.01));
  • 8. Assertions - collections ● Microsoft ● Jasmine ● JUnit CollectionAssert.AreEquivalent(new [] {"a", "b"}, names); expect(names).toEqual(['a', 'b']); assertThat(names, containsInAnyOrder("a", "b"))
  • 9. Assertions - exceptions ● Microsoft ● Jasmine ● JUnit [ExpectedException(typeof(ArgumentException))] public void SomeTest () { DivideBy(0); } expect(function () { divideBy(0); }).toThrow(ArgumentError); @Test(expected = ArgumentException.class) public void someTest() { divideBy(0); }
  • 10. Assertions - exceptions ● Microsoft try { DivideBy(0); Assert.Fail(); } catch(ArgumentException ex) { Assert.AreEqual("Cannot divide by 0", ex.Message); }
  • 11. Fixtures ● Microsoft ● Jasmine ● JUnit [TestInitialize] public void Initialize() { ... } [TestCleanup] public void Cleanup() { ... } beforeEach(function () { ... }); afterEach(function () { ... }); @Before public void before() { ... } @After public void after() { ... }
  • 12. Runners ● Microsoft ● Jasmine ● JUnit Visual Studio Test | Run | All Tests Grunt & Karma npm test IntelliJ IDEA Run | Configurations | JUnit
  • 13. Debugging ● Microsoft ● Jasmine ● JUnit Visual Studio Test | Debug | All Tests Grunt & Karma & Chrome npm run test:chrome IntelliJ IDEA Debug | Configurations | JUnit
  • 14. Exercise - unit testing ● Try out assertions ● Equality and sameness ● Floating point ● Collections ● Exceptions ● Verify that fixture methods run for every test ● Debug a test
  • 15. Workflow of TDD / BDD Failing test Succeeding test Good design Refactor Code Test Behavior Think, talk
  • 16. TDD Demo - IP# -> Location display ● Convert this JSON ● To a display string San Francisco, California Farum, Denmark Philippines { CountryCode = "US", Country = "USA", Region = "California", City = "San Francisco" } { CountryCode = "DK", Country = "Denmark", Region = "Hovedstaden", City = "Farum" } { CountryCode = "PH", Country = "Philippines", Region = "", City = "" }
  • 17. Why TDD? Well-designed Just Works™ Refactorable No debugger Documented Productive!
  • 18. Exercise - TDD ● Input: a date ● Output: the time since that date, human readable ● "2 minutes ago" ● "1week ago" ● "3 months ago" ● Think first about the precise behavior ● Then write one test for the simplest case not covered yet ● and make it fail for the right reason ● Then write the code to make the test pass ● ...repeat :)
  • 19. ● Enjoy more efficient and predictable course of development ● Find and fix bugs faster ● Prevent bugs from reappearing ● Improve the design of our software ● Reliable documentation ● Way more fun :) Why is TDD a good thing?
  • 21. TDD - questions ● How fine grained is the TDD cycle? ● Do we always write tests before code? ● Why can't we just write the tests afterwards? ● How many tests are enough? ● How do we test private methods? ● Do we have to setup all dependencies? ● What about our legacy code?
  • 22. What is good design? ● One element of good design is loose coupling ● Use interfaces (for static languages) ● Inject dependencies ● Avoid using new: ● Inject dependencies instead: private IEmailSvc emailSvc; public Notifier(IEmailSvc emailSvc) { this.emailSvc = emailSvc; } public void Trigger() { emailSvc.SendEmail(); public void Trigger() { var emailSvc = new EmailSvc(); emailSvc.SendEmail(); }
  • 23. Stubs and mocks ● When testing an object X, that depends on an object Y ● replace the real Y with a fake Y ● Benefits ● Only test one thing (X) at a time ● Faster tests (Y may be slow) ● Simpler (Y may depend on Z etc) ● Examples: ● Time ● Database ● Email ● HttpContext Notifier EmailSvc IEmailSvc EmailSvcStub NotifierTest
  • 24. Stubs ● Hand crafted ● More effort to write ● Easier to maintain ● Can be more "black box" than mocks
  • 25. Mocks ● Mocks are automatically generated stubs ● Easy to use ● More "magical", may be slower ● More effort to maintain ● Will be more "white-box" than stubs ● Example frameworks: ● C#: NSubstitute ● JavaScript: Sinon.JS ● Java: Easymock / Powermock
  • 26. Stubs - example public class EmailSvcStub : IEmailSvc { public int NumberOfEmailsSent { get; set; } public void SendEmail() { ++NumberOfEmailsSent; } } [Test] public void Trigger() { // setup var emailSvc = new EmailSvcStub(); var notifier = new Notifier(emailSvc); // invoke notifier.Trigger(); // verify Assert.That(emailSvc.NumberOfEmailsSent, Is.EqualTo(1)); }
  • 27. Mocks - example [Test] public void Trigger() { // setup var emailSvc = Substitute.For<IEmailSvc>(); var notifier = new Notifier(emailSvc); // invoke notifier.Trigger(); // verify emailSvc.Received(1).SendEmail(); }
  • 28. Test data builder - example [Test] public void GetResponseMedia() { // given var stub = new StubBuilder { Questions = new [] { new QuestionBuilder { Name = "MEDIA" }, }, Participants = new[] { new ParticipantBuilder { Name = "Lars", Votes = new [] { new VoteBuilder { Question = "MEDIA", Responses = new ResponseBuilder(new byte [] {1, 2, 3}) }, }}, }, }.Build(); var voteController = new VoteController(stub.Session); // when var result = voteController.GetResponseMedia(vote.Id, true) as MediaResult; // then Assert.That(result.Download, Is.True); Assert.That(result.MediaLength, Is.EqualTo(3)); Assert.That(TfResponse.ReadAllBytes(result.MediaStream), Is.EqualTo(new byte[] {1, 2, 3})); }
  • 29. Legacy code ● Add pinning tests ● special kinds of unit tests for legacy code ● verifies existing behaviour ● acts as a safety net ● Can be driven by change requests ● Refactor the code to be able to write unit tests ● Add unit test for the change request ● Track coverage trend for existing code ● and make sure it grows