$30 off During Our Annual Pro Sale. View Details »

Why You Can't Test

Why You Can't Test

Slides for a talk I gave at CakeFest 2014 about obstacles developers face when they decide to start writing automated tests for their code.

Chris Hartjes

August 24, 2014
Tweet

More Decks by Chris Hartjes

Other Decks in Technology

Transcript

  1. WHY YOU CAN'T TEST
    Chris Hartjes - CakeFest 2014
    August 24, 2014

    View Slide

  2. View Slide

  3. Testing Is Hard

    View Slide

  4. View Slide

  5. 7 Obstacles

    View Slide

  6. Configuration
    Configuring tools

    is difficult

    View Slide

  7. Configuration
    Use Composer (http://getcomposer.org) to isolate tools to your
    project
    Modify your project to fit the defaults of tools whenever possible
    Avoid fighting with tools but also resist urge to write your own

    View Slide

  8. Configuration
    Composer + Packagist = library management
    Keeps dependencies as part of the project

    View Slide

  9. Configuration
    {
    "require": {
    "fabpot/goutte": "1.0.*@dev",
    "igorw/yolo": "~42.0@dev"
    }
    }

    View Slide

  10. Configuration
    Making your own packages available is easy
    Standard locations for files reduces confusion

    View Slide

  11. Configuration
    Avoid “my project is SOOOOO unique” mentality
    Makes it increasingly difficult to integrate other projects
    Convention over configuration matters

    View Slide

  12. Education
    Developers not taught

    how to test

    View Slide

  13. Education

    View Slide

  14. Education
    Impossible to learn how to write tests in isolation
    Mentorships are awesome! (http://phpmentoring.org)
    Talking about mentoring is awesome (#phpmentoring
    on Freenode)

    View Slide

  15. Education
    It takes two to test

    View Slide

  16. Education

    View Slide

  17. Education
    Mentoring is a great way to make connections
    My own apprentices have gone on to success
    Easy way to give back to the community

    View Slide

  18. Education

    View Slide

  19. Coupling
    Tightly-coupled

    code common

    View Slide

  20. Coupling
    Learn about dependency injection
    Learn about Demeter’s Law
    Understand WHY coupling is bad

    View Slide

  21. Coupling
    You probably know DI by a less-complicated name
    You probably already do it

    View Slide

  22. Coupling

    View Slide

  23. Coupling
    DI Containers / Service Locators
    Constructor injection
    Setter injection

    View Slide

  24. Coupling
    Containers are simply a way to store something for later retrieval
    Think “define stuff in my bootstrap sequence”

    View Slide

  25. Coupling

    View Slide

  26. Coupling

    View Slide

  27. Coupling
    Dependency Injection Containers and Service Locators are
    simply globally-available ways to retrieve stuff
    Be sure to not make you code dependant on the container /
    locator!

    View Slide

  28. Dependencies
    Too many

    dependencies

    View Slide

  29. Dependencies
    You never know it until you try to create mocks for them
    Every dependency you add needs to be justified

    View Slide

  30. Dependencies
    “Mocking” is the creation of copies of dependencies the code
    you are testing has
    You create those copies in a specific state
    Easiest way to see if your code is testable — can you easily inject
    dependencies in?

    View Slide

  31. Dependencies
    http://www.littlehart.net/atthekeyboard/2012/03/27/
    metatesting-understanding-mock-objects/

    View Slide

  32. Misconceptions
    “You are paid to

    write code, not tests!”

    View Slide

  33. Misconceptions
    Getting paid not to write code, but to solve problems
    Many people discount the costs of fixing bugs
    Many people discount the costs of missed opportunities bugs
    create

    View Slide

  34. Misconceptions
    TDD is about shifting bug-fixing to a cheaper part of the process
    TDD is a design pattern!
    20% to 40% more time for 40% to 90% fewer bugs

    View Slide

  35. Misconceptions
    Tests can help you go home from work on time every day

    View Slide

  36. Misconceptions
    Bugs found in development cost $X
    Bugs found by QA cost $2X
    Bugs found in production cost $10X

    View Slide

  37. Environments
    Lack of control

    over environments

    View Slide

  38. Environments
    Composer can play double duty
    Vagrant / other virtual machines are key
    Repeatable automated processes are key

    View Slide

  39. Environments
    Composer lets you lock down the environment
    Easy to specify versions of all libraries
    Reduce likelihood of “works on my machine”

    View Slide

  40. Environments
    http://vagrantup.com
    Learn it, live it, love it

    View Slide

  41. Environments
    People make mistakes all the tie
    Computers make mistakes you tell them to
    Examine procedures carefully to find automation opportunities

    View Slide

  42. Motivation
    Maintenance

    programming

    sucks

    View Slide

  43. Motivation
    Good test suites make bug fixes easy
    Good test suites make it easier to integrate new things

    View Slide

  44. Motivation
    Good test suites mean more time worrying about big problems
    Good test suites mean less time worrying about regressions

    View Slide

  45. Shameless Self-Promotion
    http://grumpy-testing.com http://grumpy-phpunit.com

    View Slide

  46. Feed My Ego
    [email protected]
    @grmpyprogrammer
    ASK ME ANYTHING - I READ ALL EMAIL AND TWEETS
    https://joind.in/11602

    View Slide