Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Test & BehaviourDriven DevelopmentLars ThorupZeaLake Software ConsultingMarch, 2012
Who is Lars Thorup?●   Software developer/architect    ●   C++, C# and JavaScript    ●   Test Driven Development●   Coach:...
Why are we here today?●   What is TDD/BDD?    ●   Express expected behaviour before writing code●   Why is TDD/BDD a good ...
Workflow of TDD/BDD    Think, talk                  Idea                          Test                                   F...
BDD or TDD?●   Behaviour first    ●   makes more sense than "Test first"●   Structure of test programs    ●   Given <preco...
Different kinds of automated tests●   Unit tests    ●   Test individual pieces of code and the interaction between code   ...
Unit tests or system tests?●   Unit tests are efficient    ●   Fast to run (hundreds per second)    ●   Robust and predict...
Can we automate performance tests?●   Performance tests are brittle    ●   Tip: create performance trend curves instead
How do we run the tests automatically?●   From our programming environment (IDE)    ●   Command line: make test    ●   Rig...
How can tests help improve our design?●   The software design will evolve over time●   A refactoring improves the design w...
Are we wasting developer time writing tests?●   No●   Time spent writing tests is not taken from time spent coding    ●   ...
How do we get started?●   When we have a lot of existing code without tests    ●   Create a set of system tests to get a s...
Maintainability●   Stick to a pattern for your tests    ●   E.g. Given-When-Then●   Focus on readability over code duplica...
What does a real-world project look like?●   wizerize.com    ●   Web application: C# and JavaScript    ●   3½ years of dev...
Where can I read more?●   http://googletesting.blogspot.com/●   http://testdrivendeveloper.com/●   http://codesheriff.blog...
But what about:●   Stubs & mocks●   Test data●   UI testing●   SQL testing●   JavaScript testing●   Web Service testing●  ...
What is good design?●   One element of good design is loose coupling    ●   Use interfaces (for static languages)    ●   I...
Stubs and mocks●   When testing an object X, that depends on an object Y    ●   replace the real Y with a fake Y●   Benefi...
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"●   More effort to maintain●   Will be mo...
Stubs - example                              public class EmailSvcStub : IEmailSvc                              {         ...
Mocks - example [Test] public void Trigger() {     // setup     var emailSvc = Substitute.For<IEmailSvc>();     var notifi...
Test data●   Each developer his/her own database●   Standard test data    ●   Created before running tests●   Test data bu...
Test data builder - example[Test]public void GetResponseMedia(){    // given    var stub = new StubBuilder    {        Que...
Web UI testing●   Control a browser from the tests using a seperate tool●   Tools    ●   Selenium    ●   WatiN    ●   Cucu...
SQL testing●   Test stored procedure, constraints, functions and triggers●   Use your backend testing framework (like NUni...
JavaScript testing●   Use a JavaScript unit testing framework    ●   QUnit    ●   jsTestDriver    ●   Jasmine
Web Service testing●    Use your backend testing framework (like NUnit)●    Use a JSON friendly version of WebClient:    /...
Legacy code●   Add pinning tests    ●   special kinds of unit tests for legacy        code    ●   verifies existing behavi...
Upcoming SlideShare
Loading in …5
×

Test and Behaviour Driven Development (TDD/BDD)

19,091 views

Published on

In this introduction to Test Driven Development (TDD) or Behaviour Driven Development (BDD) we give a high level description of what it is and why it is useful for developers. Then we go into some details on stubs and mocks, test data, UI testing, SQL testing, JavaScript testing, web services testing and how to start doing TDD/BDD on an existing code base.

Published in: Technology, Education
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THE is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, EBOOK, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THE Helped Ignite a Movement,-- Atomic Habits: An Easy &amp; Proven Way to Build Good Habits &amp; Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THE the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • D0WNL0AD FULL ▶ ▶ ▶ ▶ http://1lite.top/rTNzk ◀ ◀ ◀ ◀
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Although I'm a KS3 student, I decided to give Jeevan's guide a try because I know GCSE and KS3 are fairly similar. Looking back now, it was one of the best decisions I made. The guide has helped me jump from a level 6A to a level 8C in maths, and I'm only in year 8! Achieving a level 8 in maths is regarded as a gifted individual. Thank you for this terrific guide and I will recommend it to anyone taking a maths exam!  http://t.cn/AirraVnG
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Test and Behaviour Driven Development (TDD/BDD)

  1. 1. Test & BehaviourDriven DevelopmentLars ThorupZeaLake Software ConsultingMarch, 2012
  2. 2. Who is Lars Thorup?● Software developer/architect ● C++, C# and JavaScript ● Test Driven Development● Coach: Teaching agile and automated testing● Advisor: Assesses software projects and companies● Founder and CEO of BestBrains and ZeaLake
  3. 3. Why are we here today?● What is TDD/BDD? ● Express expected behaviour before writing code● Why is TDD/BDD a good thing? ● 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● How do we do TDD/BDD? ● Write test programs ● Run the tests automatically
  4. 4. Workflow of TDD/BDD Think, talk Idea Test Failing test Code Good Succeeding design Refactor test
  5. 5. BDD or TDD?● Behaviour first ● makes more sense than "Test first"● Structure of test programs ● Given <precondition> ● When <invocation> ● Then <expectation>● High level as well as low level ● Testing user stories and requirements ● Testing class design and algorithms● Communicate intent● Fast feedback
  6. 6. Different kinds of automated tests● Unit tests ● Test individual pieces of code and the interaction between code blocks● System tests / acceptance tests ● Verify the behaviour of the entire system against the requirements● Performance tests ● Test non functional requirements
  7. 7. Unit tests or system tests?● Unit tests are efficient ● Fast to run (hundreds per second) ● Robust and predictable ● Can be easy to write ● Is written together with the code it is testing● System tests are thorough ● Tests all layers together ● Most efficient way to create a set of tests for existing code ● Can be easier to read for non-technical people
  8. 8. Can we automate performance tests?● Performance tests are brittle ● Tip: create performance trend curves instead
  9. 9. How do we run the tests automatically?● From our programming environment (IDE) ● Command line: make test ● Right click | Run Tests● On every commit ● Setup a build server ● Jenkins, TeamCity ● Let the build server run all tests ● Get build notifications ● Keep the build green ● Fixing a broken build has priority over any other development task
  10. 10. How can tests help improve our design?● The software design will evolve over time● A refactoring improves the design without changing behavior● Tests ensure that behavior is not accidentally changed● Without tests, refactoring is scary ● and with no refactoring, the design decays over time● With tests, we have the courage to refactor ● so we continually keep our design healthy
  11. 11. Are we wasting developer time writing tests?● No● Time spent writing tests is not taken from time spent coding ● ... but from time otherwise spent on manual testing and debugging● The cost of a bug keeps increasing until we fix it● Find bugs faster ● Avoid losing customer confidence ● Free QA to do exploratory testing so they find the hard-to-find bugs ● Spend less time trying to figure out what is causing the bug and how to fix it● Avoid spending time testing again
  12. 12. How do we get started?● When we have a lot of existing code without tests ● Create a set of system tests to get a safety net● When we are writing new code ● Write unit tests in conjunction with the new code● Set up a standard test environment for our specific application ● Test data: Automate the creation of standard testdata in a local database ● External dependencies: Write stubs to use in the tests
  13. 13. Maintainability● Stick to a pattern for your tests ● E.g. Given-When-Then● Focus on readability over code duplication in test code● Write reusable helper classes (builders) to simplify tests
  14. 14. What does a real-world project look like?● wizerize.com ● Web application: C# and JavaScript ● 3½ years of development, 3½ years in production ● 2-4 developers ● 40% test code, 60% production code (in lines of code) ● 71% code coverage of unit tests ● 872 unit tests – run in 1½ minute ● 72 system tests – run in 20 minutes ● No functional errors seen by end users in production (yet)
  15. 15. Where can I read more?● http://googletesting.blogspot.com/● http://testdrivendeveloper.com/● http://codesheriff.blogspot.com/● http://www.zealake.com/category/test/
  16. 16. But what about:● Stubs & mocks● Test data● UI testing● SQL testing● JavaScript testing● Web Service testing● Legacy code
  17. 17. What is good design?● One element of good design is loose coupling ● Use interfaces (for static languages) ● Inject dependencies public void Trigger()● Avoid using new: { var emailSvc = new EmailSvc(); emailSvc.SendEmail(); }● Inject dependencies instead: private IEmailSvc emailSvc; public Notifier(IEmailSvc emailSvc) { this.emailSvc = emailSvc; } public void Trigger() { emailSvc.SendEmail();
  18. 18. 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 NotifierTest ● Faster tests (Y may be slow) ● Simpler (Y may depend on Z etc)● Examples: IEmailSvc Notifier ● Time ● Database EmailSvcStub EmailSvc ● Email ● HttpContext
  19. 19. Stubs● Hand crafted● More effort to write● Easier to maintain● Can be more "black box" than mocks
  20. 20. Mocks● Mocks are automatically generated stubs● Easy to use● More "magical"● More effort to maintain● Will be more "white-box" than stubs● Example frameworks: ● Moq ● NSubstitute
  21. 21. 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)); }
  22. 22. 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(); }
  23. 23. Test data● Each developer his/her own database● Standard test data ● Created before running tests● Test data builders ● Stubbed database ● Real database
  24. 24. 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}));}
  25. 25. Web UI testing● Control a browser from the tests using a seperate tool● Tools ● Selenium ● WatiN ● Cucumber + capybara● Minimize system level testing ● Web UI tests are brittle and slow ● Hard to integrate into continuous integration● Maximize JavaScript unit testing
  26. 26. SQL testing● Test stored procedure, constraints, functions and triggers● Use your backend testing framework (like NUnit) ● Easy to integrate in your Continuous Integration process● Consider using a dedicated framework● Or write your own
  27. 27. JavaScript testing● Use a JavaScript unit testing framework ● QUnit ● jsTestDriver ● Jasmine
  28. 28. Web Service testing● Use your backend testing framework (like NUnit)● Use a JSON friendly version of WebClient: // when var votes = jsonClient.Get("Vote", "GetVotes", new { questionId = questionId }); // then Assert.That(votes.Length, Is.EqualTo(1)); var vote = votes[0]; Assert.That(vote.ResponseText, Is.EqualTo("3")); Assert.That(vote.ParticipantName, Is.EqualTo("Lars Thorup"));● Input converted from .NET anonymous type to JSON● Output converted from JSON to .NET dynamic type● https://github.com/larsthorup/JsonClient
  29. 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

×