Primer on Automated Testing With Karate DSL
Tags: automatedtesting, karatedsl, apitesting, hacktoberfest
Published: 2020-12-16 02:45:06

####Motivation
With Microservices architecture, comes along a plethora of challenges that need to be dealt with - and most of these challenges can be resolved with well established patterns & implementations - for ex Spring Cloud provides concrete implementations along the lines of api gateway, service discoveries , distributed tracings, externalized and centralized configuration server and the list goes on. Having said that, one of the bigger challenges I experienced is testing a distributed system[ _not simply testing an individual service_] as a whole. This system has many moving parts to it - there are distributed dependent services / UI's / network latencies / data inconsistencies etc.

Now, with 'shift left' being the norm - the idea is to provide faster feedbacks -> which results in faster resolutions -> which leads to better system, way earlier in development process [ vs Traditional Software Developments ].

The goal of this blog is to present [Karate DSL](https://github.com/intuit/karate) as a possible solution for testing such a system. Overall, Karate has much much more to offer and I will encourage you to explore more.

####Enter Karate DSL
In my past life I was yearning for an all encompassing automated testing framework that is:
* extension-able i.e. allow to do more with what is abstracted. 
* pluggable / modular i.e. add / remove functionalities as needed.
* programming language agnostic [ _as much as possible_ ] for wider reach among different stakeholders.
* highly observable with reporting and logging capabilities.
* ease of testing API's and UI all at the sametime. - I want to call an API to get auth token and use that token to invoke other services or UI or a combination of both.
* should be simple to learn and intuitive to use. 

Behold, came the Octoberfest 2020 and I stumbled upon an amazing framework called Karate DSL. I was able to contribute, as well learn more about this awesome tool at the same time.

#### [What is Karate DSL](https://intuit.github.io/karate/)
[Peter Thomas](https://github.com/ptrthomas) is the lead developer, as well main maintainer for this tool.

>_Karate is the only open-source tool to combine API test-automation, mocks, performance-testing and even UI automation into a single, unified framework. The BDD syntax popularized by Cucumber is language-neutral, and easy for even non-programmers. Powerful JSON & XML assertions are built-in, and you can run tests in parallel for speed._

#### Hands-On
Lets say, you have an API that allows to do CRUD operations on _Posts_. Now, look at this feature file - isn't it _very intuitive as to what is going on in here_? -
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/s0e0jiu3v50t64oii0fo.png)
With this you have a simple test suite with different scenarios, and you are validating different _operations_ on a API - all written in _Gherkin Syntax_. And there is nothing else to write or code - except including Karate libs.

Writing this test suite requires a very basic knowledge and anyone in your team - a developer, a tester, a BA, product owner etc. can write these and be _glued_ with the expected behavior of the system. Include this in build pipeline or run on a schedule to measure the _performance_ of the system - as you wish.

Now, let us look at the test report:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8i3udpunikkqf0d6iqz9.png)

This is a very detailed and thorough report - you can see the timestamps and individual _step_ logs:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1687l7tuxuwxa0dptl66.png)

You can seamlessly invoke rest apis and web UI calls in your test suite and exchange headers and cookies among those calls:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ay3yzungsbssui4nol01.png) 
######[source](https://github.com/intuit/karate)

You can even perform accessibility testing simply as following -
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pmfd0t9mcc4f452dkyka.png)

and your output report looks like this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0sqdubc9am3mlskwksui.png)

There are many more goodies provided by this framework - docker testing support / performance testing / UI testing / test report creations etc just to name a few. 

The framework has observed some amazing attraction in recent times [ _see github stars_ ], has amazing community support via _slack_ / _stackoverflow_ / _github_ and is something that should be seriously considered by architects / developers / QA alike. Cheers!

Feel free to leave any feedback.