class: center, middle, title # API Testing with REST Assured
Philip Durbin ([@philipdurbin](https://twitter.com/philipdurbin)) http://www.iq.harvard.edu/people/philip-durbin ] .right[
] 2016-01-12 --- # Agenda - How to write and run API tests from Netbeans. - How to execute tests on a server such as http://phoenix.dataverse.org - Tips and tricks on using REST Assured. - Overview of automated testing ideas: https://github.com/IQSS/dataverse/issues/2746 --- # What is an API? - Application Programming Interface. - Technically, APIs are everywhere. We're talking about testing REST (HTTP) APIs.
--- # The good and bad of testing APIs - Good - Real world ("acceptance tests"). - Can you create a user? - Can you upload a file? - Much easier than testing the UI. - Is business logic centralized? Any GUI-only features? - Bad - Database, etc. must be running. - Where are these tests going to run? - Many steps required to test certain scenarios (i.e. deleting a file). - Hard to measure coverage, unlike unit tests: https://coveralls.io/github/IQSS/dataverse --- # How do you test an API? - One off tests - Hit it with `curl` a few times in development. Hit the "up" arrow as needed. - Automate the tests - Use REST Assured or your favorite testing library. - How often do you run them? Where? On your laptop? - Run the automated tests on a real server - Use Jenkins or similar to trigger the tests after a build. --- # Is there an API Test already?
--- # DataversesIT ("integration test")
src/test/java/edu/harvard/iq/dataverse/api --- # Creating an Integration Test Step 1
--- # Creating an Integration Test Step 2
- FooTest vs. FooIT - IT checkbox - JUnit - Code generation --- # Side Effects of API Testing
- Do no harm. - Clean up after yourself. --- # Running an integration test
mvn test -Dtest=DataversesIT --- # What is the test about? ### `testAttemptToCreateDuplicateAlias`
- Reads a bit like a user story. - "Alice attempts to create a dataverse with an alias that's the upper case version of an existing alias. This should fail and report the error back to Alice."
cfa vs. CFA vs. CfA https://github.com/IQSS/dataverse/issues/2598 --- # Is your API test doing too much? #### `testCreateDatasetUploadFileDownloadFileEditTitle`
Assert that: - dataverse created - dataset created via SWORD - datasets listed via SWORD - file uploaded - download file by id - delete file - update title - etc. src/test/java/edu/harvard/iq/dataverse/api/SwordIT.java --- # JUnit Assertions .center[
] - `assertEquals(200, attemptToDoSomethingGood)` - `assertEquals(400, attemptToDoSomethingBad)` --- # REST Assured "Java DSL for easy testing of REST services" -- https://github.com/jayway/rest-assured
"Jayway is a software innovation consultancy" -- http://jayway.com
- status codes - JSON response - XML response --- # REST Assured Assertions Response listDatasetsResponse = UtilIT.listDatasetsViaSword(dataverseAlias1, apiToken1); listDatasetsResponse.prettyPrint(); listDatasetsResponse.then().assertThat() .statusCode(OK.getStatusCode()) .body("feed.dataverseHasBeenReleased", equalTo("false")) .body("feed.entry.title", equalTo(initialDatasetTitle)); Assert that: - Status code is OK (200). - "dataverseHasBeenReleased" is false - title matches expected string Return type: `com.jayway.restassured.response.Response` src/test/java/edu/harvard/iq/dataverse/api/SwordIT.java --- # Testing on a server
https://build.hmdc.harvard.edu:8443 --- # Triggering tests from Jenkins - Build - Build a war file and package setup scripts. - https://build.hmdc.harvard.edu:8443/job/phoenix.dataverse.org-build-4.2.3/ - Deploy - Drop database, deploy war file, setup, curl tests. - https://build.hmdc.harvard.edu:8443/job/phoenix.dataverse.org-deploy/ - Test - REST Assured tests - https://build.hmdc.harvard.edu:8443/job/phoenix.dataverse.org-apitest-4.2.3/ --- # Only a piece of the testing puzzle Improve automated testing - https://github.com/IQSS/dataverse/issues/2746
- API Tests - Unit Tests - [The Testable Command](https://github.com/IQSS/dataverse/blob/1d7eadcb89ccebb4990c62757c84ec705f13853d/doc/theTestableCommand/TheTestableCommand.md) - Selenium (browser) - Performance - Security - etc. .footnote[ https://www.flickr.com/photos/horiavarlan/4273913228 ]