GPTest

PAID
By Leonardo Silva | Updated 2 months ago | Business Software
Health Check

N/A

Back to All Tutorials (1)

Create your first unit test

Step 1: Define a function to be tested

We will create a simple temperature conversion code

// Celsius to Fahrenheit conversion
function celsiusToFahrenheit(celsius) {
  var fahrenheit = celsius * 9/5 + 32;
  return fahrenheit;
}

Step 2: Make the request

Now, open up your terminal and paste the following cURL request:

curl --location --request POST 'https://gptest1.p.rapidapi.com/api/v0/unit-test-generation' \
--header 'Content-Type: application/json' \
--header 'X-RapidAPI-Key: YOUR_RAPIDAPI_KEY \
--header 'X-RapidAPI-Host: gptest1.p.rapidapi.com' \
--data-raw '{"code": "function celsiusToFahrenheit(celsius) {  var fahrenheit = celsius * 9/5 + 32;  return fahrenheit;}"'

TA-DA! There you have your unit test

{
  "unit_test": "describe('sum function', () => {\n    it('should return the sum of two positive numbers', () => {\n      expect(sum(2, 3)).toEqual(5);\n    });\n\n    it('should return the sum of a positive and a negative number', () => {\n      expect(sum(2, -3)).toEqual(-1);\n    });\n  });"
}