APACHE SLING & FRIENDS TECH MEETUP
10-12 SEPTEMBER 2018
Karate, the black belt of HTTP API testing?

Bertrand Delacretaz :: @bdelacretaz :: grep.codeconsult.ch

Principal Scientist, Adobe Basel

Sling PMC Member, Member of the Board of Directors, Apache Software Foundation

slides revision 2018-09-11 - images are from Adobe Stock unless specified - portrait of myself here by Stefan Wieland, thanks!
Example code at

https://tinyurl.com/potsdam2018
Karate, the black belt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018
Karate?
https://github.com/intuit/karate
Efficient (and fun?) way of testing
HTTP services.
Created by Peter Thomas of Intuit.
V0.1.2 in February 2017
Current 0.8.0, moving away from
Cucumber
Example code at

https://tinyurl.com/potsdam2018 !
Karate, the black belt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018
Great docs & demos!
Excerpts from https://github.com/intuit/karate
Karate, the black belt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018
VERY readable tests - black belt!
Example from https://github.com/intuit/karate
“Laser focused on making things as simple as possible”
Karate, the black belt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018
HTTP testing in a world of services…
HTTP
service

API
Clients 

(usually machines)
Opaque Services
THIS is the “sacred boundary” that needs to be

VERY SERIOUSLY tested and documented.
Karate, the black belt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018
Karate tests as HTTP API reference documentation?
A great complement to

overview + concepts docs !
Testing Apache Sling with Karate
and demonstrating major Karate features
Karate, the black belt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018
Test Feature: basic structure
<- Tags for test selection
<- Tests are “features”
<- Background section for setup
<- Call javascript or other features
<- Use Java code to compute

unique test paths
<- We’re ready to run Scenarios
Karate, the black belt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018
Sling PostServlet Scenario
CAN U RD THS ?
Karate, the black belt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018
Define JSON structures, verify (match) responses
Testing the “import” operation of
the SlingPostServlet
Karate, the black belt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018
Uploading an image
Schema-like
matching, see
next slide
Karate, the black belt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018
Schema-like matching
Used in:
Karate, the black belt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018
Matching the image itself!
“Laser focused on making things as simple as possible”
Karate, the black belt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018
It’s not much code!
# The actual tests
src/test/java/sling/postservlet/importContent.feature
src/test/java/sling/postservlet/createContent.feature
src/test/java/sling/setup/initialContent.feature
src/test/java/sling/filestorage/uploadImage.feature
# JUnit tests "proxy"
src/test/java/sling/SlingTest.java (0 ALOC)
# Gatling performance tests "proxy"
src/test/scala/sling/PostServletSimulation.scala (~30 ALOC)
# Test utilities (all small)
src/test/java/util/cleanup-paths.js
src/test/java/util/basic-auth-header.js
src/test/java/util/sling-ready.feature
src/test/java/util/cleanup-path.feature
src/test/java/logBaseURL.js
# Test content
src/test/java/images/testimage.jpg
# Tests configuration
src/test/java/karate-config.js
src/test/java/logback-test.xml
Interlude: Running the Tests

aka “live demo”
Karate, the black belt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018
Running Karate tests
Using Maven via trivial JUnit proxy tests, selected by tags:
HTML test reports with debug info,

Gatling performance reports
Karate UI, basic
but useful.
More Fun & Useful Features

It’s not over yet
Karate, the black belt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018
Reuse Karate features for Gatling performance tests
Karate, the black belt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018
Scenario Outlines for data-driven testing
Validating initial content in
a default Sling instance
Karate, the black belt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018
Test Doubles: mock (main or auxiliary) services
Scenarios for
mocking HTTP
services,
similar syntax.
https://github.com/bdelacretaz/karate-mini-mocks
Karate, the black belt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018
Not much code for the Test Doubles either
# Mocked server + tests
src/test/java/server/server.feature
src/test/java/client/client.feature
# JUnit test "proxies"
src/test/java/server/TestBase.java
src/test/java/client/ClientTest.java
# Tests configuration
src/test/java/logback-test.xml
src/test/java/karate-config.js
Coda

Now it’s over. Almost.
Karate, the black belt of HTTP API testing? - Bertrand Delacretaz - @bdelacretaz - adaptTo() 2018
Poweful, efficient, fun
and READABLE tests!
Open for contributions
Good enough to
document HTTP APIs?
Example code at

https://tinyurl.com/potsdam2018 !
https://github.com/intuit/karate

Karate, the black belt of HTTP API testing?

  • 1.
    APACHE SLING &FRIENDS TECH MEETUP 10-12 SEPTEMBER 2018 Karate, the black belt of HTTP API testing?
 Bertrand Delacretaz :: @bdelacretaz :: grep.codeconsult.ch
 Principal Scientist, Adobe Basel
 Sling PMC Member, Member of the Board of Directors, Apache Software Foundation
 slides revision 2018-09-11 - images are from Adobe Stock unless specified - portrait of myself here by Stefan Wieland, thanks! Example code at
 https://tinyurl.com/potsdam2018
  • 2.
    Karate, the blackbelt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018 Karate? https://github.com/intuit/karate Efficient (and fun?) way of testing HTTP services. Created by Peter Thomas of Intuit. V0.1.2 in February 2017 Current 0.8.0, moving away from Cucumber Example code at
 https://tinyurl.com/potsdam2018 !
  • 3.
    Karate, the blackbelt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018 Great docs & demos! Excerpts from https://github.com/intuit/karate
  • 4.
    Karate, the blackbelt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018 VERY readable tests - black belt! Example from https://github.com/intuit/karate “Laser focused on making things as simple as possible”
  • 5.
    Karate, the blackbelt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018 HTTP testing in a world of services… HTTP service
 API Clients 
 (usually machines) Opaque Services THIS is the “sacred boundary” that needs to be
 VERY SERIOUSLY tested and documented.
  • 6.
    Karate, the blackbelt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018 Karate tests as HTTP API reference documentation? A great complement to
 overview + concepts docs !
  • 7.
    Testing Apache Slingwith Karate
and demonstrating major Karate features
  • 8.
    Karate, the blackbelt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018 Test Feature: basic structure <- Tags for test selection <- Tests are “features” <- Background section for setup <- Call javascript or other features <- Use Java code to compute
 unique test paths <- We’re ready to run Scenarios
  • 9.
    Karate, the blackbelt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018 Sling PostServlet Scenario CAN U RD THS ?
  • 10.
    Karate, the blackbelt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018 Define JSON structures, verify (match) responses Testing the “import” operation of the SlingPostServlet
  • 11.
    Karate, the blackbelt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018 Uploading an image Schema-like matching, see next slide
  • 12.
    Karate, the blackbelt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018 Schema-like matching Used in:
  • 13.
    Karate, the blackbelt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018 Matching the image itself! “Laser focused on making things as simple as possible”
  • 14.
    Karate, the blackbelt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018 It’s not much code! # The actual tests src/test/java/sling/postservlet/importContent.feature src/test/java/sling/postservlet/createContent.feature src/test/java/sling/setup/initialContent.feature src/test/java/sling/filestorage/uploadImage.feature # JUnit tests "proxy" src/test/java/sling/SlingTest.java (0 ALOC) # Gatling performance tests "proxy" src/test/scala/sling/PostServletSimulation.scala (~30 ALOC) # Test utilities (all small) src/test/java/util/cleanup-paths.js src/test/java/util/basic-auth-header.js src/test/java/util/sling-ready.feature src/test/java/util/cleanup-path.feature src/test/java/logBaseURL.js # Test content src/test/java/images/testimage.jpg # Tests configuration src/test/java/karate-config.js src/test/java/logback-test.xml
  • 15.
    Interlude: Running theTests
 aka “live demo”
  • 16.
    Karate, the blackbelt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018 Running Karate tests Using Maven via trivial JUnit proxy tests, selected by tags: HTML test reports with debug info,
 Gatling performance reports Karate UI, basic but useful.
  • 17.
    More Fun &Useful Features
 It’s not over yet
  • 18.
    Karate, the blackbelt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018 Reuse Karate features for Gatling performance tests
  • 19.
    Karate, the blackbelt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018 Scenario Outlines for data-driven testing Validating initial content in a default Sling instance
  • 20.
    Karate, the blackbelt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018 Test Doubles: mock (main or auxiliary) services Scenarios for mocking HTTP services, similar syntax. https://github.com/bdelacretaz/karate-mini-mocks
  • 21.
    Karate, the blackbelt of HTTP API testing? - Bertrand Delacretaz - adaptTo() 2018 Not much code for the Test Doubles either # Mocked server + tests src/test/java/server/server.feature src/test/java/client/client.feature # JUnit test "proxies" src/test/java/server/TestBase.java src/test/java/client/ClientTest.java # Tests configuration src/test/java/logback-test.xml src/test/java/karate-config.js
  • 22.
  • 23.
    Karate, the blackbelt of HTTP API testing? - Bertrand Delacretaz - @bdelacretaz - adaptTo() 2018 Poweful, efficient, fun and READABLE tests! Open for contributions Good enough to document HTTP APIs? Example code at
 https://tinyurl.com/potsdam2018 ! https://github.com/intuit/karate