Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

When you get lost in api testing #SunPHP20

45 views

Published on

If you are not writing tests yet, you should start. Tests will improve the quality of your code, reduce bugs and probably force you to think more about design and quality. Actually, the hardest thing is to start and figure out which type of tests to write. So you start researching and you find a lot of different materials and get even more confused. Even when you decide what type of tests to use, it can be hard to decide how to test some specific logic in your app. Don't give up! We have some tips we learned along the way that will hopefully make your life easier. In this talk, you will hear about different test types and when to use them. We'll also show some tools for checking the quality of your test.

Published in: Software
  • Be the first to comment

  • Be the first to like this

When you get lost in api testing #SunPHP20

  1. 1. WHEN YOU GET LOST IN API TESTING Paula Čučuk
  2. 2. About me Paula Čučuk paula@locastic.com @paulala_14 Backend developer @ Locastic
  3. 3. Locastic Helping clients create web and mobile apps since 2011 • UX/UI • Mobile apps • Web apps • Training & Consulting www.locastic.com @locastic
  4. 4. Intro Experience with API testing in Locastic Testing approach using examples from projects Testing tools Things we learned along the way
  5. 5. Manual testing Boring Time consuming Unlikely to achieve acceptable coverage
  6. 6. Functional tests Functional testing is defined as the testing of complete functionality of some application.
  7. 7. Functional tests In each functional tests check: Response code Response header Response content: contains expected fields in expected format
  8. 8. Functional tests Test that each endpoint behaves as expected for: OK request and response Bad request and Validation response Forbidden access response
  9. 9. CRUD Event testing with Postman
  10. 10. 1. Import JSON Schema using OpenAPI
  11. 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. 12. Adding assertions to Collection
  13. 13. Environment variables
  14. 14. Test creating new event
  15. 15. Test editing created event
  16. 16. Test get event
  17. 17. Test deleting event
  18. 18. Test get events list
  19. 19. All done! Let’s run the tests
  20. 20. Postman tests + Newman Run and test a Postman Collection directly from the command line
  21. 21. Import request from network
  22. 22. Let’s see the same test case in PHPUnit
  23. 23. The test Http Client in API Platform
  24. 24. Test creating event with bad data
  25. 25. Test creating new event
  26. 26. Test get created event
  27. 27. Test editing event
  28. 28. Test get list of events
  29. 29. Test get list of events unauthorised
  30. 30. Test deleting event
  31. 31. Refreshing database data hautelook/alice-bundle
  32. 32. Refreshing database data dmaicher/doctrine-test-bundle
  33. 33. Postman vs PhpUnit speed PhpUnit is 2.5 times faster
  34. 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. 35. Functional tests benefits ✓ Less bugs ✓ Faster testing ✓ Worked perfect on small project
  36. 36. Import results feature flow
  37. 37. The test pyramid
  38. 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. 39. Import results feature flow
  40. 40. Racer Medal Consumer class
  41. 41. Unit tests Catch errors in early stage Very fast execution Easy to write
  42. 42. It’s not unit tests, it’s you! Refactor your code using SOLID principles.
  43. 43. Import results feature flow
  44. 44. Test repository method example
  45. 45. Test repository method example
  46. 46. Unit test
  47. 47. UNIT TEST IS NOT USEFUL HERE
  48. 48. Integration tests Integration testing is the phase in software testing in which individual software modules are combined and tested as a group.
  49. 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. 50. Integration test Step 1: Prepare test data
  51. 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. 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. 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. 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. 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. 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. 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. 58. Integration test Step 2: trigger repository method and check if data updated as expected
  59. 59. Test payment endpoint example
  60. 60. Test payment - mocking 3rd party calls
  61. 61. Test payment - functional test
  62. 62. New approach benefits More coverage, less bugs Faster test execution Easier and faster refactoring Improving code quality Time saving
  63. 63. Handy testing tools
  64. 64. Infection Tool for mutation testing in PHP
  65. 65. PHP Stan PHP Static Analysis Tool
  66. 66. Continuous integration (CI) Tool for tuning tests on GIT
  67. 67. What we learned along the way
  68. 68. Do I need to write tests first? It’s just one of the approaches
  69. 69. Do I need to have 100% coverage? 100% coverage doesn’t guarantee your code is fully tested and working
  70. 70. What about legacy code? 1. Start replicating bugs with tests before fixing them 2. Test at least most important and critical parts
  71. 71. How do I test some specific code? Check out the tests of the libraries and framework you are using
  72. 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. 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. 74. Thank you!
  75. 75. Questions? https://joind.in/talk/908a3 Paula Čučuk paula@locastic.com @paoolala Backend developer @ Locastic

×