Flag tests taking longer than usual

Hi all, I have been looking for a way to monitor test performance and would like to get a warning or a flag for any tests that take longer than usual to run.

Ideally, I want to set an expected duration e.g. 10 seconds, and add a threshold and once exceeded, get a warning/flag so that I can follow this up.

Does anyone have any ideas on how I could go about this, or can point me towards some resources that may be helpful?

A easy baked-in feature would be to make use of

Scenario(...).timeout(10000)

which will fail the test when it takes longer than 10 seconds.

But in your case you want just a warning. Hm, that is not yet a feature and requires some custom code. Here is a possible approach that you can add to your “codecept.conf.js” file:

const { event, recorder } = require('codeceptjs');

const THRESHOLD = 10000; // Set threshold in milliseconds, e.g., 10 seconds

// Hook to capture test start time
event.dispatcher.on(event.test.before, (test) => {
    test.startTime = Date.now();
});

// Hook to capture test end time and calculate duration
event.dispatcher.on(event.test.after, (test) => {
    const duration = Date.now() - test.startTime;

    if (duration > THRESHOLD) {
        console.warn(`Warning: Test "${test.title}" exceeded the duration threshold of ${THRESHOLD / 1000} seconds.`);
        console.warn(`Actual duration: ${(duration / 1000).toFixed(2)} seconds.`);
    } else {
        console.log(`Test "${test.title}" completed in ${(duration / 1000).toFixed(2)} seconds.`);
    }
});

This will then print out something like this to STDOUT: