HINDSIGHT
LESSONS
ABOUT API
TESTING
By Viktor Slavchev
Siteground
Blog: mrslavchev.com
Twitter: @TheTestingTroll
Agenda
• WTF is an API?
• Why would you automate an API?
• Serious cognitive barriers
• What tests worth automating
• Setup and environment
• The framework
Warning!!! Tools don’t make
software
Nor do they test it…
Bullshit warning!
I commit to:
• Not giving you a “how to”
• No 30 mins introduction
• No “live coding misery”
• No “best practices”
Who am I: Viktor Slavchev
• Software tester, speaker and
teacher in testing
• Blog: mrslavchev.com
• @thetestingtroll
• Insert random brag about my
achievements
• This slide is bullshit
• Nobody reads this far
WTF is a web service?
Imagine a waiter…
• You make an order
(request)
• You get a response (status
code)
• You get what you ordered
(data, result)
HTTP status codes – represented
by HTTP status cats
HTTP status codes – represented
by HTTP status cats
HTTP status codes – represented
by HTTP status cats
HTTP status codes – represented
by HTTP status cats
HTTP status codes – represented
by HTTP status cats
Method correspondence with
CRUD
Why testing via the API?
Why not something that clicks?!
Few (un)obvious reasons to
automate via API
• Clear definitive response
• Close to client
• Good failure visibility
• Easy to test with code
Source: Models of automation
Speaking of pyramids…
The automation iceberg
Setup, tech stack, context
• Restful API written in Php
• ReactJS front-end
• Docker compose – test env
• Codeception – testing tool
• Custom tools to produce data
Before we even think of testing
Few important notes
Cognitive barriers
Few things to have in mind:
• The doc is not the product
• Your coding skills will hold you
back
• The need to explore
• Tests that pass vs. tests that test
What tests are worth automating?
… but first – what bugs do we care
testing for?
• Service returning wrong response code
• Throwing 500
• Returning wrong data
• No data at all
• Interaction problems
What test are worth automating?
• Status code checks
• Structure checks
• Scenario checks
Status code checks
Example:
Status code checks – pros and cons
Pros:
•Fast and easy
• Very definitive
• Work as sanity/smoke
checks
Cons:
•Very shallow level
•GET methods
•They don’t expose
serious problems
Structure checks
Example:
Structure checks – pros and cons
Pros:
•Verify data
• Can be very specific
• Can use regex
(Codecept)
Cons:
•Useful for content only
•Suck with variable data
•Suck with deep
nesting/long responses
Scenario checks
Scenario checks – pros and cons
Pros:
• Closest to client
interaction
• Expose integration
problems
• Can be used as shallow
regression suite
Cons:
•Slow to run/develop
•Require better
abstraction
•Hard to say when it is
too much
Setup of the framework
Make it yourself
Few ideas that worked for me
“Worst” practices
Incorporate OOP principles
• Inheritance
• Reuse
• KISS
• Horizontal inheritance(traits)
One simple inheritance schema
Before method inheritance
Let your framework mature
• Your framework will suck at first
• It will improve incrementally
• It can’t be done in the beginning
Splitting logic of the test and logic of
the app
• Move common actions to base
classes
• Move common prerequisites to
setup methods
• Build inheritance trees
• Reuse rather than repeat
Useful resources
• Hindsight lessons about API testing
• Automating and Testing a REST API – book by
Alan Richardson
• This article by Smart Bear
• Service virtualization by Bas Dijkstra free e-book
• Your brain!!!
Thank you!
Questions?
mrslavchev.com
@TheTestingTroll
LinkedIn
All hail The Testing
Troll!

Hindsight lessons about API testing