Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Test Driven Development

Test Driven Development

TDD meetup slides at PHP Reboot on 8th April, 2017.

Kapil Sharma

April 08, 2017
Tweet

More Decks by Kapil Sharma

Other Decks in Programming

Transcript

  1. PHPReboot.com Kapil Sharma About me • Kapil Sharma. • Technical

    Architect at Eastern Enterprise. • Working in Web Application development since 12+ years. • Twitter: @kapilsharmainfo • Facebook: /kapilsharmainfo • Linked in: kapilsharmainfo • Website: kapilsharma.info • Slides: speakerdeck.com/kapilsharma 2
  2. PHPReboot.com Kapil Sharma About PHPReboot 3 • PHP developers community

    in Pune, India. • Conduct regular meet ups • meetup.com/phpreboot • phpreboot.com • Twitter: @phpreboot • Facebook: /PHReboot • Slack: phpreboot.signup.team • Virtual team developing open source. • PHP Magazine at phpreboot.com. • PHPreboot.com is open source website • github.com/phpreboot/website • Issues: waffle.io/phpreboot/ website • Contributions welcome.
  3. PHPReboot.com Kapil Sharma – Wikipedia “Test-driven development (TDD) is a

    software development process that relies on the repetition of a very short development cycle: requirements are turned into very specific test cases, then the software is improved to pass the new tests, only.” 4 What is TDD?
  4. PHPReboot.com Kapil Sharma Quotes • All code is guilty, until

    proven innocent. • First, solve the problem. Then, write the code. • Good programmers write code for humans first and computers next. • Don’t fix it if it is ain’t broke. • Quality is free, but only to those who are willing to pay heavily for it. 6
  5. PHPReboot.com Kapil Sharma What is TDD? • TDD is test-first

    approach of software development. • It leads to think ‘How to use’ first and then about ‘How to implement’. 7
  6. PHPReboot.com Kapil Sharma TDD Wars • Short story based on

    Star Wars movie. • Next 4 slides are taken from Michelangelo Van Dam’s (DragonBe) presentation ‘PHPUnit Episode iv.iii: Return of the tests’ • I really like that theme of PHP Unit presentations and wanted to share few slides. • Original presentation: https://www.slideshare.net/ DragonBe/phpunit-episode-iviii-return-of-the-tests 8
  7. PHPReboot.com Kapil Sharma Why developers not writing tests? • No

    time. • No budget. • Write once project finishes (Never). • We do not know how to write. 13
  8. PHPReboot.com Kapil Sharma Responsibility • Developer’s job is to: •

    Write code. • Fix bugs. • Add documentation. • Write and update unit tests. 14
  9. PHPReboot.com Kapil Sharma Benefits of testing • Direct feedback (test

    fails). • Once a test is made, it will always be tested. • Easy to refactor existing code (protection). • Easy to debug: Write a test to verify if bug is valid. • Higher confidence and less uncertainty. 15
  10. PHPReboot.com Kapil Sharma – Martin Fowler “Whenever you are tempted

    to type something into a print statement or a debugging expression, write it as a test instead.” 16
  11. PHPReboot.com Kapil Sharma PHP Unit • PHPUnit is a port

    of xUnit testing framework. • Created by Sebastian Bergmann. • User ‘assertions’ to verify behaviour of ‘unit of code’. • Open source and hosted on Github. • Can be installed using • PHAR • Composer 18
  12. PHPReboot.com Kapil Sharma Assertions • assertArrayHasKey • assertClassHasAttribute • assertArraySubset

    • assertClassHasStaticAttribute • assertContains • assertContainsOnly • assertContainsOnlyInstanceOf • assertCount • assertDirectoryExists • assertDirectoryIsReadable • assertDirectoryIsWritable • assertEmpty() • assertEqualXMLStructure() • assertEquals() • assertFalse() 19 • assertFileEquals() • assertFileExists() • assertFileIsReadable() • assertFileIsWritable() • assertGreaterThan() • assertGreaterThanOrEqual() • assertInfinite() • assertInstanceOf() • assertInternalType() • assertIsReadable() • assertIsWritable() • assertJsonFileEqualsJsonFile() • assertJsonStringEqualsJsonFile() • assertJsonStringEqualsJsonString() • assertLessThan() • assertLessThanOrEqual() • assertNan() • assertNull() • assertObjectHasAttribute() • assertRegExp() • assertStringMatchesFormat() • assertStringMatchesFormatFile() • assertSame() • assertStringEndsWith() • assertStringEqualsFile() • assertStringStartsWith() • assertThat() • assertTrue() • assertXmlFileEqualsXmlFile() • assertXmlStringEqualsXmlFile() • assertXmlStringEqualsXmlString()
  13. PHPReboot.com Kapil Sharma Data Providers • We might need same

    test to be executed against different inputs. • Data providers help providing different data (input) to test method as parameters. 20
  14. PHPReboot.com Kapil Sharma Mocks • Sometime, we can’t test a

    method because: • It has difficult to resolve dependencies. • It calls another method that we can’t execute. 23
  15. PHPReboot.com Kapil Sharma Lots more to cover • We just

    saw possibilities at high level. • Refer PHP Unit docs for much more details. • We will cover some of the practical uses in following session and workshop. 25