In this post, I will show you, how simple and fast you can write automation tests for your API using ZeroCode framework
Before we start, let’s quickly read about Test Flavours
- test types and where does ZeroCode framework fits in.
Test Flavours
Alright cool, so if you have read about Test Flavours, then let’s get into code and find out how simple and quickly we can write tests with ZeroCode framework.
Product API
We have got our Product API
running locally with below end points and we will be writing ZeroCode tests for this Product API.
-
GET /products
Returns list of products -
POST /products
Creates a new product -
PUT /products/{id}
Updates a product for a given product id -
PATCH /products/{id}
Partial updates to product for a given id -
DELETE /products/{id}
Deletes a product for a given product id
Product API running locally
I have product api running on http://localhost:7070 and and it’s written in GO using Gorilla Mux library.
ZeroCode tests
Steps to write ZeroCode tests.
Step 1 - Generate test project using ArcheType
Archetype is a fastest way to generate project skeleton and we can add boiler plate code with it.
Create a new folder ➡️ cd to that folder ➡️ copy below archetype command and just run it.
You should see a project ready for you with all the boiler plate code.
mvn archetype:generate \
-DarchetypeGroupId=org.jsmart \
-DarchetypeArtifactId=zerocode-maven-archetype \
-DgroupId=com.ns \
-DartifactId=product-api-tests \
-Dversion=1.0.0-SNAPSHOT
ArchType - see in Action
Step 2 - Writing Tests
Ok, Cool. We have got our project ready with dummy tests.
Now let’s update config and tests
-
update
hostconfig_ci.properties
Open
hostconfig_ci.properties
file and add url of our Product api against the keyweb.application.endpoint.host
.
I have added url of locally running Product API.web.application.endpoint.host=
http://localhost:7070#Continuous Integration Context # Web Server host and port web.application.endpoint.host=http://localhost:7070 # Web Service Port; Leave it blank in case it is default port i.e. 80 or 443 etc web.application.endpoint.port= # Web Service context; Leave it blank in case you do not have a common context web.application.endpoint.context=
-
Write tests cases using JSON
Writing test for GET end point.
For all other endpoints tests, please have a look at the section
Writing All Tests in Action
below.GET /products
Open
get_api_200.json
and update below as per your api functionality.
For Product API, I have updated below.->
name
- “get_product_detail”. It’s a step name to tell, what api we are testing.
->url
- /products
->request body
- left it blank here for GET request in this case
->verify status
- It’s a assertion we are doing here. Verifying 200 status code.
->verify response body
- Asserting that response body should have not-null id.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
{ "scenarioName": "Validate the GET api", "steps": [ { "name": "get_product_details", "url": "/products", "method": "GET", "request": { }, "verify": { "status": 200, "body":[ { "id" : "$IS.NOTNULL" } ] } } ] }
And that’s all, Our test case is ready for GET PRODUCT details.
That’s all! We are just done with writing out tests. It just really few mins!!
Step 3 - Running the tests
Let’s run our tests. We can run test directly from IDE and from command line and from Jenkins jobs. 😎
For integration with CI/CD pipeline, we can just create mvn or gradle tasks for our tests and configure the task in the jenkins pipeline.
Running Individual Test from command line
mvn -Dtest=MyGetApiTest test
Running TestSuite from command line
mvn -Dtest=MyApiSuite test
Creating gradle task
task runProductAPITestSuite ( type : Test ) {
delete "/target/"
systemProperty 'zerocode.junit', 'gen-smart-charts-csv-reports'
include 'com/ns/MyApiSuite.class
}
Running the gradle task - runProductAPITestSuite
gradle runProductAPITestSuite
Test Reports
ZeroCode generates interactive test reports.
Just build the test project and it will run all the tests and create a report folder which will have detailed report.
mvn clean install
I hope you like this post. Please do share your comments.