name: inverse layout: true class: center, middle, inverse --- # Test Driven Development ### What's the big stink? --- template: inverse ## How many people currently do TDD or some sort of testing? --- template: inverse ### Unless you write all your code and deploy immediately...you are testing. --- layout: false ## Testing vs. TDD ### TDD * TDD is a practice, an approach to development where tests are written first and the design of code is driven by making the tests pass. This approach keeps testability at the forefront of the developer's mind when coding. * Developing this way, with an emphasis on test first, and the the iterative process of writing small pieces of code, testing, and proceeding, allows issues to be caught sooner as code is not written that is not tested. Changes made are not made without being tested, fully. * It seems that TDD has become almost synonymous with `testing` in general, but TDD is a much more methodical process that incorporates `testing`. --- layout: false ## Testing vs. TDD ### Testing * Testing refers to writing automated tests but tests do not have to be written first. * To be fair though, testing is just verifying that something works, so refreshing a browser is a basic form of testing. However, the idea of testing generally refers to automated tests. * Great to add to a legacy project so that you can refactor the code with confidence. This situation though, is extremely difficult, especially if previous developer has moved on. --- ### The Promise of TDD 1. Code Confidence 1. Cleaner Code 1. Faster Development 1. Worry-free weekends --- ### The Reality 1. I believe it does help in code confidence * However, your test may not account for each edge case and will pass, but doesn't mean it's 100% 1. I think it does help in cleaner code, because you have to write code in a way that's testable. Often this keeps things a little simpler. 1. I think it would help with development times * But you have to compare time of writing tests and development with standard approach of development, troubleshooting and bug fixes. * I think TDD helps to reduce troubleshooting and likelihood of bugs. 1. If your tests are passing, then your weekend should be uninterrupted. -- > Warning! Testing is a possible solution to these common development problems, but it isn't going to solve everything. It's a tool, not the path of enlightenment. --- ## Cool, why isn't everyone doing TDD? -- Good Question, next question. -- In my own experience with TDD, which is limited, it probably is because making the shift to TDD is a complete shift in our approach. When we all start out doing development, we write some code, go to the browser, refresh, see what we get. This process becomes ingrained in us. It isn't until years into development, that many of us even begin hearing about Testing and TDD. After years of testing in the browser, the idea seems foreign. -- ### It feels like additional work that we do not have time to do. -- Regardless of the fact, that testing in the browser takes time. We are just so used to it, that it seems like there is no alternative, no quicker way. -- ### However, automated tests can run very fast and test an entire application in seconds or minutes. A feat impossible by 'in browser testing'. --- class: middle, center ## Also, it requires an entire team to "buy-in" and adhere to it. --- ### One cowboy can ruin it for everyone ![](images/leeroy-jenkins-cat.jpg) --- ### Testing Frameworks in PHP 1. PHPUnit - [https://phpunit.de/](https://phpunit.de/) 1. PHPSpec - [http://www.phpspec.net/en/latest/](http://www.phpspec.net/en/latest/) 1. Behat - [http://docs.behat.org/en/v3.0/](http://docs.behat.org/en/v3.0/) 1. Codeception - [http://codeception.com/](http://codeception.com/) 1. There are others, but these are the big ones. -- ### Mocking in PHP 1. PHPUnit, has built in mocking 1. Mockery - [http://docs.mockery.io/en/latest/](http://docs.mockery.io/en/latest/) 1. Phake - [http://phake.readthedocs.io/en/2.1/](http://phake.readthedocs.io/en/2.1/) -- Sometimes in testing, a package may be difficult to setup in order to use it in testing; or you don't want to rely on external dependencies. Mocking allows you to fake an object, and fake a method call and return values that you know an object should return. Mocking is a little tricky to really wrap your head around what it is, and when you should actually do it. To be honest, I can't really say that I am an expert on mocking, but it is a technique that you should be aware of when testing. --- template: inverse ## Let's do some live code --- layout: false name: last-page template: inverse ## Andrew Huggins ### @andy_huggins ### andrewhuggins@gmail.com Slides: http://ahuggins.github.io/presentation-tdd/ .footnote[ [Leeroy Jenkins](https://www.youtube.com/watch?v=mLyOj_QD4a4) ]