Junior — Middle
What methods can be used to implement automated testing of API interfaces?
sobes.tech AI
Answer from AI
Automated API testing can be implemented in the following ways:
- Using specialized tools: Postman (with collections and JavaScript tests), SoapUI, Insomnia.
- Writing tests with programming languages and libraries: for example, JavaScript with Jest, Mocha, Chai, Supertest; Python with requests and pytest; Java with RestAssured.
- Integration with CI/CD: running tests automatically during project build.
- Testing various aspects of API: functional testing, response verification, status codes, response time, data schema validation (JSON Schema).
Example of a simple test in Node.js using Jest and Supertest:
const request = require('supertest');
const app = require('./app'); // your Express application
test('GET /api/users returns a list of users', async () => {
const response = await request(app).get('/api/users');
expect(response.statusCode).toBe(200);
expect(Array.isArray(response.body)).toBe(true);
});