Allure Mocha

Allure Mocha npm latest version

Generate beautiful HTML reports using Allure Report and your Mocha tests.

Allure Report Mocha Example

How to start

1. Prepare your project

  1. Open a terminal and go to the project directory. For example:

    Bash
    cd /home/user/myproject
  2. Install the Allure Report command-line tool, if it is not yet installed in your operating system. Note that Allure Report requires Java, see Installation.

    Bash
    npm install --save-dev allure-commandline
    Bash
    yarn add --dev allure-commandline
    Bash
    pnpm install --dev allure-commandline
  3. Install the Allure Mocha adapter.

    Bash
    npm install --save-dev allure-mocha
    Bash
    yarn add --dev allure-mocha
    Bash
    pnpm install --dev allure-mocha
  4. In the script or configuration file that you use for running Mocha tests, specify allure-mocha as the reporter and, optionally, select the directory for test results.

    JavaScript
    const glob = require("glob"); const Mocha = require("mocha"); const mocha = new Mocha({ parallel: false, reporter: "allure-mocha", reporterOptions: { resultsDir: "allure-results", }, }); glob.sync("test/**/*.spec.js").forEach((file) => mocha.addFile(file)); mocha.run((failures) => process.exit(failures === 0 ? 0 : 1));
    JavaScript
    module.exports = { parallel: false, reporter: "allure-mocha", reporterOptions: { resultsDir: "allure-results", }, };
    YAML
    parallel: false reporter: allure-mocha reporterOptions: resultsDir: ../allure-results
    JSON
    { "parallel": false, "reporter": "allure-mocha", "reporterOptions": { "resultsDir": "allure-results" } }
    JSON
    { // ... "mocha": { "parallel": false, "reporter": "allure-mocha", "reporterOptions": { "resultsDir": "allure-results" } } }

2. Run tests

Run your Mocha tests same way as your would run them usually.

For example:

Bash
npx mocha
Bash
yarn dlx mocha
Bash
pnpx mocha

Or, if you use a runner.js file:

Bash
node runner.js

This will save necessary data into allure-results or other directory, according to the configuration. If the directory already exists, the new files will be added to the existing ones, so that a future report will be based on them all.

3. Generate a report

Finally, run Allure to convert the test results into an HTML report. This will automatically open your browser to view the report.

Bash
npx allure serve allure-results
Bash
yarn dlx allure serve allure-results
Bash
pnpx allure serve allure-results

If necessary, replace allure-results with the path to the directory specified in the configuration.

There are some options that can affect how the report is generated, see Allure command-line options.

Writing tests

The Allure Mocha adapter extends the standard reporting features of Mocha by providing additional capabilities for crafting more informative and structured tests. This section highlights key enhancements that can be utilized:

Adding Metadata

Allure allows you to enrich your reports with a variety of metadata. This additional information provides context and details for each test, enhancing the report's usefulness. Refer to the metadata reference section for an exhaustive list of what can be added.

To assign a metadata field, call a corresponding method at any point inside a test method's body. Note, however, that it is highly recommended to assign all metadata as early as possible. Otherwise, there is a risk of the test failing before having all metadata set, which is bad for the test report's readability.

JavaScript
const { allure } = require("allure-mocha/runtime"); it("all", () => { allure.description("This test attempts to create a label with specified title."); allure.owner("John Doe"); allure.link("https://dev.example.com/", "Website"); allure.issue("AUTH-123", "https://issues.example.com/AUTH-123"); allure.tms("TMS-456", "https://tms.example.com/TMS-456"); // ... });

Organize tests

As described in Improving navigation in your test report, Allure supports multiple ways to organize tests into hierarchical structures.

To specify a test's location in the behavior-based hierarchy:

JavaScript
const { allure } = require("allure-mocha/runtime"); it("behavior", () => { allure.epic("Web interface"); allure.feature("Essential features"); allure.story("Labels"); // ... });

To specify a test's location in the suite-based hierarchy:

JavaScript
const { allure } = require("allure-mocha/runtime"); it("behavior", () => { allure.parentSuite("Web interface"); allure.suite("Essential features"); allure.subSuite("Labels"); // ... });

Divide a test into steps

To create steps and sub-steps, you can use the step() function, see reference.

JavaScript
const { allure } = require("allure-mocha/runtime"); it("steps", () => { allure.step("Step 1", () => { allure.step("Sub-step 1", () => { // ... }); allure.step("Sub-step 2", () => { // ... }); }); allure.step("Step 2", () => { // ... }); });

Describe parametrized tests

Since tests in Mocha, unlike in some other frameworks, are written as anonymous functions, it is very easy to implement the parametrized tests pattern, i.e. to run the same test logic with different test data. To do so, just write the test inside a loop and use the variable parameters in both its title and its body.

To display a parameter value in the test report, pass it to the parameter() function.

JavaScript
const { allure } = require("allure-mocha/runtime"); for (const login of ["johndoe", "[email protected]"]) { it(`Test authenticate as ${login}`, () => { allure.parameter("login", login); allure.parameter("time", new Date().toUTCString(), { excluded: true }); // ... }); }

Attach screenshots and other files

In Allure reports, you have the ability to attach various types of files, which can greatly enhance the comprehensibility of the report. A common practice is to attach screenshots that capture the state of the user interface at specific moments during test execution.

For detailed instructions on how to implement attachments, refer to the attachments section in the Allure Mocha reference.

JavaScript
const { allure } = require("allure-mocha/runtime"); const fs = require("fs"); it("attachment", () => { allure.attachment("image.png", fs.readFileSync("/path/to/image.png"), "image/png"); // ... });

Select tests via a test plan file

If the ALLURE_TESTPLAN_PATH environment variable is defined and points to an existing file, Mocha will only run tests listed in this file.

Here's an example of running tests according to a file named testplan.json (assuming you use the npm package manager):

Bash
export ALLURE_TESTPLAN_PATH=testplan.json npm test
Text
setx ALLURE_TESTPLAN_PATH "testplan.json" npm test

Environment information

For the main page of the report, you can collect various information about the environment in which the tests were executed.

For example, it is a good idea to use this to remember the OS and Java versions. This may help the future reader investigate bugs that are reproducible only in some environments.

Allure Report Environments Widget

To provide environment information, put a file named environment.properties into the allure-results directory after running the tests. See the example in Environment file.

Note that this feature should be used for properties that do not change for all tests in the report. If you have properties that can be different for different tests, consider using Parametrized tests.

Powered by
logo

Join our newsletter

Join our community

We aim to make Allure Report as reliable and user-friendly as possible, and together with the community, we're here to help when problems arise.

© 2024 Qameta Software Inc. All rights reserved.