In a previous article (https://www.ideliversoft.com/post/contract-testing-part-two-understanding-the-basics), I have presented how you can create a docker container to test a new service you are building by using only the Swagger.yml file that describes your service.
The next challenge this article is trying to address is to provide a way to test all the APIs return codes defined in the Swagger file using any given testing framework (I am using JAPI for the purpose of this demonstration).
I have used Imposter to create and start a docker image for my service: https://docs.imposter.sh/run_imposter_docker/
My Swagger.yaml file shows various response codes: 200, 400, 403, 500, etc.
The "double" service I will be testing with it is created out of the "double" folder:
You have to pull the "outcoffee/imposter" image into your local docker environment:
docker pull outofcoffee/imposter
and then build the image ("onecloud/double:latest") using the Dockerfile.
The final image that you need to build which will allow you to configure response codes for your APIs it is created out of the "testing-double" folder:
This image is extending the original image (created using the swagger.yaml file) and allows scripts to be written and loaded dynamically that will make the service's API returns different return codes:
I have built this image with the name: "onecloud/testing-double".
I am running this image by attaching a Docker's volume that points to my "./Local_Config" folder:
docker run --rm -it --name devtest3 -v ./Local_Config:/opt/imposter/config -p 8080:8080 onecloud/testing-double:latest
I am copying into my docker's volume the script that will return only success results for my API:
docker cp ./Local_Config/script-load-returnCodes.groovy devtest3:/opt/imposter/config
Running the first set of tests returns success:
Running the second set of tests (that checks the result code of 400) returns failure:
I have changed the script result codes to "400" and copied again into the docker's volume:
Running the first set of tests returns failure:
Running the second set of tests returns success:
With your service's image up and running, you just have to modify the result codes into your script file and copied it into the docker's volume and then execute the new set of test cases you want to perform.
Happy testing!
Comments