Should I Test at the GUI Level or the API Level?
Where do you test? Can you isolate functionality? If so, test the isolation most heavily. Then look to see what integrates, and how it integrates.
Join the DZone community and get the full member experience.Join For Free
question: is there a rule of thumb when deciding to test at the gui level or api level? are any rules to help decide when to test at one level over the other?
answer: i don’t think i use a simple rule of thumb — but i will try and explore some of the thought processes i use to make the decision.
when i am trying to decide whether to test at the gui or the api level, i have to figure out:
- what am i trying to test?
- can i isolate the functionality i’m testing to a specific level?
i think the word "isolate" might be the "rule of thumb" you are looking for.
test heavily where you can isolate.
test the integration heavily for the isolated unique integration functionality.
test the integration as heavily as the implementation requires.
test the integration for emergent behavior.
test the overlap between systems, during integration, more lightly.
for example, if the system has "create user" functionality:
- exposed through the gui via an admin interface.
- exposed via an api rest call.
i need to test at both levels because the functionality of ‘create user’ is not isolated to one level.
the questions i have to ask though are ‘how much’ and ‘what’ do i have to test at each level? i may decide not to test as much through the gui if my gui calls the rest api because i can model that as (at least) two communicating systems. a gui system and an api system.
i can heavily test the parts of functionality that are isolated in the api at theapi level, and then lightly test the overlap of the integration between the gui and the api. and i want to test the unique functionality in the gui. e.g. the gui may have ajax calls triggered from gui interaction — i want to test that at the gui level.
if the gui calls a different backend endpoint than the api, then i need to test both routes through to the backend code until i get to the point in the backend system where i encounter shared code — assuming there is shared code in the system used by both the rest api and the gui triggered backend flow.
but at this point, i also need to consider how much coverage we have from unit tests which cover the shared code.
i guess my rule of thumb might look like a list:
- build a model of the system such that you can identify the integration points and the ‘isolated’ shared functionality.
- test isolated functionality at the lowest points you can.
- work back out to higher (or peer) levels of integration and abstraction and consider the system in terms of integrating systems.
- look for unique functionality at the higher (or peer) levels of abstraction, you will need to test them there.
- if you exercise unique functionality in isolation by mocking out the integrating systems, then you might need to look at this from a technical risk perspective and decide if, or how much, you need to exercise it while integrated.
i could also model the above as:
- create multiple overlapping models of the system.
- consider coverage of the multiple models of the system.
- consider coverage of flows through the different models of the system.
- consideration of technical risk relating to the implementation and the environments the system runs on.
p.s. when i explain this, people often map this on to their favorite pyramid model. i don’t use any pyramid models. i prefer to model the system that i’m working with, rather than have a general model. other people prefer pyramid models.
Published at DZone with permission of Alan Richardson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Authorization: Get It Done Right, Get It Done Early
Scaling Site Reliability Engineering (SRE) Teams the Right Way
Replacing Apache Hive, Elasticsearch, and PostgreSQL With Apache Doris
Never Use Credentials in a CI/CD Pipeline Again