Top Banner
Test the REST Testing RESTful web services using REST Assured An open source workshop by … Originally created by Bas Dijkstra – [email protected] – http://www.testautomation.com - @_basdijkstra
39

Testing RESTful web services with REST Assured

Jan 15, 2017

Download

Software

Bas Dijkstra
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Testing RESTful web services with REST Assured

Test the RESTTesting RESTful web services using REST Assured

An open source workshop by …

Originally created by Bas Dijkstra – [email protected] – http://www.testautomation.com - @_basdijkstra

Page 2: Testing RESTful web services with REST Assured

What are we going to do?

_RESTful web services

_REST Assured

_Get your hands dirty

Page 3: Testing RESTful web services with REST Assured

Preparation

_Install Eclipse (or any other IDE)

_Install TestNG plugin (for Eclipse)

_Install m2e (or similar for any other IDE)

_Import Maven project into IDE

_Update project (Eclipse) or similar

Page 4: Testing RESTful web services with REST Assured

What are RESTful web services?

_HTTP request methods (GET, POST, PUT, …)

_URI’s

_CRUD operations on dataPOST CreateGET ReadPUT UpdateDELETE Delete

Page 5: Testing RESTful web services with REST Assured

An example

_GET http://api.zippopotam.us/us/90210

_Result:

Page 6: Testing RESTful web services with REST Assured

Usage of RESTful web services

_Mobile applications

_Internet of Things

_API Economy

Page 7: Testing RESTful web services with REST Assured

Why REST (and not SOAP, for example)?_Support for a multitude of data formats

_JSON_XML_…

_Smaller overhead and therefore better performance

Page 8: Testing RESTful web services with REST Assured

Why SOAP(and not REST, for example)?_WS-Security

_WS-ReliableMessaging

_WS-AtomicTransaction

_Mostly used to secure banking applications

Page 9: Testing RESTful web services with REST Assured

Tools for testing RESTful web services_Browser (using plugins like Postman for Chrome)

_Open source (SoapUI, REST Assured)

_COTS (Parasoft SOAtest, SoapUI Pro)

Page 10: Testing RESTful web services with REST Assured

REST Assured

_Java library for writing tests for RESTful web services

_Removes a lot of boilerplate code

_Integrates seamlessly with existing Java-based testing frameworks_JUnit, TestNG_Selenium WebDriver

Page 11: Testing RESTful web services with REST Assured

Configuring REST Assured

_Download from http://rest-assured.io

_Add as a dependency to your project

_Maven

Page 12: Testing RESTful web services with REST Assured

REST Assured documentation

_Usage guide_https://github.com/rest-assured/rest-assured/wiki/Usage

_Links to other documentation (JavaDoc, getting started, release notes)_http://rest-assured.io/

Page 13: Testing RESTful web services with REST Assured

A sample test

Page 14: Testing RESTful web services with REST Assured

REST Assured features

_Support for HTTP methods (GET, POST, PUT, …)_Support for BDD / Gherkin (Given/When/Then)_Use of Hamcrest matchers for checks (equalTo)_Use of GPath for selecting elements from JSON response

Page 15: Testing RESTful web services with REST Assured

About Hamcrest matchers

_Express expectations in natural language

_Examples:

_ http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html

equalTo(X) Does the object equal X?

hasItem(“Rome”) Does the collection contain an item “Rome”?

hasSize(3) Does the size of the collection equal 3?

not(equalTo(X)) Inverts matcher equalTo()

Page 16: Testing RESTful web services with REST Assured

About GPath

_GPath is a path expression language integrated into Groovy _REST Assured is built in Groovy

_Similar aims and scope as XPath for XML

_Documentation and examples:_http://groovy-lang.org/processing-xml.html#_gpath_http://groovy.jmiguel.eu/groovy.codehaus.org/GPath.html

Page 17: Testing RESTful web services with REST Assured

GPath example

http://goessner.net/articles/JsonPath/

Page 18: Testing RESTful web services with REST Assured

Validating technical response data_HTTP status code

_MIME-type of received responses

_Cookies and their value

_…

Page 19: Testing RESTful web services with REST Assured

Our application under test

_Ergast F1 API

_Presents historical data of Formula 1 races, drivers, circuits, etc.

_Data can be returned in JSON and XML format

_API documentation at http://ergast.com/mrd/

Page 20: Testing RESTful web services with REST Assured

Some examples

_Data for driver Max Verstappen (in JSON):

http://ergast.com/api/f1/drivers/max_verstappen.json

_A list of circuits for the 2015 season (in JSON):

http://ergast.com/api/f1/2015/circuits.json

Page 21: Testing RESTful web services with REST Assured

Demo_ API documentation

_ How to use the test suite_ Executing your tests_ Reviewing test results

Page 22: Testing RESTful web services with REST Assured

Get your hands dirty!

_RestAssuredExercises1

_Simple checks_Validating individual elements_Validating collections and items therein_Validating technical response properties

_RestAssuredExamples contains all examples from the presentation

Page 23: Testing RESTful web services with REST Assured

Parameters in RESTful web services_Path parameters

_http://ergast.com/api/f1/drivers/max_verstappen.json_http://ergast.com/api/f1/drivers/hamilton.json

_Query string parameters_http://md5.jsontest.com/?text=testcaseOne_http://md5.jsontest.com/?text=testcaseTwo

_There is no official standard!

Page 24: Testing RESTful web services with REST Assured

Using parameters in REST Assured

_Examples for query parameters:_ Call to http://md5.jsontest.com/?text=testcaseOne

_ Call to http://api.openweathermap.org/data/2.5/weather/?q=Kopenhagen&mode=xml

Page 25: Testing RESTful web services with REST Assured

Using parameters in REST Assured

_Examples for path parameters:_ Call to http://ergast.com/api/f1/drivers/max_verstappen.json

_ Call to http://ergast.com/api/f1/drivers/alonso/constructors/renault/seasons.json

Page 26: Testing RESTful web services with REST Assured

Using parameters in REST Assured

_Iterating over a collection of parameter values:

_ Creating pairs of driver ID’s and numbers:

_ Using test data in API calls and validation of response data:

Page 27: Testing RESTful web services with REST Assured

Get your hands dirty!

_RestAssuredExercises2

_Data driven tests_Creating a test data object_Using test data in calling the right URI_Using test data in assertions

_RestAssuredExamples contains all examples from the presentation

Page 28: Testing RESTful web services with REST Assured

Authentication

_Securing web services

_Basic authentication (preemptive / challenged)

_OAuth(2)

_Digest / Form

Page 29: Testing RESTful web services with REST Assured

Basic authentication

_Username/password sent in header for every request

Page 30: Testing RESTful web services with REST Assured

OAuth(2)

_Request of authentication token based on username and password (Basic authentication)

_Include authentication token in header of all subsequent requests

Page 31: Testing RESTful web services with REST Assured

Measuring response times

_Measuring response times for individual requests

_Setting response time thresholds_Test fails when threshold is exceeded

_No full-fledged performance test

_First rough indication of API performance

Page 32: Testing RESTful web services with REST Assured

Measuring response times

_An example:

Page 33: Testing RESTful web services with REST Assured

Get your hands dirty!_RestAssuredExercises3

_Communicating with an OAuth2-secured API_ Requesting authentication token_ Using authentication token in subsequent requests

_Measuring API response times_ Execute a specific API call_ Evaluate response time against predefined threshold

_RestAssuredExamples contains all examples from the presentation

Page 34: Testing RESTful web services with REST Assured

Sharing variables between tests

_Example: authentication tests

_Copy / paste required for OAuth2 token

_Preferably: store and retrieve for reuse!

Page 35: Testing RESTful web services with REST Assured

Sharing variables between tests

_REST Assuredsupports thiswith extract()

Page 36: Testing RESTful web services with REST Assured

Get your hands dirty!

_RestAssuredExercises4

_Try it for yourself

_Can you apply this to the Formula 1 API?

_RestAssuredExamplesParameterPassing contains all examples from the presentation

Page 37: Testing RESTful web services with REST Assured

Executing tests in CI

_REST Assured-tests are no different from other Java (unit) tests

_Can be easily added to your CI/CD pipeline

_Part of the build process

_Demonstration (using Jenkins)

Page 38: Testing RESTful web services with REST Assured

Questions

?

Page 39: Testing RESTful web services with REST Assured

Contact

_Email: [email protected]

_Weblog: http://www.ontestautomation.com

_Twitter: @_basdijkstra