Karate for Complex Web-Service API Testing by Peter Thomas

18
Peter Thomas Architect | Intuit @ptrthomas | @KarateDSL Karate for Complex Web- Service API Testing

Transcript of Karate for Complex Web-Service API Testing by Peter Thomas

Page 1: Karate for Complex Web-Service API Testing by Peter Thomas

Peter Thomas

Architect | Intuit

@ptrthomas | @KarateDSL

Karate for Complex Web-Service API Testing

Page 2: Karate for Complex Web-Service API Testing by Peter Thomas

2

What you can expect from this session

I want to test web-services and

currently use Java

I want to test web-services but

don’t know Java

I develop web-services

I am part of a team delivering or

maintaining web-services

Karate is easier and more

effective than Java

Karate is not-Java and

scripts are plain-text

Karate can be used for

TDD of web-services

Karate increases velocity,

and fits into your existing

QA / CI / CD

Page 3: Karate for Complex Web-Service API Testing by Peter Thomas

Karate

github.com/intuit/karate

Intuit Open Source

Released 09-Feb-2017

A DSL for writing web-service acceptance tests

Built on Cucumber | BDD syntax

Page 5: Karate for Complex Web-Service API Testing by Peter Thomas

5

Many teams use Java for writing HTTP-API

regression tests.

Java is not the best language to manipulate

JSON / XML.

Consequences:

• Over-dependence on Java objects for

building and validating data-payloads

• Object-equality checks done by hand,

effort-intensive

• So tests end-up taking “short-cuts”, and

don’t validate the full response, coverage

& quality suffers

• Proliferation of helper methods, code bloat

& in-house layers

• Result: poor Dev Productivity &

Maintainability

Current State of Web-API Testing

• DSL laser-focused on HTTP

• JSON and XML are “first-class”

citizens of the syntax

• Compare deeply-nested payloads in

a single step

• 3:1 Reduction in Lines of Code

• Tests are super-readable and concise

• Testing is Easier & actually Fun

Karate’s Solve

Page 6: Karate for Complex Web-Service API Testing by Peter Thomas

6

Scenario: create and retrieve a cat

Given url 'http://myhost.com/v1/cats'

And request { name: 'Billie' }

When method post

Then status 201

And match response == { id: '#notnull', name: 'Billie' }

Given path response.id

When method get

Then status 200

Hello World

Intuitive DSL for

HTTP

Payload

assertion in

one line

Second HTTP

call using

response data

JSON is ‘native’

to the syntax

Page 7: Karate for Complex Web-Service API Testing by Peter Thomas

7

Java vs

JSON

Page 8: Karate for Complex Web-Service API Testing by Peter Thomas

8

Validation examples: JsonPath & RegEx

Type Directive

Ignore / exists #ignore | #null | #notnull

Primitive Type #boolean | #number | #string

Complex Type #array | #object

Special #uuid

RegEx #regex <regex string>

Predicate #? <expression>

Page 9: Karate for Complex Web-Service API Testing by Peter Thomas

9

JSON ‘templating’

Type Directive

Replace #(<expression>)

Page 10: Karate for Complex Web-Service API Testing by Peter Thomas

10

XML and XPath

Page 11: Karate for Complex Web-Service API Testing by Peter Thomas

11

Comparison with REST-assured

http://tinyurl.com/karatera

REST-assured Karate

Plain Text ❌ (needs compilation) ✅

Parallel Execution ? (partial) ✅

Data Driven Testing ❌ (needs TestNG etc.) ✅ (built-in)

Environment Switching ❌ ✅ (built-in)

Match full payload in one step ❌ ✅

Update JSON payload / object ❌ ✅

@Test public void

lotto_resource_returns_200_with_expected_id_and_winners() {

when().

get("/lotto/{id}", 5).

then().

statusCode(200).

body("lotto.lottoId", equalTo(5),

"lotto.winners.winnerId", containsOnly(23, 54));

Scenario: lotto resource returns 200 with expected id and winners

Given path ‘lotto’, 5

When method get

Then status 200

And match $.lotto.lottoId == 5

And match $.lotto.winners[*].winnerId contains only [23, 54]

given().

param("key1", "value1").

param("key2", "value2").

when().

get("/somewhere").

then().

body(containsString("OK"));

Given param key1 = ‘value1’

And param key2 = ‘value2’

And path ‘somewhere’

When method get

Then response contains ‘OK’

Page 12: Karate for Complex Web-Service API Testing by Peter Thomas

12

Page 13: Karate for Complex Web-Service API Testing by Peter Thomas

13

Components

Karate DSL interpreter

(Cucumber “Step Definitions”)

HTTP Client

Abstraction

JSON XMLJS

Engine(Nashorn)

Karate ScriptJUnit /

Test NG

(Runner)

Ja

va

8 J

RE

HTTP/S calls

Cucumber-

JVM

Data Functionsmatch, get, set

Reports

Apache Jersey

Page 14: Karate for Complex Web-Service API Testing by Peter Thomas

14

• Built on Cucumber, BDD syntax

• Scripts are plain-text, no compilation or IDE required

• REST as well as SOAP support

• Assert, manipulate, compare and re-use JSON / XML

• User defined functions (Java or JavaScript)

• Configuration switching (e.g. dev | e2e | pre-prod)

• Simple ‘plug and play’ HTTP Header & Auth management

• Comprehensive HTTP support: SSL, Proxy, File-Upload

• JUnit XML reports compatible with all CI tools

• Speed - Parallel Execution of Tests

Karate Features

BDD

Full HTTP

CI / CD

Ready

Page 15: Karate for Complex Web-Service API Testing by Peter Thomas

Demos

Page 16: Karate for Complex Web-Service API Testing by Peter Thomas

Q & A

https://github.com/intuit/karate

@ptrthomas | @KarateDSL

Page 17: Karate for Complex Web-Service API Testing by Peter Thomas

17

The “missing” Cucumber Features that Karate adds

Capability Cucumber Karate

Step Definitions built-in, no Java code needed ❌ ✅

Re-Use Feature files from other Features ❌ ✅

Dynamic Data-Driven Testing ❌ ✅

Parallel Test Execution and Reporting ❌ ✅

Option to run routines only once per Feature ❌ ✅

Page 18: Karate for Complex Web-Service API Testing by Peter Thomas

18

CI Report(Jenkins)