API Automation Using Karate at Funding Societies

Automation is a crucial aspect of ensuring the quality and reliability of our software products. API automation testing is a critical step in achieving this, and it’s a journey that we embarked on with Karate, a versatile and powerful tool for API testing and automation. In this article, we’ll discuss how our team achieved 100% API automation using Karate.

Why API Automation Matters

APIs act as the bridge connecting different software components, enabling them to communicate and work together seamlessly.

Automating API tests has several advantages:

  1. Efficiency: Automated tests can be executed quickly and repeatedly, ensuring a rapid feedback loop during development and continuous integration.
  2. Accuracy: Automation eliminates the risk of human error, providing consistent and reliable results.
  3. Coverage: Automation allows for thorough testing of various scenarios and edge cases, which can be challenging to achieve with manual testing.
  4. Regression Testing: As software evolves, regression tests become essential to ensure that new changes don’t break existing functionality. Automation makes this easier.

The Karate Advantage

Karate, an open-source tool, quickly emerged as our preferred choice for API automation. Its unique strengths set it apart from other tools:

1. Behaviour-Driven Development (BDD):

Karate uses a BDD approach to writing tests. This means that tests are expressed in a language that both technical and non-technical stakeholders can understand. This has improved collaboration and communication within our team, as everyone can contribute to test scenarios, and even non-programmers can write tests. We only need to write tests in Karate DSL with very little custom code required.

Feature: User API Test

Background:
* url 'https://api.example.com/user'

Scenario: Get User by ID
Given path 'get'
And header Authorization = 'Bearer your-access-token'
And param userId = 123
When method GET
Then status 200
And match response == { id: 123, name: 'John Doe', email: 'johndoe@example.com' }

2. Powerful Assertions:

Karate provides a rich set of built-in assertions, enabling comprehensive validation of API responses. Karate supports deep assertions on JSON and XML structures, making it easy to validate nested elements and complex data structures and it offers logical assertions that allow you to combine multiple assertions using logical operators (e.g., and, or, not).

Feature: Testing the Todo API

Background:
* url 'https://jsonplaceholder.typicode.com/todos'

Scenario: Get a list of todos
Given path '1'
When method GET
Then status 200
And match response == { userId: 1, id: 1, title: '# Buy groceries', completed: false }
And match response.userId == 1
And match response.title contains 'groceries'
And match response.completed == false
And match response.id > 0
And match response.title == '# Buy groceries' && response.completed == false

3. Data-Driven Testing:

Karate’s built-in data management capabilities allow us to handle data-driven testing seamlessly. We can easily parameterise our tests and run them with different data sets, ensuring comprehensive test coverage.

Feature: Data-Driven Test Example

Background:
* url 'https://jsonplaceholder.typicode.com/users'

Scenario Outline: Verify User Data
Given path '1'
When method GET
Then status 200
And match response == <expectedUser>

Examples:
| expectedUser |
| { id: 1, name: 'Leanne Graham', username: 'Bret', email: 'Sincere@april.biz' } |
| { id: 2, name: 'Ervin Howell', username: 'Antonette', email: 'Shanna@melissa.tv' } |
| { id: 3, name: 'Clementine Bauch', username: 'Samantha', email: 'Nathan@yesenia.net' } |
| { id: 4, name: 'Patricia Lebsack', username: 'Karianne', email: 'Julianne.OConner@kory.org' } |

4. Parallel Execution:

Karate supports parallel test execution, enabling us to significantly reduce the time it takes to run a large suite of tests. This scalability is vital as our application grows and the number of APIs increases.

5. Custom JavaScript Functions:

While Karate provides a rich set of built-in functions, there were cases where we needed custom logic. Karate allows you to integrate custom JavaScript functions seamlessly.

Background:
* def calculateSum =
"""
function(a, b) {
return a + b;
}
"""

Scenario: Using Custom JavaScript Function
* def sum = calculateSum(3, 5)
Then assert sum == 8

6. Rich Reporting:

Karate generates detailed HTML reports, making it easy to understand test results and identify issues. These reports are invaluable for tracking the progress of our API tests.

Our Journey to 100% API Automation

Our path to achieving 100% API automation using Karate was marked by a few key steps:

1. Tool Evaluation:

We began by evaluating different API automation tools, including RestAssured, pytest, Postman, and Karate. While these tools had their merits, we found that Karate’s integrated approach to BDD, data management, reporting, and parallel execution stood out as the most suitable for our needs.

2. Team Training:

To make the most of Karate, we invested in training and documentation to ensure that all team members could effectively use the tool. This lowered the learning curve and boosted our adoption rate.

3. Test Case Design:

We adopted a behaviour-driven development (BDD) approach, which helped us write clear and concise test cases. These test cases were written in plain English and covered both positive and negative scenarios.

4. Continuous Integration:

We integrated our Karate tests into our CI/CD pipeline, ensuring that tests run automatically with each code commit. This approach caught issues early and reduced the likelihood of bugs reaching production.

5. Maintenance and Updates:

APIs and applications evolve, so we established a process to update APIs and Karate tests simultaneously. This helped us adapt to changes swiftly and maintain test reliability.

The Benefits of 100% API Automation

Achieving 100% API automation has had a profound impact on our development process:

  • Faster Feedback: Our automated tests provide rapid feedback on code changes, allowing us to address issues early in the development cycle.
  • Reliability: With every test case automated, we have significantly reduced the risk of regressions or unexpected issues in our application.
  • Enhanced Collaboration: The BDD approach in Karate promotes collaboration among team members, as it encourages a shared understanding of test scenarios.
  • Confident Releases: The confidence that our tests provide has translated into smoother and more reliable software releases. our cycle time & deployment frequency have improved drastically,
    From biweekly releases (once per sprint) we have achieved near-daily releases
  • Cost-Efficiency: Automated tests reduced the need for manual testing, saving both time and resources.

Conclusion

Automation is essential to maintain the quality and reliability of our applications. Achieving 100% API automation using Karate has been a game-changer for our team. It has allowed us to create comprehensive and efficient API test suites that integrate seamlessly into our CI/CD pipeline.

As our application continues to grow and evolve, we are certain that Karate will remain an invaluable tool in our testing toolkit, helping us maintain the highest level of quality and reliability in our software.

--

--