Unit Testing and Functional Testing: Automating test results reporting

It’s cool we have automated test execution, and Mocha makes the test results look nice with all those checkmarks. But what if management wants a graph of test failure trends over time? There could be any number of reasons to report test results as data rather than as a user-friendly printout on the console.

For example, tests are often not run on a developer laptop or by a quality team tester, but by automated background systems. The CI/CD model is widely used, in which tests are run by the CI/CD system on every commit to the shared code repository.

When fully implemented, if the tests all pass on a particular commit, then the system is automatically deployed to a server, possibly the production servers. In such a circumstance, the user-friendly test result report is not useful, and instead, it must be delivered as data that can be displayed on a CI/CD results dashboard website.

Mocha uses what’s called a Reporter to report test results. A Mocha Reporter is a module that prints data in whatever format it supports. More information on this can be found on the Mocha website: https://mochajs.org/#reporters.

You will find the current list of available reporters like so:

# mocha –reporters 

dot – dot matrix

doc – html documentation

spec – hierarchical spec list

json – single json object

progress – progress bar

list – spec-style listing

tap – test-anything-protocol


Then, you can use a specific Reporter, like so:

root@df3e8a7561a7:/userauth/test# npm run test — –reporter tap 

> userauth-test@1.0.0 test /userauth/test

> cross-env URL_USERS_TEST=http://localhost:5858 mocha test.mjs “– reporter” “tap”


ok 1 Users Test List user list created users

ok 2 Users Test find user find created users

ok 3 Users Test find user fail to find non-existent users

ok 4 Users Test delete user delete nonexistent users

# tests 4

# pass 4

# fail 0 

In the npm run script-name command, we can inject command-line arguments, as we’ve done here. The — token tells npm to append the remainder of its command line to the command that is executed. The effect is as if we had run this:

root@df3e8a7561a7:/userauth/test# URL_USERS_TEST=http://localhost:5858

mocha test.mjs “–reporter” “tap” 

For Mocha, the –reporter option selects which Reporter to use. In this case, we selected the TAP reporter, and the output follows that format.

Test Anything Protocol (TAP) is a widely used test results format that increases the possibility of finding higher-level reporting tools. Obviously, the next step would be to save the results into a file somewhere, after mounting a host directory into the container.

In this section, we learned about the test results reporting formats supported by Mocha. This will give you a starting point for collecting long-term results tracking and other useful software quality metrics. Often, software teams rely on quality metrics trends as part of deciding whether a product can be shipped to the public.

In the next section, we’ll round off our tour of testing methodologies by learning about a framework for frontend testing.

Source: Herron David (2020), Node.js Web Development: Server-side web development made easy with Node 14 using practical examples, Packt Publishing.

Leave a Reply

Your email address will not be published. Required fields are marked *