Getting started with Testing Adewale Andrade. 28/07/2017, Salvador, Bahia
WHY TEST? ○ Know that your code works…(and doesn’t breaks anyone else’s). ○ Make deliveries and deploys more stable…. ○ Saves time (and pain) wasted on bug fixing later… ○ Improving code designs and refactoring… ○ Reduce fear of failing...
TYPES OF TESTS (most common) ○ Unit Tests: ● Tests a small part of the application independently from other parts; (Testing one class’s method); ○ Integration Test (Functional Tests): ● Tests different parts of the application working together; (Testing a controller using multiple classes) ○ End-To-End Tests (E2E): ● Tests whether the flow of an application performs as designed from start to finish; (Testing a login flow)
OUR STACK ○ PHPUnit ○ Neos Flow ○ PHP 5.5.9 ○ Apache 2
OK… BUT….. What are we really testing?
Test behaviour Not implementation….
“When testing, face the code like a complete stranger… You have no idea how it does what it should do… But you know your expectations!
WHAT MAKES A GOOD UNIT TEST? ○ Unit tests should be: ● Easy to write; ● Easy to read; ● Fast; ● Not an integration test ■ No access to external resources (network, databases, file systems, etc.)
A SIMPLE UNIT TEST
A SIMPLE INTEGRATION TEST
WHAT DO YOU NEED TO KNOW? ○ What are you testing? ○ How should it behave? ○ What are the edge cases? ○ Mapped entries? ○ Expected results?
OK… BUT….. Does the code quality even matter?
THINGS TO AVOID As much as possible!
NON-DETERMINISTIC FACTORS
INTERNAL DEPENDENCIES
WHEN TO USE EACH ONE ○ Unit Tests: ● Models ● Services methods that don’t have internal dependencies; ○ Integration Tests: ● Controllers; ● Services; ● Repositories;
HANDLING DEPENDENCIES ○ Unit tests: ● Maybe you should refactor it…. ● Dependency Injection; ● Mocking; ● Maybe it is an integration test; https://jtreminio.com/2013/03/unit-testing-tutorial-part-4-mock-objects-stub-methods-depen dency-injection/
HANDLING DEPENDENCIES ○ Integration tests: ● Dependency Injection; ● Mocking; ● Do not mock things that you are testing.
Show me the code! Finally… some action!
REINFORCING THE CULTURE ○ Pull Requests + Code Review ○ Continuous Integration Servers ● Coverage Tests ● Build Breaking and Notifying ○ Leading by example
Thanks ;) “With faith, my brother” Adewale Andrade Software Engineer @Agilize Contabilidade Online adewale.andrade@gmail.com

Getting started with unit and functional testing

  • 1.
    Getting started with Testing AdewaleAndrade. 28/07/2017, Salvador, Bahia
  • 2.
    WHY TEST? ○ Knowthat your code works…(and doesn’t breaks anyone else’s). ○ Make deliveries and deploys more stable…. ○ Saves time (and pain) wasted on bug fixing later… ○ Improving code designs and refactoring… ○ Reduce fear of failing...
  • 3.
    TYPES OF TESTS(most common) ○ Unit Tests: ● Tests a small part of the application independently from other parts; (Testing one class’s method); ○ Integration Test (Functional Tests): ● Tests different parts of the application working together; (Testing a controller using multiple classes) ○ End-To-End Tests (E2E): ● Tests whether the flow of an application performs as designed from start to finish; (Testing a login flow)
  • 4.
    OUR STACK ○ PHPUnit ○Neos Flow ○ PHP 5.5.9 ○ Apache 2
  • 5.
    OK… BUT….. What arewe really testing?
  • 6.
  • 7.
    “When testing, facethe code like a complete stranger… You have no idea how it does what it should do… But you know your expectations!
  • 8.
    WHAT MAKES AGOOD UNIT TEST? ○ Unit tests should be: ● Easy to write; ● Easy to read; ● Fast; ● Not an integration test ■ No access to external resources (network, databases, file systems, etc.)
  • 9.
  • 10.
  • 11.
    WHAT DO YOUNEED TO KNOW? ○ What are you testing? ○ How should it behave? ○ What are the edge cases? ○ Mapped entries? ○ Expected results?
  • 12.
    OK… BUT….. Does thecode quality even matter?
  • 14.
    THINGS TO AVOID Asmuch as possible!
  • 15.
  • 16.
  • 17.
    WHEN TO USEEACH ONE ○ Unit Tests: ● Models ● Services methods that don’t have internal dependencies; ○ Integration Tests: ● Controllers; ● Services; ● Repositories;
  • 18.
    HANDLING DEPENDENCIES ○ Unittests: ● Maybe you should refactor it…. ● Dependency Injection; ● Mocking; ● Maybe it is an integration test; https://jtreminio.com/2013/03/unit-testing-tutorial-part-4-mock-objects-stub-methods-depen dency-injection/
  • 19.
    HANDLING DEPENDENCIES ○ Integrationtests: ● Dependency Injection; ● Mocking; ● Do not mock things that you are testing.
  • 20.
  • 21.
    REINFORCING THE CULTURE ○Pull Requests + Code Review ○ Continuous Integration Servers ● Coverage Tests ● Build Breaking and Notifying ○ Leading by example
  • 22.
    Thanks ;) “With faith,my brother” Adewale Andrade Software Engineer @Agilize Contabilidade Online adewale.andrade@gmail.com