際際滷

際際滷Share a Scribd company logo
WHEN YOU GET LOST
IN API TESTING
Paula uuk
About me
Paula uuk
paula@locastic.com
@paulala_14
Backend developer @ Locastic
Locastic
Helping clients create web
and mobile apps since 2011
 UX/UI
 Mobile apps
 Web apps
 Training & Consulting
www.locastic.com
@locastic
Intro
Experience with API testing in Locastic
Testing approach using examples from projects
Testing tools
Things we learned along the way
Manual testing
Boring
Time consuming
Unlikely to achieve acceptable coverage
Functional tests
Functional testing is defined as the testing of
complete functionality of some application.
Functional tests
In each functional tests check:
Response code
Response header
Response content: contains expected fields in expected
format
Functional tests
Test that each endpoint behaves as expected for:
OK request and response
Bad request and Validation response
Forbidden access response
CRUD Event testing with PhpUnit
The test Http Client in API Platform
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
Test creating new event
Test creating event with bad data
Test creating new event
Test get event
Test get created event
Test editing created event
Test editing event
Test get events list
Test get list of events
Test get list of events unauthorised
Test deleting event
Test deleting event
Refreshing database data
Refreshing database data
hautelook/alice-bundle
Refreshing database data
dmaicher/doctrine-test-bundle
Functional tests benefits
 Less bugs
 Faster testing
 Worked perfect on small project
Import results feature flow
The test pyramid
When you get lost in api testing #DarkmiraTour
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.
Import results feature flow
Racer Medal Consumer
class
When you get lost in api testing #DarkmiraTour
When you get lost in api testing #DarkmiraTour
Unit tests
Catch errors in early stage
Very fast execution
Easy to write
When you get lost in api testing #DarkmiraTour
Its not unit tests, its you!
Refactor your code using SOLID principles.
Import results feature flow
Test repository method example
Test repository method
example
Unit test
When you get lost in api testing #DarkmiraTour
Integration tests
Integration testing is the phase in software testing
in which individual software modules are combined and
tested as a group.
When to use integration tests
Validation
3rd party integration
Database queries
Verifying two or more modules which have unit tests work as
expected together
Integration test
Step 1: Prepare test data
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
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
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
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
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
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
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
When you get lost in api testing #DarkmiraTour
Integration test
Step 2: trigger repository method and check if data
updated as expected
When you get lost in api testing #DarkmiraTour
Test payment endpoint example
Test payment - mocking 3rd party calls
Test payment - functional test
New approach benefits
More coverage, less bugs
Faster tests
Easier and faster refactoring
Improving code quality
Time saving
Handy testing tools
Infection
Tool for mutation testing in PHP
PHP Stan
PHP Static Analysis Tool
Continuous integration (CI)
Tool for tuning tests on GIT
What we learned along the way
Do I need to write tests first?
Its just one of the approaches
Do I need to have 100% coverage?
100% coverage doesnt guarantee your code is
fully tested and working
What about legacy code?
1. Start replicating bugs with tests before fixing them
2. Test at least most important and critical parts
How do I test some specific code?
Check out the tests of the libraries and framework you
are using
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?
Conclusion
 You should write tests!
 Its important to write Unit, Integration and Functional tests
 If something is hard to test - refactor it
 Check out the tools we mentioned
Thank you!
Questions?
Paula uuk
paula@locastic.com
@paoolala
Backend developer @ Locastic

More Related Content

When you get lost in api testing #DarkmiraTour

  • 1. WHEN YOU GET LOST IN API TESTING Paula uuk
  • 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 PhpUnit
  • 10. The test Http Client in API Platform
  • 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
  • 13. Test creating event with bad data
  • 20. Test get list of events
  • 21. Test get list of events unauthorised
  • 27. Functional tests benefits Less bugs Faster testing Worked perfect on small project
  • 31. 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.
  • 36. Unit tests Catch errors in early stage Very fast execution Easy to write
  • 38. Its not unit tests, its you! Refactor your code using SOLID principles.
  • 44. Integration tests Integration testing is the phase in software testing in which individual software modules are combined and tested as a group.
  • 45. When to use integration tests Validation 3rd party integration Database queries Verifying two or more modules which have unit tests work as expected together
  • 46. Integration test Step 1: Prepare test data
  • 47. 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
  • 48. 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
  • 49. 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
  • 50. 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
  • 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
  • 55. Integration test Step 2: trigger repository method and check if data updated as expected
  • 58. Test payment - mocking 3rd party calls
  • 59. Test payment - functional test
  • 60. New approach benefits More coverage, less bugs Faster tests Easier and faster refactoring Improving code quality Time saving
  • 62. Infection Tool for mutation testing in PHP
  • 63. PHP Stan PHP Static Analysis Tool
  • 64. Continuous integration (CI) Tool for tuning tests on GIT
  • 65. What we learned along the way
  • 66. Do I need to write tests first? Its just one of the approaches
  • 67. Do I need to have 100% coverage? 100% coverage doesnt guarantee your code is fully tested and working
  • 68. What about legacy code? 1. Start replicating bugs with tests before fixing them 2. Test at least most important and critical parts
  • 69. How do I test some specific code? Check out the tests of the libraries and framework you are using
  • 70. 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?
  • 71. Conclusion You should write tests! Its important to write Unit, Integration and Functional tests If something is hard to test - refactor it Check out the tools we mentioned