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

Unit Testing: The Why and The How

Unit Testing: The Why and The How

A basic introduction to reasons to do unit testing and basics of doing unit testing in PHPUnit.

Justin Yost

March 03, 2016
Tweet

More Decks by Justin Yost

Other Decks in Technology

Transcript

  1. Unit Testing: The Why and The How Justin Yost Web

    Developer at Loadsys 1 CC BY-NC 4.0 Justin Yost
  2. Unit Testing Firstly there is a notion that unit tests

    are low-level, focusing on a small part of the software system. Secondly unit tests are usually written these days by the programmers themselves using their regular tools - the only difference being the use of some sort of unit testing framework. Thirdly unit tests are expected to be significantly faster than other kinds of tests. — Martin Fowler 3 CC BY-NC 4.0 Justin Yost
  3. Unit Testing • Test and verify the intent and design

    of units of code, typically at the function/method level in PHP and C type languages. • Unit tests are written in the same language you write your code in (though you may include a test framework). • Should be fast, they shouldn't slow down your process for writing code. 4 CC BY-NC 4.0 Justin Yost
  4. Unit Testing Goals • Verify implementation of code matches the

    programmer's intent. • Verify bugs are solved today and in the future (Regressions). • Create better designed software. • Improve Refactoring 5 CC BY-NC 4.0 Justin Yost
  5. Why Unit Testing • Can't I just manually test stuff?

    • Isn't unit testing going to slow me down? • No one cares? • Business/Manager/Client/etc won't let me spend time on this stuff. 6 CC BY-NC 4.0 Justin Yost
  6. Complaint: Manually Test • Slow • You write code not

    hitting refresh • Computers do repetitive stuff really well • You forget • You make mistakes • You are bad at remembering what you tested • You are bad at communicating needed tests to other co-workers 7 CC BY-NC 4.0 Justin Yost
  7. Complaint: Unit Testing is Slow • Unit Testing is overall

    faster • It may be slower in terms of pure speed to develop a thing • Total Time to a reliable useful product is slower 8 CC BY-NC 4.0 Justin Yost
  8. Research The results of the case studies indicate that the

    pre-release defect density of the four products decreased between 40% and 90% relative to similar projects that did not use the TDD practice. Subjectively, the teams experienced a 15–35% increase in initial development time after adopting TDD. — Realizing quality improvement through test driven development: results and experiences of four industrial teams http:/ /research.microsoft.com/en-us/groups/ese/nagappan_tdd.pdf 9 CC BY-NC 4.0 Justin Yost
  9. Complaint: Unit Testing is Slow • Unit Testing may be

    slower in the initial learning, phase, so is the new framework, so is the new language, so is everything • The speed improvements in the long run will be worth it • The short improvements in loss of defects will easily be worth it 10 CC BY-NC 4.0 Justin Yost
  10. Complaint: Who Cares? • Your customers care about quality •

    Your boss/manager should care about quality 13 CC BY-NC 4.0 Justin Yost
  11. Complaint: Who Cares? • Unit testing isn't just fixing bugs

    that you observe • It's about preventing bugs in the first place 14 CC BY-NC 4.0 Justin Yost
  12. Complaint: No Time • Microsoft Research • If you have

    limited time then you should care even more, for any bugs are detrimental to your time 15 CC BY-NC 4.0 Justin Yost
  13. General Theory of Unit Testing • Unit Testing is writing

    code that proves what you did works • Unit Testing means you stay in the flow of the code you are working on longer • Unit Testing means you spend less time debugging and figuring out what went wrong • Unit Testing means you spend time thinking through your code 16 CC BY-NC 4.0 Justin Yost
  14. General Theory of Unit Testing • None of these may

    be perfect • None of these may the right answer • You should do it because you care about producing quality software • If you don't learn now, what happens when you really need it? 17 CC BY-NC 4.0 Justin Yost
  15. The Why • It is faster • It produces a

    better quality product • It will make you a better developer/engineer 18 CC BY-NC 4.0 Justin Yost
  16. PHPUnit • A framework for writing unit tests • Frameworks

    may have their own customizations 20 CC BY-NC 4.0 Justin Yost
  17. PHPUnit Install • Phar wget https://phar.phpunit.de/phpunit.phar chmod +x phpunit.phar sudo

    mv phpunit.phar /usr/local/bin/phpunit 21 CC BY-NC 4.0 Justin Yost
  18. Other Resources on Unit Testing? • Grumpy Programmer: Chris Hartjes

    - https:/ /grumpy-learning.com/ • PHPUnit: https:/ /phpunit.de/ 25 CC BY-NC 4.0 Justin Yost