SlideShare a Scribd company logo
1 of 51
Download to read offline
API Testing following the
Test Pyramid
Elias Nogueira
@eliasnogueira
Elias Nogueira
Brazilian guy living in the Netherlands.
I (try to) help people and companies to
deliver a high-quality software.
@eliasnogueira
github.com/eliasnogueira
Disclaimer
The Test Pyramid focus will be on the API part,
not related to unit and web tests
Concept
Original Test Pyramid
UI
Tests
Service Tests
Unit Tests
more
isolation
more
integration
faster
slower
Ideal Test Pyramid
Individual APIs
API
API
API
API
API
API
API
API
Individual APIs
API
API
API
API
API
API
API
API
We’ll assume that
each API is
already tested
Unit Test must
be done
Individual APIs
API
API
API
API
API
API
API
API
Functional testing
in each API
Contract and E2E testing
API
API
API
API
API
API
API
API
Contract
&
E2E Testing
Contract and E2E testing
API
API
API
API
API
API
API
API
Contract
&
E2E Testing
Guarantee that
they are
communicating
without problem
Contract and E2E testing
API
API
API
API
API
API
API
API
Contract
&
E2E Testing
Assure that they
can be used
together (e2e)
Recall
Rest-Assured
http://rest-assured.io
Java DSL for simplifying testing of REST based services
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
public class RestAssuredExampleTest {
@Test
public void welcome() {
given().
param("name", "Elias").
when().
post("/register").
then().
body("message", is("Hello Elias"));
}
}
Rest-Assured
http://rest-assured.io
Java DSL for simplifying testing of REST based services.
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
public class RestAssuredExampleTest {
@Test
public void welcome() {
given().
param("name", "Elias").
when().
post("/register").
then().
body("message", is("Hello Elias"));
}
}
import libraries
Rest-Assured
http://rest-assured.io
Java DSL for simplifying testing of REST based services
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
public class RestAssuredExampleTest {
@Test
public void welcome() {
given().
param("name", "Elias").
when().
post("/register").
then().
body("message", is("Hello Elias"));
}
}
request pre-condition
Rest-Assured
http://rest-assured.io
Java DSL for simplifying testing of REST based services
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
public class RestAssuredExampleTest {
@Test
public void welcome() {
given().
param("name", "Elias").
when().
post("/register").
then().
body("message", is("Hello Elias"));
}
}
action (request)
Rest-Assured
http://rest-assured.io
Java DSL for simplifying testing of REST based services
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
public class RestAssuredExampleTest {
@Test
public void welcome() {
given().
param("name", "Elias").
when().
post("/register").
then().
body("message", is("Hello Elias"));
}
}
assert the response body
SUT – System Under Test
SUT – System Under Test | Front-end
We should inform a CPF
* Its a Brazilian social security number
If a restriction is found,
show a message
SUT – System Under Test | Front-end
Fill in loan information
SUT – System Under Test | Front-end
CRUD operations
Pipeline
API
Pipeline
Health Check
Contract
Functional
Acceptance
Verify if the endpoint is alive
Assert that the specs haven’t changed
Assert all the criteria from the requirement +
happy/sad paths
Assert that the most important user
scenarios still works
Health Check
Pipeline (pyramid view)
Contract
Acceptance
Functional
Pipeline
GitLab CI
https://gitlab.com/elias.nogueira/test-combined-credit-api/pipelines
Enabling the Pipeline
Create a way to, easily, filter the tests by their focus/function/level

@Test(groups = "functional")
public void testNgGroup() {
// test goes here
}
@Test
@Tag("functional")
void junitTag() {
// test goes here
}
XML file Suite Class Suite
[how to] Enabling the Pipeline
In the example
1. Create a strategy to filter your tests
○ Example of using groups in a test [1]
○ Example of a test suite [2]
2. Create a strategy to enable the test be executed by command line
○ pom.xml showing show to run a suite [3]
3. Create a pipeline as a code
○ .gitlab-ci.yml [4]
[1] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/src/test/java/com/eliasnogueira/credit/restrictions/RestrictionsFunctionalTest.java
[2] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/src/test/resources/suites/e2e.xml
[3] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/pom.xml#L101
[4] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/.gitlab-ci.yml
API
health-check
Health Check
Contract
Functional
Acceptance
Verify it the endpoint is alive
Assert that the specs haven’t changed
Assert all the criteria from the requirement +
happy/sad paths
Assert that the most important user
scenarios still works
heath-check
Verify if the API is available
If there’s no way to verify by a monitoring strategy we can make a request and
validate the status code
@Test(groups = "health")
public void healthCheckViaActuator() {
basePath = "/actuator";
when().
get("/health").
then().
statusCode(200).
body("status", is("UP"));
}
via monitoring
@Test(groups = "health")
public void healthCheckViaAPI() {
given().
pathParam("cpf", "81016654049").
when().
get("/restrictions/{cpf}").
then().
statusCode(200);
}
via API
[how to] health-check
1. Identify if you have a health check endpoint
○ If true find out, beyond the endpoint, any return status
○ If false, make a request to the easiest endpoint (GET?)
[how to] health-check
In the example
● In the local project hit http://localhost:8088/actuator/health
○ You’ll see the health status
● See the implemented tests on:
○ CreditHealthCheckTest [1]
● Items to pay attention:
○ It’s getting the health context from the file because it does not have the
/api/v1 to hit the actuator endpoint
[1] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/src/test/java/com/eliasnogueira/credit/general/CreditHealthCheckTest.java
API
contract
Health Check
Contract
Functional
Acceptance
Verify it the endpoint is alive
Assert that the specs haven’t changed
Assert all the criteria from the requirement +
happy/sad paths
Assert that the most important user
scenarios still works
● It’s the name given to the pact between producer and
consumer
● Ensures that API changes do not invalidate consumption:
● path
● parameters
● sending data (request)
● return data (response body)
● json-schema is a contract that defines the expected data,
types and formats of each field in the response
contract
{
"name": "Elias",
"age": 37
}
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
}
},
"required": [
"name",
"age"
],
"additionalProperties": false
}
json-schema
{
"name": "Elias",
"age": 37
}
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
}
},
"required": [
"name",
"age"
],
"additionalProperties": false
}
json-schema has the attribute
name and data type
json-schema
{
"name": "Elias",
"age": 37
}
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"name": {
"type": "string"
},
”age": {
"type": "integer"
}
},
"required": [
"name",
"age"
],
"additionalProperties": false
}
both attributes must be present
json-schema
{
"name": "Elias",
"age": 37
}
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
}
},
"required": [
”name",
”age"
],
"additionalProperties": false
}
json-schema
no other attributes are
allowed
[how to] contract
1. Create schema files
○ We can use online tools to create if it’s not available
2. Create a test making a request and validating the json schema
○ Add json-schema-validator library
○ Statically import the matcher
○ Use the matcher against json schema file
[how to] contract
In the example
● See the implemented tests on:
○ RestrictionsContractTest.java [1]
○ SimulationsContractTest.java [2]
● Items to pay attention:
○ The validation is done by the matcher matchesJsonSchemaInClasspath,
checking the response body with the json schema file
○ If you want to see different API enable the test contractOnV2 on
RestrictionsContractTests
[1] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/src/test/java/com/eliasnogueira/credit/restrictions/RestrictionsContractTest.java
[2] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/src/test/java/com/eliasnogueira/credit/simulations/SimulationsContractTest.java
API
functional
Health Check
Contract
Functional
Acceptance
Verify it the endpoint is alive
Assert that the specs haven’t changed
Assert all the criteria from the requirement +
happy/sad paths
Assert that the most important user
scenarios still works
functional
Validate positive and negative scenarios (happy and sad path)
@Test(groups = {"functional"})
public void existentSocialSecirityNumber() {
given().
pathParam("cpf", "66414919004").
when().
get("/simulations/{cpf}").
then().
statusCode(200).
body(
"id", equalTo(1),
"name", equalTo("Oleksander"),
"cpf", equalTo("66414919004"),
"email", equalTo("oleksander@gmail.com"),
"amount", equalTo(11000f),
"installments", equalTo(3),
"insurance", equalTo(true)
);
}
data validation
[how to] functional
1. Create the tests avoiding repetitions
○ Use Test Data Factories and Data Driver in your advantage
2. Apply the strategy you like
○ All tests in the same class
○ One class per test
[how to] functional
In the example
● See the implemented tests on:
○ RestrictionsFunctionalTest.java [1]
○ SimulationsFunctionalTest.java [2]
● Items to pay attention:
○ The tests are using a soft assertion for the body validation
○ MessageFormat class is used to properly concatenate String
○ SimulationsFunctionalTests is using Test Data Factory, Builders for the
model object and Data Driven
[1] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/src/test/java/com/eliasnogueira/credit/restrictions/RestrictionsFunctionalTest.java
[2] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/src/test/java/com/eliasnogueira/credit/simulations/SimulationsFunctionalTest.java
Assert all the criteria from the requirement +
happy/sad paths
API
acceptance
Health Check
Contract
Functional
Acceptance
Verify it the endpoint is alive
Assert that the specs haven’t changed
Assert that the most important user
scenarios still works
Testing from the user's perspective
● Access the page and make the restriction search
by the CPF
● Insert a credit simulation
acceptance (e2e)
@Test(groups = "e2e")
public void completeSimulation() {
new RestrictionsClient().queryRestrictionAndReturnNotFound();
new SimulationsClient().submitSuccessfulSimulation();
}
[how to] acceptance (e2e)
1. Create the tests avoiding repetitions
○ Use Request and Response Specifications
○ Try to break out in reusable methods common actions
2. Do the User Journey
○ The few most important user journeys must run before the
functional tests
https://martinfowler.com/articles/practical-test-pyramid.html#RestApiEnd-to-endTest
[how to] functional
In the example
● See the implemented tests on:
○ FullSimulationE2ETest.java [1]
● Items to pay attention:
○ There’s a utility class with the suffix Client in order to have an easy way
to make request and response calls
○ The request and response calls are Specification Re-use [2] created to
reuse these actions
○ See also the packages client [3] and specs [4]
[1] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/src/test/java/com/eliasnogueira/credit/e2e/FullSimulationE2ETest.java
[2] https://github.com/rest-assured/rest-assured/wiki/usage#specification-re-use
[3] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/tree/master/src/main/java/com/eliasnogueira/credit/client
[4] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/tree/master/src/main/java/com/eliasnogueira/credit/specs
For free
Work in progress in 2 languages
Access Leanpub and subscribe
Thank you!
@eliasnogueira
github.com/eliasnogueira

More Related Content

What's hot

JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first stepsRenato Primavera
 
N Unit Presentation
N Unit PresentationN Unit Presentation
N Unit Presentationpriya_trivedi
 
Unit Testing Using N Unit
Unit Testing Using N UnitUnit Testing Using N Unit
Unit Testing Using N UnitGaurav Arora
 
2015-StarWest presentation on REST-assured
2015-StarWest presentation on REST-assured2015-StarWest presentation on REST-assured
2015-StarWest presentation on REST-assuredEing Ong
 
Test Automation Framework Designs
Test Automation Framework DesignsTest Automation Framework Designs
Test Automation Framework DesignsSauce Labs
 
Automação de Teste para REST, Web e Mobile
Automação de Teste para REST, Web e MobileAutomação de Teste para REST, Web e Mobile
Automação de Teste para REST, Web e MobileElias Nogueira
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testingAdam Stephensen
 
Confie no seu pipeline: Teste automaticamente um aplicativo Java de ponta a p...
Confie no seu pipeline: Teste automaticamente um aplicativo Java de ponta a p...Confie no seu pipeline: Teste automaticamente um aplicativo Java de ponta a p...
Confie no seu pipeline: Teste automaticamente um aplicativo Java de ponta a p...Elias Nogueira
 
API Test Automation
API Test Automation API Test Automation
API Test Automation SQALab
 
Automation Testing With Appium
Automation Testing With AppiumAutomation Testing With Appium
Automation Testing With AppiumKnoldus Inc.
 
RESTful API Testing using Postman, Newman, and Jenkins
RESTful API Testing using Postman, Newman, and JenkinsRESTful API Testing using Postman, Newman, and Jenkins
RESTful API Testing using Postman, Newman, and JenkinsQASymphony
 
Introduction to Automation Testing
Introduction to Automation TestingIntroduction to Automation Testing
Introduction to Automation TestingArchana Krushnan
 
Solucionando a Teoria do Caos com Cypress.io
Solucionando a Teoria do Caos com Cypress.ioSolucionando a Teoria do Caos com Cypress.io
Solucionando a Teoria do Caos com Cypress.ioPatrick Monteiro
 
Test Design and Automation for REST API
Test Design and Automation for REST APITest Design and Automation for REST API
Test Design and Automation for REST APIIvan Katunou
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesDerek Smith
 

What's hot (20)

JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
Workshop unit test
Workshop   unit testWorkshop   unit test
Workshop unit test
 
N Unit Presentation
N Unit PresentationN Unit Presentation
N Unit Presentation
 
Rest assured
Rest assuredRest assured
Rest assured
 
Unit Testing Using N Unit
Unit Testing Using N UnitUnit Testing Using N Unit
Unit Testing Using N Unit
 
2015-StarWest presentation on REST-assured
2015-StarWest presentation on REST-assured2015-StarWest presentation on REST-assured
2015-StarWest presentation on REST-assured
 
Test Automation Framework Designs
Test Automation Framework DesignsTest Automation Framework Designs
Test Automation Framework Designs
 
Automação de Teste para REST, Web e Mobile
Automação de Teste para REST, Web e MobileAutomação de Teste para REST, Web e Mobile
Automação de Teste para REST, Web e Mobile
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testing
 
Confie no seu pipeline: Teste automaticamente um aplicativo Java de ponta a p...
Confie no seu pipeline: Teste automaticamente um aplicativo Java de ponta a p...Confie no seu pipeline: Teste automaticamente um aplicativo Java de ponta a p...
Confie no seu pipeline: Teste automaticamente um aplicativo Java de ponta a p...
 
API Test Automation
API Test Automation API Test Automation
API Test Automation
 
Mockito
MockitoMockito
Mockito
 
Api Testing
Api TestingApi Testing
Api Testing
 
Automation Testing With Appium
Automation Testing With AppiumAutomation Testing With Appium
Automation Testing With Appium
 
RESTful API Testing using Postman, Newman, and Jenkins
RESTful API Testing using Postman, Newman, and JenkinsRESTful API Testing using Postman, Newman, and Jenkins
RESTful API Testing using Postman, Newman, and Jenkins
 
Introduction to Automation Testing
Introduction to Automation TestingIntroduction to Automation Testing
Introduction to Automation Testing
 
Solucionando a Teoria do Caos com Cypress.io
Solucionando a Teoria do Caos com Cypress.ioSolucionando a Teoria do Caos com Cypress.io
Solucionando a Teoria do Caos com Cypress.io
 
Test Design and Automation for REST API
Test Design and Automation for REST APITest Design and Automation for REST API
Test Design and Automation for REST API
 
testng
testngtestng
testng
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 

Similar to API Testing following the Test Pyramid

How_to_create_modular_microservice_test_projects.pdf
How_to_create_modular_microservice_test_projects.pdfHow_to_create_modular_microservice_test_projects.pdf
How_to_create_modular_microservice_test_projects.pdfskimorod
 
Web Services and Introduction of SOAPUI
Web Services and Introduction of SOAPUIWeb Services and Introduction of SOAPUI
Web Services and Introduction of SOAPUIDinesh Kaushik
 
Automation testing
Automation testingAutomation testing
Automation testingTomy Rhymond
 
Performance testing and j meter
Performance testing and j meterPerformance testing and j meter
Performance testing and j meterPurna Chandar
 
Automated Developer Testing: Achievements and Challenges
Automated Developer Testing: Achievements and ChallengesAutomated Developer Testing: Achievements and Challenges
Automated Developer Testing: Achievements and ChallengesTao Xie
 
Acceptance tests
Acceptance testsAcceptance tests
Acceptance testsDragan Tomic
 
CI/CD Pipeline Security: Advanced Continuous Delivery Best Practices: Securit...
CI/CD Pipeline Security: Advanced Continuous Delivery Best Practices: Securit...CI/CD Pipeline Security: Advanced Continuous Delivery Best Practices: Securit...
CI/CD Pipeline Security: Advanced Continuous Delivery Best Practices: Securit...Amazon Web Services
 
Testing in Agile Development
Testing in Agile DevelopmentTesting in Agile Development
Testing in Agile DevelopmentHariprakash Agrawal
 
Automated Acceptance Tests & Tool choice
Automated Acceptance Tests & Tool choiceAutomated Acceptance Tests & Tool choice
Automated Acceptance Tests & Tool choicetoddbr
 
DevOps CI Automation Continuous Integration
DevOps CI Automation Continuous IntegrationDevOps CI Automation Continuous Integration
DevOps CI Automation Continuous IntegrationIRJET Journal
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power pointjustmeanscsr
 
Performancetestingjmeter 121109061704-phpapp02
Performancetestingjmeter 121109061704-phpapp02Performancetestingjmeter 121109061704-phpapp02
Performancetestingjmeter 121109061704-phpapp02Shivakumara .
 
Performance testing jmeter
Performance testing jmeterPerformance testing jmeter
Performance testing jmeterBhojan Rajan
 
Getting started with_testcomplete
Getting started with_testcompleteGetting started with_testcomplete
Getting started with_testcompleteankit.das
 
WiKi Based Automation Testing: Fitness & DevOps
WiKi Based Automation Testing: Fitness & DevOpsWiKi Based Automation Testing: Fitness & DevOps
WiKi Based Automation Testing: Fitness & DevOpsAgile Testing Alliance
 
Mykola Kovsh - Functional API automation with Jmeter
Mykola Kovsh - Functional API automation with JmeterMykola Kovsh - Functional API automation with Jmeter
Mykola Kovsh - Functional API automation with JmeterIevgenii Katsan
 
About Qtp 92
About Qtp 92About Qtp 92
About Qtp 92techgajanan
 
About Qtp_1 92
About Qtp_1 92About Qtp_1 92
About Qtp_1 92techgajanan
 

Similar to API Testing following the Test Pyramid (20)

How_to_create_modular_microservice_test_projects.pdf
How_to_create_modular_microservice_test_projects.pdfHow_to_create_modular_microservice_test_projects.pdf
How_to_create_modular_microservice_test_projects.pdf
 
Web Services and Introduction of SOAPUI
Web Services and Introduction of SOAPUIWeb Services and Introduction of SOAPUI
Web Services and Introduction of SOAPUI
 
Automation testing
Automation testingAutomation testing
Automation testing
 
Performance testing and j meter
Performance testing and j meterPerformance testing and j meter
Performance testing and j meter
 
Automated Developer Testing: Achievements and Challenges
Automated Developer Testing: Achievements and ChallengesAutomated Developer Testing: Achievements and Challenges
Automated Developer Testing: Achievements and Challenges
 
Acceptance tests
Acceptance testsAcceptance tests
Acceptance tests
 
CI/CD Pipeline Security: Advanced Continuous Delivery Best Practices: Securit...
CI/CD Pipeline Security: Advanced Continuous Delivery Best Practices: Securit...CI/CD Pipeline Security: Advanced Continuous Delivery Best Practices: Securit...
CI/CD Pipeline Security: Advanced Continuous Delivery Best Practices: Securit...
 
Testing in Agile Development
Testing in Agile DevelopmentTesting in Agile Development
Testing in Agile Development
 
Integration testing - A&BP CC
Integration testing - A&BP CCIntegration testing - A&BP CC
Integration testing - A&BP CC
 
Automated Acceptance Tests & Tool choice
Automated Acceptance Tests & Tool choiceAutomated Acceptance Tests & Tool choice
Automated Acceptance Tests & Tool choice
 
DevOps CI Automation Continuous Integration
DevOps CI Automation Continuous IntegrationDevOps CI Automation Continuous Integration
DevOps CI Automation Continuous Integration
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
 
Performancetestingjmeter 121109061704-phpapp02
Performancetestingjmeter 121109061704-phpapp02Performancetestingjmeter 121109061704-phpapp02
Performancetestingjmeter 121109061704-phpapp02
 
Performance testing jmeter
Performance testing jmeterPerformance testing jmeter
Performance testing jmeter
 
Getting started with_testcomplete
Getting started with_testcompleteGetting started with_testcomplete
Getting started with_testcomplete
 
WiKi Based Automation Testing: Fitness & DevOps
WiKi Based Automation Testing: Fitness & DevOpsWiKi Based Automation Testing: Fitness & DevOps
WiKi Based Automation Testing: Fitness & DevOps
 
Mykola Kovsh - Functional API automation with Jmeter
Mykola Kovsh - Functional API automation with JmeterMykola Kovsh - Functional API automation with Jmeter
Mykola Kovsh - Functional API automation with Jmeter
 
About Qtp 92
About Qtp 92About Qtp 92
About Qtp 92
 
About QTP 9.2
About QTP 9.2About QTP 9.2
About QTP 9.2
 
About Qtp_1 92
About Qtp_1 92About Qtp_1 92
About Qtp_1 92
 

More from Elias Nogueira

De a mĂĄxima cobertura nos seus testes de API
De a mĂĄxima cobertura nos seus testes de APIDe a mĂĄxima cobertura nos seus testes de API
De a mĂĄxima cobertura nos seus testes de APIElias Nogueira
 
Usando containers com auto-escala de testes
Usando containers com auto-escala de testesUsando containers com auto-escala de testes
Usando containers com auto-escala de testesElias Nogueira
 
Coach por ImersĂŁo - Buscando a excelĂȘncia tĂ©cnica com o time
Coach por ImersĂŁo - Buscando a excelĂȘncia tĂ©cnica com o timeCoach por ImersĂŁo - Buscando a excelĂȘncia tĂ©cnica com o time
Coach por ImersĂŁo - Buscando a excelĂȘncia tĂ©cnica com o timeElias Nogueira
 
O Agile Coach pode (e muitas vezes deve) ser técnico
O Agile Coach pode (e muitas vezes deve) ser técnicoO Agile Coach pode (e muitas vezes deve) ser técnico
O Agile Coach pode (e muitas vezes deve) ser técnicoElias Nogueira
 
Paralelize seus testes web e mobile para ter feedbacks mais rĂĄpidos
Paralelize seus testes web e mobile para ter feedbacks mais rĂĄpidosParalelize seus testes web e mobile para ter feedbacks mais rĂĄpidos
Paralelize seus testes web e mobile para ter feedbacks mais rĂĄpidosElias Nogueira
 
Como 4 Agile Coaches trabalham em uma Transformação Ágil
Como 4 Agile Coaches trabalham em uma Transformação Ágil Como 4 Agile Coaches trabalham em uma Transformação Ágil
Como 4 Agile Coaches trabalham em uma Transformação Ágil Elias Nogueira
 
Papel do QA na Transformação Ágil
Papel do QA na Transformação ÁgilPapel do QA na Transformação Ágil
Papel do QA na Transformação ÁgilElias Nogueira
 
BDD não é automação de teste - Scrum Gathering
BDD não é automação de teste - Scrum GatheringBDD não é automação de teste - Scrum Gathering
BDD não é automação de teste - Scrum GatheringElias Nogueira
 
Como criar e executar testes paralelos web usando Selenium e containers
Como criar e executar testes paralelos web usando Selenium e containersComo criar e executar testes paralelos web usando Selenium e containers
Como criar e executar testes paralelos web usando Selenium e containersElias Nogueira
 
Improve Yourself -- Learn the Skills, Join the Community - Tests
Improve Yourself -- Learn the Skills, Join the Community - TestsImprove Yourself -- Learn the Skills, Join the Community - Tests
Improve Yourself -- Learn the Skills, Join the Community - TestsElias Nogueira
 
BDD não é Automação de Testes
BDD não é Automação de TestesBDD não é Automação de Testes
BDD não é Automação de TestesElias Nogueira
 
Criando uma grid para execução de testes paralelo com Appium
Criando uma grid para execução de testes paralelo com AppiumCriando uma grid para execução de testes paralelo com Appium
Criando uma grid para execução de testes paralelo com AppiumElias Nogueira
 
Como ter sucesso ministrando uma palestra técnica
Como ter sucesso ministrando uma palestra técnicaComo ter sucesso ministrando uma palestra técnica
Como ter sucesso ministrando uma palestra técnicaElias Nogueira
 
Quais sĂŁo os steps de que deve conter na sua pipeline?
Quais sĂŁo os steps de que deve conter na sua pipeline?Quais sĂŁo os steps de que deve conter na sua pipeline?
Quais sĂŁo os steps de que deve conter na sua pipeline?Elias Nogueira
 
Tem que testar mesmo?
Tem que testar mesmo?Tem que testar mesmo?
Tem que testar mesmo?Elias Nogueira
 
Testes em todos os niveis de planejamento
Testes em todos os niveis de planejamentoTestes em todos os niveis de planejamento
Testes em todos os niveis de planejamentoElias Nogueira
 
Coaching the Agile Coach
Coaching the Agile CoachCoaching the Agile Coach
Coaching the Agile CoachElias Nogueira
 
O que Ă© um Agile Coach
O que Ă© um Agile CoachO que Ă© um Agile Coach
O que Ă© um Agile CoachElias Nogueira
 
Criando uma grid para execução de teste automatizado funcional e e2e
Criando uma grid para execução de teste automatizado funcional e e2eCriando uma grid para execução de teste automatizado funcional e e2e
Criando uma grid para execução de teste automatizado funcional e e2eElias Nogueira
 
A importancia de testes em todos os aspectos
A importancia de testes em todos os aspectosA importancia de testes em todos os aspectos
A importancia de testes em todos os aspectosElias Nogueira
 

More from Elias Nogueira (20)

De a mĂĄxima cobertura nos seus testes de API
De a mĂĄxima cobertura nos seus testes de APIDe a mĂĄxima cobertura nos seus testes de API
De a mĂĄxima cobertura nos seus testes de API
 
Usando containers com auto-escala de testes
Usando containers com auto-escala de testesUsando containers com auto-escala de testes
Usando containers com auto-escala de testes
 
Coach por ImersĂŁo - Buscando a excelĂȘncia tĂ©cnica com o time
Coach por ImersĂŁo - Buscando a excelĂȘncia tĂ©cnica com o timeCoach por ImersĂŁo - Buscando a excelĂȘncia tĂ©cnica com o time
Coach por ImersĂŁo - Buscando a excelĂȘncia tĂ©cnica com o time
 
O Agile Coach pode (e muitas vezes deve) ser técnico
O Agile Coach pode (e muitas vezes deve) ser técnicoO Agile Coach pode (e muitas vezes deve) ser técnico
O Agile Coach pode (e muitas vezes deve) ser técnico
 
Paralelize seus testes web e mobile para ter feedbacks mais rĂĄpidos
Paralelize seus testes web e mobile para ter feedbacks mais rĂĄpidosParalelize seus testes web e mobile para ter feedbacks mais rĂĄpidos
Paralelize seus testes web e mobile para ter feedbacks mais rĂĄpidos
 
Como 4 Agile Coaches trabalham em uma Transformação Ágil
Como 4 Agile Coaches trabalham em uma Transformação Ágil Como 4 Agile Coaches trabalham em uma Transformação Ágil
Como 4 Agile Coaches trabalham em uma Transformação Ágil
 
Papel do QA na Transformação Ágil
Papel do QA na Transformação ÁgilPapel do QA na Transformação Ágil
Papel do QA na Transformação Ágil
 
BDD não é automação de teste - Scrum Gathering
BDD não é automação de teste - Scrum GatheringBDD não é automação de teste - Scrum Gathering
BDD não é automação de teste - Scrum Gathering
 
Como criar e executar testes paralelos web usando Selenium e containers
Como criar e executar testes paralelos web usando Selenium e containersComo criar e executar testes paralelos web usando Selenium e containers
Como criar e executar testes paralelos web usando Selenium e containers
 
Improve Yourself -- Learn the Skills, Join the Community - Tests
Improve Yourself -- Learn the Skills, Join the Community - TestsImprove Yourself -- Learn the Skills, Join the Community - Tests
Improve Yourself -- Learn the Skills, Join the Community - Tests
 
BDD não é Automação de Testes
BDD não é Automação de TestesBDD não é Automação de Testes
BDD não é Automação de Testes
 
Criando uma grid para execução de testes paralelo com Appium
Criando uma grid para execução de testes paralelo com AppiumCriando uma grid para execução de testes paralelo com Appium
Criando uma grid para execução de testes paralelo com Appium
 
Como ter sucesso ministrando uma palestra técnica
Como ter sucesso ministrando uma palestra técnicaComo ter sucesso ministrando uma palestra técnica
Como ter sucesso ministrando uma palestra técnica
 
Quais sĂŁo os steps de que deve conter na sua pipeline?
Quais sĂŁo os steps de que deve conter na sua pipeline?Quais sĂŁo os steps de que deve conter na sua pipeline?
Quais sĂŁo os steps de que deve conter na sua pipeline?
 
Tem que testar mesmo?
Tem que testar mesmo?Tem que testar mesmo?
Tem que testar mesmo?
 
Testes em todos os niveis de planejamento
Testes em todos os niveis de planejamentoTestes em todos os niveis de planejamento
Testes em todos os niveis de planejamento
 
Coaching the Agile Coach
Coaching the Agile CoachCoaching the Agile Coach
Coaching the Agile Coach
 
O que Ă© um Agile Coach
O que Ă© um Agile CoachO que Ă© um Agile Coach
O que Ă© um Agile Coach
 
Criando uma grid para execução de teste automatizado funcional e e2e
Criando uma grid para execução de teste automatizado funcional e e2eCriando uma grid para execução de teste automatizado funcional e e2e
Criando uma grid para execução de teste automatizado funcional e e2e
 
A importancia de testes em todos os aspectos
A importancia de testes em todos os aspectosA importancia de testes em todos os aspectos
A importancia de testes em todos os aspectos
 

Recently uploaded

Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....rightmanforbloodline
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaWSO2
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vĂĄzquez
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringWSO2
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 

Recently uploaded (20)

Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 

API Testing following the Test Pyramid

  • 1. API Testing following the Test Pyramid Elias Nogueira @eliasnogueira
  • 2. Elias Nogueira Brazilian guy living in the Netherlands. I (try to) help people and companies to deliver a high-quality software. @eliasnogueira github.com/eliasnogueira
  • 3. Disclaimer The Test Pyramid focus will be on the API part, not related to unit and web tests
  • 5. Original Test Pyramid UI Tests Service Tests Unit Tests more isolation more integration faster slower
  • 8. Individual APIs API API API API API API API API We’ll assume that each API is already tested Unit Test must be done
  • 10. Contract and E2E testing API API API API API API API API Contract & E2E Testing
  • 11. Contract and E2E testing API API API API API API API API Contract & E2E Testing Guarantee that they are communicating without problem
  • 12. Contract and E2E testing API API API API API API API API Contract & E2E Testing Assure that they can be used together (e2e)
  • 14. Rest-Assured http://rest-assured.io Java DSL for simplifying testing of REST based services import static io.restassured.RestAssured.*; import static org.hamcrest.Matchers.*; public class RestAssuredExampleTest { @Test public void welcome() { given(). param("name", "Elias"). when(). post("/register"). then(). body("message", is("Hello Elias")); } }
  • 15. Rest-Assured http://rest-assured.io Java DSL for simplifying testing of REST based services. import static io.restassured.RestAssured.*; import static org.hamcrest.Matchers.*; public class RestAssuredExampleTest { @Test public void welcome() { given(). param("name", "Elias"). when(). post("/register"). then(). body("message", is("Hello Elias")); } } import libraries
  • 16. Rest-Assured http://rest-assured.io Java DSL for simplifying testing of REST based services import static io.restassured.RestAssured.*; import static org.hamcrest.Matchers.*; public class RestAssuredExampleTest { @Test public void welcome() { given(). param("name", "Elias"). when(). post("/register"). then(). body("message", is("Hello Elias")); } } request pre-condition
  • 17. Rest-Assured http://rest-assured.io Java DSL for simplifying testing of REST based services import static io.restassured.RestAssured.*; import static org.hamcrest.Matchers.*; public class RestAssuredExampleTest { @Test public void welcome() { given(). param("name", "Elias"). when(). post("/register"). then(). body("message", is("Hello Elias")); } } action (request)
  • 18. Rest-Assured http://rest-assured.io Java DSL for simplifying testing of REST based services import static io.restassured.RestAssured.*; import static org.hamcrest.Matchers.*; public class RestAssuredExampleTest { @Test public void welcome() { given(). param("name", "Elias"). when(). post("/register"). then(). body("message", is("Hello Elias")); } } assert the response body
  • 19. SUT – System Under Test
  • 20. SUT – System Under Test | Front-end We should inform a CPF * Its a Brazilian social security number If a restriction is found, show a message
  • 21. SUT – System Under Test | Front-end Fill in loan information
  • 22. SUT – System Under Test | Front-end CRUD operations
  • 24. API Pipeline Health Check Contract Functional Acceptance Verify if the endpoint is alive Assert that the specs haven’t changed Assert all the criteria from the requirement + happy/sad paths Assert that the most important user scenarios still works
  • 25. Health Check Pipeline (pyramid view) Contract Acceptance Functional
  • 27. Enabling the Pipeline Create a way to, easily, filter the tests by their focus/function/level
 @Test(groups = "functional") public void testNgGroup() { // test goes here } @Test @Tag("functional") void junitTag() { // test goes here } XML file Suite Class Suite
  • 28. [how to] Enabling the Pipeline In the example 1. Create a strategy to filter your tests ○ Example of using groups in a test [1] ○ Example of a test suite [2] 2. Create a strategy to enable the test be executed by command line ○ pom.xml showing show to run a suite [3] 3. Create a pipeline as a code ○ .gitlab-ci.yml [4] [1] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/src/test/java/com/eliasnogueira/credit/restrictions/RestrictionsFunctionalTest.java [2] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/src/test/resources/suites/e2e.xml [3] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/pom.xml#L101 [4] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/.gitlab-ci.yml
  • 29. API health-check Health Check Contract Functional Acceptance Verify it the endpoint is alive Assert that the specs haven’t changed Assert all the criteria from the requirement + happy/sad paths Assert that the most important user scenarios still works
  • 30. heath-check Verify if the API is available If there’s no way to verify by a monitoring strategy we can make a request and validate the status code @Test(groups = "health") public void healthCheckViaActuator() { basePath = "/actuator"; when(). get("/health"). then(). statusCode(200). body("status", is("UP")); } via monitoring @Test(groups = "health") public void healthCheckViaAPI() { given(). pathParam("cpf", "81016654049"). when(). get("/restrictions/{cpf}"). then(). statusCode(200); } via API
  • 31. [how to] health-check 1. Identify if you have a health check endpoint ○ If true find out, beyond the endpoint, any return status ○ If false, make a request to the easiest endpoint (GET?)
  • 32. [how to] health-check In the example ● In the local project hit http://localhost:8088/actuator/health ○ You’ll see the health status ● See the implemented tests on: ○ CreditHealthCheckTest [1] ● Items to pay attention: ○ It’s getting the health context from the file because it does not have the /api/v1 to hit the actuator endpoint [1] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/src/test/java/com/eliasnogueira/credit/general/CreditHealthCheckTest.java
  • 33. API contract Health Check Contract Functional Acceptance Verify it the endpoint is alive Assert that the specs haven’t changed Assert all the criteria from the requirement + happy/sad paths Assert that the most important user scenarios still works
  • 34. ● It’s the name given to the pact between producer and consumer ● Ensures that API changes do not invalidate consumption: ● path ● parameters ● sending data (request) ● return data (response body) ● json-schema is a contract that defines the expected data, types and formats of each field in the response contract
  • 35. { "name": "Elias", "age": 37 } { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "properties": { "name": { "type": "string" }, "age": { "type": "integer" } }, "required": [ "name", "age" ], "additionalProperties": false } json-schema
  • 36. { "name": "Elias", "age": 37 } { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "properties": { "name": { "type": "string" }, "age": { "type": "integer" } }, "required": [ "name", "age" ], "additionalProperties": false } json-schema has the attribute name and data type json-schema
  • 37. { "name": "Elias", "age": 37 } { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "properties": { "name": { "type": "string" }, ”age": { "type": "integer" } }, "required": [ "name", "age" ], "additionalProperties": false } both attributes must be present json-schema
  • 38. { "name": "Elias", "age": 37 } { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "properties": { "name": { "type": "string" }, "age": { "type": "integer" } }, "required": [ ”name", ”age" ], "additionalProperties": false } json-schema no other attributes are allowed
  • 39. [how to] contract 1. Create schema files ○ We can use online tools to create if it’s not available 2. Create a test making a request and validating the json schema ○ Add json-schema-validator library ○ Statically import the matcher ○ Use the matcher against json schema file
  • 40. [how to] contract In the example ● See the implemented tests on: ○ RestrictionsContractTest.java [1] ○ SimulationsContractTest.java [2] ● Items to pay attention: ○ The validation is done by the matcher matchesJsonSchemaInClasspath, checking the response body with the json schema file ○ If you want to see different API enable the test contractOnV2 on RestrictionsContractTests [1] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/src/test/java/com/eliasnogueira/credit/restrictions/RestrictionsContractTest.java [2] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/src/test/java/com/eliasnogueira/credit/simulations/SimulationsContractTest.java
  • 41. API functional Health Check Contract Functional Acceptance Verify it the endpoint is alive Assert that the specs haven’t changed Assert all the criteria from the requirement + happy/sad paths Assert that the most important user scenarios still works
  • 42. functional Validate positive and negative scenarios (happy and sad path) @Test(groups = {"functional"}) public void existentSocialSecirityNumber() { given(). pathParam("cpf", "66414919004"). when(). get("/simulations/{cpf}"). then(). statusCode(200). body( "id", equalTo(1), "name", equalTo("Oleksander"), "cpf", equalTo("66414919004"), "email", equalTo("oleksander@gmail.com"), "amount", equalTo(11000f), "installments", equalTo(3), "insurance", equalTo(true) ); } data validation
  • 43. [how to] functional 1. Create the tests avoiding repetitions ○ Use Test Data Factories and Data Driver in your advantage 2. Apply the strategy you like ○ All tests in the same class ○ One class per test
  • 44. [how to] functional In the example ● See the implemented tests on: ○ RestrictionsFunctionalTest.java [1] ○ SimulationsFunctionalTest.java [2] ● Items to pay attention: ○ The tests are using a soft assertion for the body validation ○ MessageFormat class is used to properly concatenate String ○ SimulationsFunctionalTests is using Test Data Factory, Builders for the model object and Data Driven [1] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/src/test/java/com/eliasnogueira/credit/restrictions/RestrictionsFunctionalTest.java [2] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/src/test/java/com/eliasnogueira/credit/simulations/SimulationsFunctionalTest.java
  • 45. Assert all the criteria from the requirement + happy/sad paths API acceptance Health Check Contract Functional Acceptance Verify it the endpoint is alive Assert that the specs haven’t changed Assert that the most important user scenarios still works
  • 46. Testing from the user's perspective ● Access the page and make the restriction search by the CPF ● Insert a credit simulation
  • 47. acceptance (e2e) @Test(groups = "e2e") public void completeSimulation() { new RestrictionsClient().queryRestrictionAndReturnNotFound(); new SimulationsClient().submitSuccessfulSimulation(); }
  • 48. [how to] acceptance (e2e) 1. Create the tests avoiding repetitions ○ Use Request and Response Specifications ○ Try to break out in reusable methods common actions 2. Do the User Journey ○ The few most important user journeys must run before the functional tests https://martinfowler.com/articles/practical-test-pyramid.html#RestApiEnd-to-endTest
  • 49. [how to] functional In the example ● See the implemented tests on: ○ FullSimulationE2ETest.java [1] ● Items to pay attention: ○ There’s a utility class with the suffix Client in order to have an easy way to make request and response calls ○ The request and response calls are Specification Re-use [2] created to reuse these actions ○ See also the packages client [3] and specs [4] [1] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/blob/master/src/test/java/com/eliasnogueira/credit/e2e/FullSimulationE2ETest.java [2] https://github.com/rest-assured/rest-assured/wiki/usage#specification-re-use [3] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/tree/master/src/main/java/com/eliasnogueira/credit/client [4] https://gitlab.com/elias.nogueira/test-combined-credit-api/-/tree/master/src/main/java/com/eliasnogueira/credit/specs
  • 50. For free Work in progress in 2 languages Access Leanpub and subscribe