DEV Community

Cover image for Boost your code coverage with API Tests
Anudeep
Anudeep

Posted on • Updated on

Boost your code coverage with API Tests

Code coverage is an important quality metric that determines the number of lines of source code that is tested & covered by automated tests. Usually, developers achieve code coverage close to 80% by writing unit tests (most popular tests to generate code coverage).

Targeted Code

In general unit tests targets happy paths, core business logic, and rarely sad paths. Most likely they can give us close to 80% of code coverage. The remaining 20% of source code might be responsible for handling external interfaces and exceptions/errors. Unit tests generally omit testing external interface logic that interacts with outside applications & databases.

Testing the external interface logic at the early phases of SDLC is very critical for delivering a quality software product.

API Testing

API testing is critical for automating testing because APIs now serve as the primary interface to application logic. API tests could greatly improve the code coverage of applications and the overall confidence in the product.

Let's see how to get code coverage from API tests.

Example

In this example, we will be looking at a basic Node.js web application.

Source Code

Look at the sample web app server written in express. (Express is a minimal and flexible Node.js web application framework)

// index.js
const express = require('express');
const app = express();
const port = 3333;

app.get('/hello', (req, res) => {
  res.send('Hello World!');
});

app.listen(port, () => {
  console.log(`App listening at http://localhost:${port}`);
});
Enter fullscreen mode Exit fullscreen mode

To run the application, execute the below command in terminal

node index.js
Enter fullscreen mode Exit fullscreen mode

API Tests

Look at the sample API tests written using PactumJS and mocha.

GitHub logo pactumjs / pactum

REST API Testing Tool for all levels in a Test Pyramid

// app.test.js
const pactum = require('pactum');

describe('App', () => {

  it('GET /hello', async () => {
    await pactum.spec()
      .get('http://localhost:3333/hello')
      .expectStatus(200)
      .expectBody('Hello World!');
  });

});
Enter fullscreen mode Exit fullscreen mode

To run the tests, execute the below command in terminal

mocha app.test.js
Enter fullscreen mode Exit fullscreen mode

Code Coverage

We have seen how to run the application & execute tests against it. To generate code coverage from API tests, we will be using an npm package called nyc.

Install the package globally

npm i nyc -g
Enter fullscreen mode Exit fullscreen mode

Now run your application with this magical tool nyc.

nyc node index.js
Enter fullscreen mode Exit fullscreen mode
Output

output

Run Tests

mocha app.test.js
Enter fullscreen mode Exit fullscreen mode
Output

output

Stop the application by pressing CTRL + c. Now the nyc tool will generate and display the code coverage in the terminal.

Output

output

For real and complex web applications, the code-coverage setup might not be straightforward. It might require additional steps and advanced configurations.

Conclusion

Importance of API testing is growing day by day. Not only using these tests for validating applications but also for generating code coverage metrics is an added advantage.

In most scenarios, a simple API test could cover a big chunk of source code. It means with fewer API Tests we can get more code coverage and confidence in the application.

Top comments (0)