Advertisement
Advertisement

More Related Content

Advertisement

When you get lost in api testing #SunPHP20

  1. WHEN YOU GET LOST IN API TESTING Paula Čučuk
  2. About me Paula Čučuk paula@locastic.com @paulala_14 Backend developer @ Locastic
  3. Locastic Helping clients create web and mobile apps since 2011 • UX/UI • Mobile apps • Web apps • Training & Consulting www.locastic.com @locastic
  4. Intro Experience with API testing in Locastic Testing approach using examples from projects Testing tools Things we learned along the way
  5. Manual testing Boring Time consuming Unlikely to achieve acceptable coverage
  6. Functional tests Functional testing is defined as the testing of complete functionality of some application.
  7. Functional tests In each functional tests check: Response code Response header Response content: contains expected fields in expected format
  8. Functional tests Test that each endpoint behaves as expected for: OK request and response Bad request and Validation response Forbidden access response
  9. CRUD Event testing with Postman
  10. 1. Import JSON Schema using OpenAPI
  11. Open API Allows you to specify: Available endpoints and operations Input and output parameters for each operation Authentication methods Contact information, license, terms of use and other information. An API description format for REST APIs
  12. Adding assertions to Collection
  13. Environment variables
  14. Test creating new event
  15. Test editing created event
  16. Test get event
  17. Test deleting event
  18. Test get events list
  19. All done! Let’s run the tests
  20. Postman tests + Newman Run and test a Postman Collection directly from the command line
  21. Import request from network
  22. Let’s see the same test case in PHPUnit
  23. The test Http Client in API Platform
  24. Test creating event with bad data
  25. Test creating new event
  26. Test get created event
  27. Test editing event
  28. Test get list of events
  29. Test get list of events unauthorised
  30. Test deleting event
  31. Refreshing database data hautelook/alice-bundle
  32. Refreshing database data dmaicher/doctrine-test-bundle
  33. Postman vs PhpUnit speed PhpUnit is 2.5 times faster
  34. Postman vs PHPUnit Postman PHPUnit Easy to use and setup Faster execution Interactive documentation and specification for FE developers and QA More flexible Import using Open API Free
  35. Functional tests benefits ✓ Less bugs ✓ Faster testing ✓ Worked perfect on small project
  36. Import results feature flow
  37. The test pyramid
  38. Unit tests Unit testing is a level of testing where individual units/components are tested. The purpose is to validate that each separate unit performs as expected. A unit is the smallest testable part of any software.
  39. Import results feature flow
  40. Racer Medal Consumer class
  41. Unit tests Catch errors in early stage Very fast execution Easy to write
  42. It’s not unit tests, it’s you! Refactor your code using SOLID principles.
  43. Import results feature flow
  44. Test repository method example
  45. Test repository method example
  46. Unit test
  47. UNIT TEST IS NOT USEFUL HERE
  48. Integration tests Integration testing is the phase in software testing in which individual software modules are combined and tested as a group.
  49. When to use integration tests Validation 3rd party integration Database queries Verifying two or more modules which have unit tests work as expected together
  50. Integration test Step 1: Prepare test data
  51. Event Time Category Gender Distance Disqualified Race Category 1 NULL a1 male long FALSE Individual/Team 1 2:00:00 a2 male long TRUE Individual/Team 1 2:00:00 a4 male long FALSE Him & Her 1 2:01:00 a1 male long FALSE Individual/Team 1 2:05:00 a2 female long FALSE Individual/Team 1 2:10:00 a1 male long FALSE Individual/Team 1 2:10:00 a2 female long FALSE Individual/Team 1 2:40:00 a3 male medium FALSE Individual/Team 1 3:10:00 a2 female long FALSE Individual/Team 2 2:15:00 a2 female long FALSE Individual/Team
  52. Event Time Category Gender Distance Disqualified Race Category 1 NULL a1 male long FALSE Individual/Team 1 2:00:00 a2 male long TRUE Individual/Team 1 2:00:00 a4 male long FALSE Him & Her 1 2:01:00 a1 male long FALSE Individual/Team 1 2:05:00 a2 female long FALSE Individual/Team 1 2:10:00 a1 male long FALSE Individual/Team 1 2:10:00 a2 female long FALSE Individual/Team 1 2:40:00 a3 male medium FALSE Individual/Team 1 3:10:00 a2 female long FALSE Individual/Team 2 2:15:00 a2 female long FALSE Individual/Team
  53. Event Time Category Gender Distance Disqualified Race Category 1 NULL a1 male long FALSE Individual/Team 1 2:00:00 a2 male long TRUE Individual/Team 1 2:00:00 a4 male long FALSE Him & Her 1 2:01:00 a1 male long FALSE Individual/Team 1 2:05:00 a2 female long FALSE Individual/Team 1 2:10:00 a1 male long FALSE Individual/Team 1 2:10:00 a2 female long FALSE Individual/Team 1 2:40:00 a3 male medium FALSE Individual/Team 1 3:10:00 a2 female long FALSE Individual/Team 2 2:15:00 a2 female long FALSE Individual/Team
  54. Event Time Category Gender Distance Disqualified Race Category 1 NULL a1 male long FALSE Individual/Team 1 2:00:00 a2 male long TRUE Individual/Team 1 2:00:00 a4 male long FALSE Him & Her 1 2:01:00 a1 male long FALSE Individual/Team 1 2:05:00 a2 female long FALSE Individual/Team 1 2:10:00 a1 male long FALSE Individual/Team 1 2:10:00 a2 female long FALSE Individual/Team 1 2:40:00 a3 male medium FALSE Individual/Team 1 3:10:00 a2 female long FALSE Individual/Team 2 2:15:00 a2 female long FALSE Individual/Team
  55. Event Time Category Gender Distance Disqualified Race Category 1 NULL a1 male long FALSE Individual/Team 1 2:00:00 a2 male long TRUE Individual/Team 1 2:00:00 a4 male long FALSE Him & Her 1 2:01:00 a1 male long FALSE Individual/Team 1 2:05:00 a2 female long FALSE Individual/Team 1 2:10:00 a1 male long FALSE Individual/Team 1 2:10:00 a2 female long FALSE Individual/Team 1 2:40:00 a3 male medium FALSE Individual/Team 1 3:10:00 a2 female long FALSE Individual/Team 2 2:15:00 a2 female long FALSE Individual/Team
  56. Event Time Category Gender Distance Disqualified Race Category 1 NULL a1 male long FALSE Individual/Team 1 2:00:00 a2 male long TRUE Individual/Team 1 2:00:00 a4 male long FALSE Him & Her 1 2:01:00 a1 male long FALSE Individual/Team 1 2:05:00 a2 female long FALSE Individual/Team 1 2:10:00 a1 male long FALSE Individual/Team 1 2:10:00 a2 female long FALSE Individual/Team 1 2:40:00 a3 male medium FALSE Individual/Team 1 3:10:00 a2 female long FALSE Individual/Team 2 2:15:00 a2 female long FALSE Individual/Team
  57. Event Time Category Gender Distance Disqualified Race Category 1 NULL a1 male long FALSE Individual/Team 1 2:00:00 a2 male long TRUE Individual/Team 1 2:00:00 a4 male long FALSE Him & Her 1 2:01:00 a1 male long FALSE Individual/Team 1 2:05:00 a2 female long FALSE Individual/Team 1 2:10:00 a1 male long FALSE Individual/Team 1 2:10:00 a2 female long FALSE Individual/Team 1 2:40:00 a3 male medium FALSE Individual/Team 1 3:10:00 a2 female long FALSE Individual/Team 2 2:15:00 a2 female long FALSE Individual/Team
  58. Integration test Step 2: trigger repository method and check if data updated as expected
  59. Test payment endpoint example
  60. Test payment - mocking 3rd party calls
  61. Test payment - functional test
  62. New approach benefits More coverage, less bugs Faster test execution Easier and faster refactoring Improving code quality Time saving
  63. Handy testing tools
  64. Infection Tool for mutation testing in PHP
  65. PHP Stan PHP Static Analysis Tool
  66. Continuous integration (CI) Tool for tuning tests on GIT
  67. What we learned along the way
  68. Do I need to write tests first? It’s just one of the approaches
  69. Do I need to have 100% coverage? 100% coverage doesn’t guarantee your code is fully tested and working
  70. What about legacy code? 1. Start replicating bugs with tests before fixing them 2. Test at least most important and critical parts
  71. How do I test some specific code? Check out the tests of the libraries and framework you are using
  72. How do I know if the tests I wrote are good enough? Ask yourself: “Am I sure the code I tested works as it should?”
  73. Conclusion You should write tests! Think about your projects requirements If something is hard to test - refactor it Check out the tools we mentioned
  74. Thank you!
  75. Questions? https://joind.in/talk/908a3 Paula Čučuk paula@locastic.com @paoolala Backend developer @ Locastic
Advertisement