Skip to content

How to get thread number with parallel execution for Allure report timeline? #1855

Description

@theptyza

What are you trying to achieve?

I'm running tests in parallel with 4 chunks and write reports to Allure.
I want to see tests from each chunk displayed in a separate timeline. If I manually add a "thread" label to each suite or test (in the XML) I can split the test results into multiple timelines.
parallel timelines

What do you get instead?

By default in the Allure report all tests are displayed in one timeline.
single timeline

I can add labels to test cases like

const allure = codeceptjs.container.plugins('allure');

Before(() => 
  allure.addLabel('thread', '...')
)

My question is - how can I get the chunk number in the test?

Details

  • CodeceptJS version: 2.3.0
  • NodeJS Version: 10.16.3
  • Operating System: Windows 10
  • Configuration file:
exports.config = {
  ...
  mocha: {
    reporterOptions: {
      reportDir: "./test/report",
      autoOpen: true,
    }
  },
  plugins: {
    allure: {
      enabled: true,
      outputDir: "./test/.output/allure-results"
    },
    stepByStepReport: {}
  },
  multiple: {
    parallel: {
      chunks: "4"
    }
  },
  bootstrap: false,
  teardown: null,
  hooks: [],
  tests: "./test/tests/ui/dashboard/*.js",
  timeout: 120000,
};

Running tests with

npx codeceptjs run-multiple parallel

Activity

  1. xMutaGenx commented on Aug 20, 2019

    @xMutaGenx
    Contributor

    You can retrieve your chunks configuration with the following const chunks = require('codeceptjs').config.get()['multiple']['parallel']['chunks']; alternatively you can pass in the number of chunks to the node env, which then allows you to access that globally CHUNKS=4 npx codeceptjs run-multiple parallel then you can set them in your config via:

    multiple: {
        parallel: {
          chunks: process.env.CHUNKS
        }
      }
    

    As well as access them globally:

    const allure = codeceptjs.container.plugins('allure');
    
    Before(() => 
      allure.addLabel('thread', process.env.CHUNKS)
    )
    

    Hope this helps.

  2. theptyza commented on Aug 20, 2019

    @theptyza
    Author

    @xMutaGenx thanks. What I'm trying to do is get the current chunk number.
    So that tests from chunk 1 are recorded as thread 1, tests from chunk 2 as thread 2 etc.

    With

    const chunks = require('codeceptjs').config.get()['multiple']['parallel']['chunks'];
    
    Before(() => 
      allure.addLabel('thread', process.env.CHUNKS)
    )

    Every test is recorded as thread 4 (the number of chunks).

  3. xMutaGenx commented on Aug 20, 2019

    @xMutaGenx
    Contributor

    Ah apologies @theptyza misunderstood you then, one way you could do this is by accessing the mocha child processes:

    let child = require('codeceptjs').config.get().mocha.child;

    This will give you something like 1.chrome:chunk1:chrome
    Next you'll want to get the number of your child process from the string:

    child = child.substring(0, child.indexOf('.'));

    Finally you'll have something like:

    Before(() => {
        let child = require('codeceptjs').config.get().mocha.child;
        child = child.substring(0, child.indexOf('.'));
        allure.addLabel('thread', child)
    })

    Not the cleanest approach, but it works, might allow you to continue working until a better solution appears 🤷‍♂ Hope this helps.

  4. theptyza commented on Aug 20, 2019

    @theptyza
    Author

    I was able to use process ID as thread number for Allure report by creating a helper that sets thread label for each test

    let Helper = codecept_helper;
    
    const process = require('process');
    
    class AllureHelper extends Helper {
        _before() {
            const allure = codeceptjs.container.plugins('allure');
            allure.addLabel('thread', process.pid);
        }
    }
    
    module.exports = AllureHelper;

    parallel pid

    Wondering if there is a better way to do this.

  5. theptyza commented on Aug 20, 2019

    @theptyza
    Author

    @xMutaGenx thanks! Your solution is even better since it gives the actual chunk number.

  6. Naimadnap commented on Sep 9, 2019

    @Naimadnap

    @theptyza or @xMutaGenx could you share the configuration to achive this

  7. theptyza commented on Sep 24, 2019

    @theptyza
    Author

    @Naimadnap I've created an allureHelper

    'use strict';
    
    let Helper = codecept_helper;
    
    class AllureHelper extends Helper {
        _before() {        
            let child = codeceptjs.config.get().mocha.child;
            // if there are child processes set thread label in Allure report to chunk number
            // child process name is like '1.parallel:chunk1:default'
            if(child) {
                const allure = codeceptjs.container.plugins('allure');
                allure.addLabel('thread', child.substring(0, child.indexOf('.')));
            }
        }
    }
    
    module.exports = AllureHelper;

    and included it in codecept.conf.js

    helpers: {
        Nightmare: {
          url: process.env.CODECEPT_URL || "http://localhost:3000",
          windowSize: "1920x1080",
          typeInterval: "-1"
        },
        allureHelper: {
          require: "./test/helpers/allureHelper.js",
        }
      },
  8. jancorvus commented on Mar 29, 2021

    @jancorvus
    Contributor

    What about

    const { threadId } = require('worker_threads');
    const allure = codeceptjs.container.plugins('allure');
    
    allure.addLabel('thread', threadId);

    ?

  9. locked and limited conversation to collaborators on Sep 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions