DEV Community

Royal Jain
Royal Jain

Posted on

Automation of API Testing using CodeParrot

Tired of the monotonous task of testing your code? Well, it’s time to break free from the boredom and discover a fresh new way to approach API testing!

Code Testing Meme

CodeParrot is a continuous testing platform which continuously records production traffic (request / response of your app) and tests your app by replaying the captured requests and comparing responses.

Setup

In this tutorial, we will use CodeParrot with a NodeJs application. Our sample application “Paymentservice” contains /charge_card API which takes amount and credit card details as input and returns transaction id. Transaction id is -1 if the inputs are incorrect. Our goal is to test for all the possible scenarios ( example — payment success, incorrect expiry date, incorrect cvv etc)

POST /charge_card

{
  "amount": {
    "currency_code": "USD",
    "units": "346"
  },
  "credit_card": {
    "credit_card_cvv": 452,
    "credit_card_expiration_month": 5,
    "credit_card_expiration_year": 2025,
    "credit_card_number": "4242-4242-4242-4242"
  }
}

Enter fullscreen mode Exit fullscreen mode

Installation

CodeParrot can be simply installed as a npm package which records the production traffic. Checkout installation instructions

Continuous Testing

As users start interacting with the API, CodeParrot will automatically start recording unique test cases. To start recording test cases, just require the codeparrot library before starting your app

node -r @codeparrot/js-agent <app_name>.js

The generated test cases for /charge_card API are

// Incorrect credit card number {"amount": {"currency_code": "INR","units": "28750"},"credit_card": {"credit_card_cvv": 789,"credit_card_expiration_month":
5, "credit_card_expiration_year": 2025,"credit_card_number": "4242-4242-4242-42432"}}

// Success {"amount": {"currency_code": "USD","units": "346"},"credit_card": {"credit_card_cvv": 789,"credit_card_expiration_month":
5, "credit_card_expiration_year": 2027,"credit_card_number": "4786-8756-4356-7865"}}

// Expired credit card {"amount": {"currency_code": "USD", "units": "346"},"credit_card": {"credit_card_cvv": 789,"credit_card_expiration_month":
5, "credit_card_expiration_year": 2024,"credit_card_number": "4553-8723-8529-0867"}}

// Incorrect credit card cvv {"amount": {"currency_code": "USD", "units": "346"},"credit_card": {"credit_card_cvv": "a789","credit_card_expiration_month":
5, "credit_card_expiration_year": 2025,"credit_card_number": "4532-0981-4528-0956"}}

Enter fullscreen mode Exit fullscreen mode

Before pushing an update, all the captured test cases can be easily replayed on local machine without any setup ( CodeParrot automatically mocks any downstream dependency with the production response ensuring consistency in production and localhost). To replay the recorded test cases, simply set the following environment variables and restart the app

CODE_PARROT_IS_REPLAY=true

// Identifier for the test report, test report will be overwritten if this version is same
CODE_PARROT_VERSION=1.0

node -r @codeparrot/js-agent <app_name>.js

Enter fullscreen mode Exit fullscreen mode

This will generate a link to the test report, you can quickly see the impact of the change and push confidently.

Liked it? If you would like access to CodeParrot, just share your email address on this form and we’ll share the API key or reach out at royal@codeparrot.ai for more details

Top comments (1)

Collapse
 
michele2244 profile image
michele

When a business offers a cash discount, they encourage customers to pay promptly, preferably in cash or by a specific payment method such as electronic funds transfer. The discount acts as an incentive to motivate customers to pay early, thereby accelerating the cash flow for the business. Credit Card Processing The discount is usually presented as a small percentage off the total purchase price, such as 1% or 2%, but it can vary depending on the industry and business preferences.