Automation of API Testing using CodeParrot
Tags: api, javascript, testing, automation
Published: 2023-04-12 08:30:12

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28k0fghcrf0wxd2yemva.png)




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"
  }
}

```

**Installation**

CodeParrot can be simply installed as a npm package which records the production traffic. Checkout [installation instructions](https://wry-open-089.notion.site/CodeParrot-nodeJS-setup-8970027671dd42fba86f9312141d1b89)  


**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"}}

```

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

```
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