cypress icon indicating copy to clipboard operation
cypress copied to clipboard

Allow for random spec ordering when running tests

Open joelzimmer opened this issue 7 years ago • 12 comments

Current behavior:

When running specs in cypress, the files run in alphabetical order, and tests within each spec run in order. See #2901.

Desired behavior:

It'd be great to pass a flag into cypress (or for it to do some sort of optimization like Jest does) to randomize the order of test runs. This would allow tests that rely on other state to fail and could help increase confidence in how the app works.

Versions

3.1.3, Alpine Linux, Electron

joelzimmer avatar Dec 06 '18 19:12 joelzimmer

Just noticed there is a library for randomizing tests in Mocha by the almighty @bahmutov, would be great to integrate it to Cypress :)

https://github.com/bahmutov/rocha

Strajk avatar Apr 16 '19 15:04 Strajk

One thought: it would be nice to have a callback from Cypress into userspace code with the list of specs and tests in each. The user code then can return the list of tests - in any desired order. I kind of do this in https://github.com/bahmutov/cypress-select-tests projects where I get a callback of all tests found in the spec file and can remove all tests I don't want to run

On Tue, Apr 16, 2019 at 11:20 AM Strajk [email protected] wrote:

Just noticed there is a library for randomizing tests in Mocha by the almighty @bahmutov https://github.com/bahmutov, would be great to integrate it to Cypress :)

https://github.com/bahmutov/rocha

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/cypress-io/cypress/issues/2908#issuecomment-483707749, or mute the thread https://github.com/notifications/unsubscribe-auth/ACHApsM-jXyR5MgkhhuybvFWTHANHjBJks5vhepFgaJpZM4ZHKIm .

-- Dr. Gleb Bahmutov, PhD

Schedule video chat / phone call / meeting with me via https://calendly.com/bahmutov [email protected] @bahmutov https://twitter.com/@bahmutov https://glebbahmutov.com/ https://glebbahmutov.com/blog https://github.com/bahmutov

bahmutov avatar Apr 16 '19 15:04 bahmutov

Is there an update on this @ Cypress Team?

hcharley avatar Aug 26 '19 05:08 hcharley

+1

heathd avatar Sep 17 '19 13:09 heathd

+1

mxygem avatar Sep 18 '19 16:09 mxygem

+1

sumitngupta avatar Jan 13 '20 16:01 sumitngupta

+1

If you share where the location is in this repo that grabs spec files I can take a stab at this. Would it be enough to:

  1. randomize the spec files order
  2. use https://github.com/bahmutov/cypress-select-tests to randomize the tests within the spec files?

Or is there another reason that randomizing spec file order might break things? (Does something depend on spec file ordering?)

terencechow avatar Feb 25 '20 14:02 terencechow

I've had a go at randomising the order of the tests (it blocks) within a spec, it's not perfect yet, but is a start - https://www.npmjs.com/package/cypress-random-test-order

mncharlton avatar Jul 16 '20 15:07 mncharlton

@mncharlton Could you open a pull request to add this plugin to our documentation?

Instructions here as well as a PR Template Checklist. Thanks!

jennifer-shehane avatar Jul 28 '20 10:07 jennifer-shehane

Thanks for the prompt @jennifer-shehane! PR is here: https://github.com/cypress-io/cypress-documentation/pull/3024

mncharlton avatar Jul 28 '20 10:07 mncharlton

Any other movement on this? Seems like the cypress-random-test-order plugin is deprecated by the owner as they don't have time to work on issues.

duraz0rz avatar Aug 10 '23 13:08 duraz0rz

Hi, we'd also love to have randomized test execution (at least at spec file level), is there an alternative to brewing our own execution ordering ( - based on @bahmutov's plugin for instance) ? Cheers :)

EDIT : I achieved random test execution using the config.specPattern property : https://github.com/cypress-io/cypress/issues/390 , this is heavily inspired from @bahmutov's @cypress/grep plugin. This is very hacky but works for the time being...

// cypress/plugins/randomizeSpecs.ts
import * as fs from 'node:fs/promises';
/**
 * @param {Cypress.ConfigOptions} config
 */
export default async function randomizeSpecsPlugin(config) {
  if (!config?.env?.randomizedTestsSeed) {
    return config;
  }

  const specPattern = /.*.cy.ts/;
  const integrationFolder = process.cwd();

  const allFiles = await fs.readdir(integrationFolder, { recursive: true });
  const specFiles = allFiles.filter(s => specPattern.exec(s)).map(s => 'cypress/' + s);
 
  // Run your  custom shuffling function here - for me it's modifying specFiles in-place 
  shuffle(specFiles);

  // Warning : we're overriding existing specPattern to ensure tests are run with this new order
  // Any existing specPattern will be OVERWRITTEN
  // See https://github.com/cypress-io/cypress/issues/390  as to why we're doing things this way
  config.specPattern = specFiles;
  return config;
}

// cypress/cypress.local.config.ts

import randomizeSpecsPlugin from './plugins/randomizeSpecs';
...
async setupNodeEvents(on, config) {
    
      await randomizeSpecsPlugin(config);
      return config
}

AlexandreRozier avatar Oct 15 '24 09:10 AlexandreRozier

any news about this? thanks!

estefafdez avatar Apr 02 '25 09:04 estefafdez