This content originally appeared on DEV Community and was authored by Anudeep
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}`);
});
To run the application, execute the below command in terminal
node index.js
API Tests
Look at the sample API tests written using PactumJS and mocha.
// 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!');
});
});
To run the tests, execute the below command in terminal
mocha app.test.js
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
Now run your application with this magical tool nyc.
nyc node index.js
Output
Run Tests
mocha app.test.js
Output
Stop the application by pressing CTRL + c
. Now the nyc tool will generate and display the code coverage in the terminal.
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.
This content originally appeared on DEV Community and was authored by Anudeep
Anudeep | Sciencx (2021-05-07T17:40:26+00:00) Boost your code coverage with API Tests. Retrieved from https://www.scien.cx/2021/05/07/boost-your-code-coverage-with-api-tests/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.